StringView
A StringView references an immutable range of memory. It consists of a pointer bytes and an
integer numBytes. A StringView does not own the memory it points to, and no heap memory is freed
when the StringView is destroyed. The caller is responible for ensuring that memory referenced by
StringView remains valid for the lifetime of the StringView object.
StringView objects often reference UTF-8-encoded text, but this isn't a requirement. The memory
referenced by a StringView can hold any kind of data.
Even though StringView objects aren't required to contain text, they provide many member functions
to help manipulate text, such as trim(), splitByte() and upperAsc(). These text manipulation
functions are generally intended to work with UTF-8-encoding strings, but most of them will also
work with any 8-bit format compatible with ASCII, such as ISO 8859-1 or Windows-1252.
When a StringView object does contain text, the text string is generally not null-terminated. If
you need a null-terminated string (for example, to pass to a third-party library), you must
construct a string that includes the null terminator byte yourself. The null terminator then counts
towards the total number of bytes in numBytes. A convenience function withNullTerminator() is
provided for this.
Several StringView functions accept byte offset arguments, such as subStr(), left() and
right(). Be aware that when a StringView contains UTF-8-encoded text, byte offsets are not
necessarily the same as the number of characters (Unicode points) encoded by the string. For more
information, see Unicode Support.
Header File
#include <ply-runtime/string/StringView.h>
Also included from <ply-runtime/Base.h>.
Data Members
-
The first byte in the immutable memory range.
-
The number of bytes in the immutable memory range.
Member Functions
-
Constructs an empty
StringView. -
template <typename U, std::size_t N, std::enable_if_t<std::is_same<U, std::decay_t<decltype(*u8"")>>::value, bool> = false>
StringView::StringView(const U& s[])[code] -
Constructs a
StringViewfrom a null-terminated string. The string memory is expected to remain valid for the lifetime of theStringView. Note that the null terminator character does not count towardsnumBytes. For example,StringView{"hello"}results innumBytesequal to 5.When the argument is a C-style string literal, compilers are able to compute
numBytesat compile time if optimization is enabled.The second form of this constructor exists in order to support C++20 compilers, where the type of UTF-8 string literals such as
u8"hello"has been changed fromconst char*(before C++20) toconst char8_t*(after C++20).StringViewsimply interprets such literals asconst char*. -
Constructs a
StringViewfrom a single byte.cis expected to remain valid for the lifetime of theStringView. -
Constructs a
StringViewexplicitly from the arguments. The string memory is expected to remain valid for the lifetime of theStringView. -
Returns a
StringViewreferencing an immutable range of memory between two pointers. The number of bytes in the memory range is given byendByte-startByte, andendByteis considered a pointer to the first byte after the memory range. const char& StringView::operator[](u32 index) const[code]-
Subscript operator with runtime bounds checking.
-
Reverse subscript operator with runtime bound checking. Expects a negative index.
-1returns the last byte of the given string;-2returns the second-last byte, etc. -
Returns
bytes + numBytes. This pointer is considered to point to the first byte after the memory range. -
Moves
bytesforward and subtracts the given number of bytes fromnumBytes. -
Advances the end of the memory range by
ofsbytes while keeping the start of the memory range unchanged. template <typename T>
T StringView::to(const T& defaultValue = subst::createDefault<T>()) const[code]-
Parse the given string directly as
Type. Whitepsace is trimmed from the beginning and end of the string before parsing occurs. If the string cannot be parsed, or if the string is not completely consumed by the parse operation,defaultValueis returned.StringView{"123"}.to<u32>(); // returns 123 StringView{" 123 "}.to<u32>(); // returns 123 StringView{"abc"}.to<u32>(); // returns 0 StringView{"123a"}.to<u32>(); // returns 0 StringView{""}.to<u32>(); // returns 0 StringView{""}.to<s32>(-1); // returns -1This function uses
ViewInStreaminternally. If you need to distinguish between a successful parse and an unsuccessful one, create and use aViewInStreamobject directly. explicit StringView::operator bool() const[code]-
Explicit conversion to
bool. Returnstrueif the length of the string is greater than 0. Allows you to use aStringobject inside anifcondition.if (str) { ... } -
Returns
trueif the length of the string is 0. -
Returns a substring that starts at the offset given by
start. The optionalnumBytesargument determines the length of the substring in bytes. IfnumBytesis not specified, the substring continues to the end of the string. -
Returns
trueifcurBytepoints to a byte inside the memory range. -
Returns a substring that contains only the first
numBytesbytes of the string. -
Returns a substring with the last
numBytesbytes of the string omitted. -
Returns a substring that contains only the last
numBytesbytes of the string. -
Returns the offset of the first occurence of
matchBytein the string, or-1if not found. The search begins at the offset specified bystartPos. This function can find ASCII codes in UTF-8 encoded strings, since ASCII codes are always encoded as a single byte in UTF-8. template <typename MatchFunc>
s32 StringView::findByte(const MatchFunc& matchFunc, u32 startPos = 0) const[code]-
A template function that returns the offset of the first byte for which
matchFuncreturnstrue, or-1if none. The search begins at the offset specified bystartPos. This function can be used to find a whitespace character by callingfindByte(isWhite). -
template <typename MatchFunc>
s32 StringView::rfindByte(const MatchFunc& matchFunc, u32 startPos) const[code] -
Reverse
findBytefunctions. Returns the offset of the last byte in the string that matches the first argument (if passed achar) or for which the first argument returns true (if passed a function). The optionalstartPosargument specifies an offset at which to begin the search. -
Returns
trueif the string starts witharg. -
Returns
trueif the string ends witharg. StringView StringView::trim(bool* matchFunc(char) = isWhite, bool left = true, bool right = true) const[code]-
Returns a substring with leading and/or trailing bytes removed. Bytes are removed if
trueis returned when passed tomatchFunc. These functions can be used to trim whitespace characters from a UTF-8 string, for example by callingtrim(isWhite), since whitespace characters are each encoded as a single byte in UTF-8.ltrim()trims leading bytes only,rtrim()trims trailing bytes only, andtrim()trims both leading and trailing bytes. -
Returns a new
Stringcontaining every item incompsconcatenated together, with the given string used as a separator. For example:StringView{", "}.join({"a", "b", "c"}); // returns "a, b, c" StringView{""}.join({"a", "b", "c"}); // returns "abc" -
Returns a list of the words in the given string using
sepas a delimiter byte. -
Returns a new
Stringwith all lowercase ASCII characters converted to uppercase. This function works with UTF-8 strings. Also works with any 8-bit text encoding compatible with ASCII. -
Returns a new
Stringwith all uppercase ASCII characters converted to lowercase. This function works with UTF-8 strings. Also works with any 8-bit text encoding compatible with ASCII. -
Returns a new
Stringwith the bytes reversed. This function is really only suitable when you know that all characters contained in the string are encoded in a single byte. -
Returns a new
Stringwith UTF-8 characters reversed. For example, ifscontains the string"ππΊπ"encoded as UTF-8,s.reversedUTF8()returns the string"ππΊπ"encoded as UTF-8. -
Returns a new
Stringwith each byte passed through the providedfilterFunc. It's safe to call this function on UTF-8 encoded strings as long asfilterFuncleaves byte values greater than or equal to 128 unchanged. Therefore, this function is mainly suitable for filtering ASCII codes. -
Returns
trueif the last byte in the string is a zero byte. -
If the last byte of the given string is not a zero byte, this function allocates memory for a new string, copies the contents of the given string to it, appends a zero byte and returns the new string. In that case, the new string's
numByteswill be one greater than thenumBytesof the original string. If the last byte of the given string is already a zero byte, a view of the given string is returned and no new memory is allocated. -
If the last byte of the given string is not a zero byte, returns a view of the given string. If the last byte of the given string is a zero byte, returns a substring with the last byte omitted.
-
Returns:
-1ifaprecedesbin sorted order0if the strings are equal1ifafollowsbin sorted order
Strings are sorted by comparing the unsigned value of each byte. If one of the strings contains the other as a prefix, the shorter string comes first in sorted order.
bool operator==(StringView a, StringView b)[code]bool operator!=(StringView a, StringView b)[code]bool operator<(StringView a, StringView b)[code]bool operator<=(StringView a, StringView b)[code]bool operator>(StringView a, StringView b)[code]bool operator>=(StringView a, StringView b)[code]-
Comparison functions.
a<bifaprecedesbin sorted order. -
Returns a new
Stringcontaining the concatenation of twoStringViews. -
Returns a new
Stringcontaining the contents of the givenStringViewrepeatedcounttimes.StringView{'*'} * 10; // returns "**********"