Skip to content

Commit

Permalink
let UTF8StringPiece be standalone
Browse files Browse the repository at this point in the history
Summary:
Currently, `UTF8StringPiece` is defined in terms of `Range<boost::u8_to_u32_iterator<...>>`. But this makes certain necessary changes in `Range` fail to compile. Change `UTF8StringPiece` to be a standalone range type.

Unblocks {D60181556} (aka #2265), which intends to resolve a problem blocking `folly::StringPiece` from being formattable with `fmt::format` since `fmt-11`.

Reviewed By: vitaut

Differential Revision: D60252703

fbshipit-source-id: 282d166c1456bdc5e311f4ed7c3ce8eae91422b0
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Jul 26, 2024
1 parent fe6b890 commit 12b11d0
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions folly/UTF8String.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,33 @@

namespace folly {

template <
class Iterator = const char*,
class Base = folly::Range<boost::u8_to_u32_iterator<Iterator>>>
class UTF8Range : public Base {
class UTF8StringPiece {
public:
/* implicit */ UTF8Range(const folly::Range<Iterator> baseRange)
: Base(
boost::u8_to_u32_iterator<Iterator>(
baseRange.begin(), baseRange.begin(), baseRange.end()),
boost::u8_to_u32_iterator<Iterator>(
baseRange.end(), baseRange.begin(), baseRange.end())) {}
/* implicit */ UTF8Range(const std::string& baseString)
: Base(folly::Range<Iterator>(baseString)) {}
using iterator = boost::u8_to_u32_iterator<const char*>;
using size_type = std::size_t;

/* implicit */ UTF8StringPiece(const folly::StringPiece piece)
: begin_{piece.begin(), piece.begin(), piece.end()},
end_{piece.end(), piece.begin(), piece.end()} {}
template <
typename T,
std::enable_if_t<std::is_convertible_v<T, folly::StringPiece>, int> = 0>
/* implicit */ UTF8StringPiece(const T& t)
: UTF8StringPiece(folly::StringPiece(t)) {}

iterator begin() const noexcept { return begin_; }
iterator cbegin() const noexcept { return begin_; }
iterator end() const noexcept { return end_; }
iterator cend() const noexcept { return end_; }

bool empty() const noexcept { return begin_ == end_; }
size_type walk_size() const {
return static_cast<size_type>(std::distance(begin_, end_));
}

private:
iterator begin_;
iterator end_;
};

using UTF8StringPiece = UTF8Range<const char*>;

} // namespace folly

0 comments on commit 12b11d0

Please sign in to comment.