folly::Range

A lightweight, non-owning view over a range of elements.

Synopsis

Declared in <folly/Range.h>

template<class Iter>
class Range;

Description

A Range refers to the half-open interval [begin, end)] delimited by a pair of iterators. It does not own or manage the underlying data, so the referenced storage must outlive the range.

Type Aliases

NameDescription
const_iterator The constant iterator type over the range.
const_range_type The corresponding range type with a constant element type.
const_reference The constant reference type to an element of the range.
difference_type The signed integer type used for iterator differences.
iterator The iterator type over the range.
reference The reference type to an element of the range.
size_type The unsigned integer type used for sizes and offsets.
traits_type The character-traits-like type used for element comparisons.
value_type The type of the elements referenced by the range.

Member Functions

NameDescription
Range [constructor]Constructors
operator= Assignment operators
advance Advance the beginning of the range by a number of elements.
assign Assign start and end iterators
at Access the element at a given index with bounds checking.
back Access the last element.
begin Get the iterator to the first element.
castToConst Get a copy of this range with a constant element type.
cbegin Get the constant iterator to the first element.
cend Get the constant iterator past the last element.
clear Clear start and end iterators
compare Lexicographically compare this range with another.
contains contains overloads
data Get a pointer or iterator to the first element.
empty Check whether the range is empty.
end Get the iterator past the last element.
endsWith endsWith overloads
ends_with ends_with overloads
equals Compare this range with another for equality, using a custom comparator.
erase Remove the items in [b, e), as long as this subrange is at the beginning] or end of the Range.
find find overloads
find_first_of find_first_of overloads
front Access the first element.
operator[] Access the element at a given index without bounds checking.
pop_back Remove the last element from the range.
pop_front Remove the first element from the range.
removePrefix removePrefix overloads
removeSuffix removeSuffix overloads
replaceAll Replaces all occurrences of 'source' with 'dest'. Returns number of replacements made. Source and dest have to have the same length. Throws if the lengths are different. If 'source' is a pattern that is overlapping with itself, we perform sequential replacement: "aaaaaaa".replaceAll("aa", "ba") --> "bababaa"
replaceAt Replaces the content of the range, starting at position 'pos', with contents of 'replacement'. Entire 'replacement' must fit into the range. Returns false if 'replacements' does not fit. Example use:
reset reset overloads
rfind Find the last occurrence of an element.
size Get the number of elements in the range.
split_step split_step overloads
start Get the iterator to the first element.
startsWith startsWith overloads
starts_with starts_with overloads
str Convert the range to a std::string. Works only for Range<const char*> and Range<char*>.
subpiece Return a window into the current range, starting at first, and spanning length characters (or until the end of the current range, whichever comes first).
substr Return a substring window into the range. Works only for character ranges.
subtract Retract the end of the range by a number of elements.
swap Swap the contents of this range with another.
to to overloads
toString Convert the range to a std::string.
uncheckedAdvance Advance the beginning of the range without bounds checking.
uncheckedSubpiece Return a subrange window without bounds checking.
uncheckedSubtract Retract the end of the range without bounds checking.
walk_size Get the number of elements by walking the iterators.
operator Tgt Conversion operators

Static Data Members

NameDescription
npos Sentinel offset returned by search functions when no match is found.

Non-Member Functions

NameDescription
crangeCreate a constant range referencing a constant C array.
crangeCreate a constant range referencing an initializer list.
crangeCreate a constant range referencing the contents of a container.
crangeCreate a constant range referencing a constant std::array.
decodeVarintDecode a value from a given buffer, advances data past the returned value. Throws on error.
operator!=Compare two ranges for inequality.
operator!=Inequality operator
operator""_spCreate a range from a char32_t string literal.
operator""_spCreate a StringPiece from a char string literal.
operator""_spCreate a range from a char16_t string literal.
operator""_spCreate a range from a char8_t string literal.
operator""_spCreate a range from a wchar_t string literal.
operator<Test whether one range is lexicographically less than another.
operator<=Test whether one range is lexicographically less than or equal to another.
operator<=>Three-way comparison operator
operator==Templated comparison operators
operator==Equality operator
operator>Test whether one range is lexicographically greater than another.
operator>=Test whether one range is lexicographically greater than or equal to another.
qfindFinds the first occurrence of needle in haystack. The result is the offset reported to the beginning of haystack, or string::npos if needle wasn't found.
qfindFinds the first occurrence of needle in haystack. The algorithm is on average faster than O(haystack.size() * needle.size()) but not as fast as Boyer-Moore. On the upside, it does not do any upfront preprocessing and does not allocate memory.
qfind_first_ofFinds the first occurrence of any element of needle in haystack. The algorithm is O(haystack.size() * needle.size()).
qfind_first_ofFind the first occurrence of any element of needles in haystack, using a custom comparator.
rangeCreate a range referencing a C array.
rangeCreate a range referencing a constant std::array.
rangeCreate a range referencing the contents of a contiguous-storage container.
rangeCreate a range referencing an initializer list.
rangeCreate a range from two iterators, with type deduction.
rangeCreate a range referencing the contents of a constant container.
rangeCreate a range referencing a std::array.
rangeCreate a range referencing a constant C array.
rfindFinds the last occurrence of needle in haystack. The result is the offset reported to the beginning of haystack, or string::npos if needle wasn't found.
swapSwap the contents of two ranges.
tag_invokeReturns a range that enumerates the set in order-preserving reinsertion order.
tag_invokeReturns a range that enumerates the set in order-preserving reinsertion order.
tag_invokeReturns a range that enumerates the map in order-preserving reinsertion order.
tryDecodeVarintA variant of decodeVarint() that does not throw on error. Useful in contexts where only part of a serialized varint may be attempted to be decoded, e.g., when a serialized varint arrives on the boundary of a network packet.