str_view.h#
String view.
Authors#
TheSilvered
Structs#
Nst_StrView#
Synopsis:
typedef struct _Nst_StrView {
u8 *value;
usize len;
} Nst_StrView
Description:
A structure representing a string view. It does not own the data in value.
Fields:
value: the string datalen: the string length
Functions#
Nst_sv_new#
Synopsis:
Nst_StrView Nst_sv_new(u8 *value, usize len)
Description:
Create a new Nst_StrView given the value
and the len.
Nst_sv_new_c#
Synopsis:
Nst_StrView Nst_sv_new_c(const char *value)
Description:
Create a new Nst_StrView given a
NUL-terminated string.
Nst_sv_from_str#
Synopsis:
Nst_StrView Nst_sv_from_str(Nst_Obj *str)
Description:
Create a new Nst_StrView from a Nest Str
object.
Nst_sv_from_str_slice#
Synopsis:
Nst_StrView Nst_sv_from_str_slice(Nst_Obj *str, usize start_idx, usize end_idx)
Description:
Create a new Nst_StrView from a slice of a
Nest Str object.
Parameters:
str: the string to slicestart_idx: the starting character index, included in the sliceend_idx: the ending character index, excluded from the slice
Returns:
The new Nst_StrView. It will have a len of
0 and a value of NULL if the indices are the same or are invalid. Any
end_idx beyond the end of the string is clamped to the length of the string.
Nst_str_from_sv#
Synopsis:
Nst_ObjRef *Nst_str_from_sv(Nst_StrView sv)
Description:
Create a new Nest Str object from an
Nst_StrView.
Nst_sv_next#
Synopsis:
isize Nst_sv_next(Nst_StrView sv, isize idx, u32 *out_ch)
Description:
Iterate through a Nst_StrView, assuming it
contains UTF-8-encoded data.
Note
idx always points to the index of the first byte of the current character,
therefore this function can be used with
Nst_sv_prev to navigate a
Nst_StrView.
Parameters:
sv: the string view to iterate throughidx: the current iteration index, use-1to begin iteratingout_ch: pointer set to the codepoint of the character, it may beNULL
Returns:
The next index to pass to this function to continue iterating or a negative number when no more characters are available.
Nst_sv_prev#
Synopsis:
isize Nst_sv_prev(Nst_StrView sv, isize idx, u32 *out_ch)
Description:
Iterate through a Nst_StrView from the end,
assuming it contains UTF-8-encoded data.
Note
idx always points to the index of the first byte of the current character,
therefore this function can be used with
Nst_sv_next to navigate a
Nst_StrView.
Parameters:
sv: the string view to iterate throughidx: the current iteration index, use-1to begin iteratingout_ch: pointer set to the codepoint of the character, it may beNULL
Returns:
The next index to pass to this function to continue iterating or a negative number when no more characters are available.
Nst_sv_parse_int#
Synopsis:
bool Nst_sv_parse_int(Nst_StrView sv, u8 base, u32 flags, u32 sep,
i64 *out_num, Nst_StrView *out_rest)
Description:
Parse an integer from sv. Any leading whitespace is ignored. Whitespace is
determined using
Nst_unicode_is_whitespace.
The following flags are accepted and modify the behaviour of the function:
Nst_SVFLAG_CAN_OVERFLOW: allows the integer to overflow over 2^63-1 or underflow below-2^63;Nst_SVFLAG_FULL_MATCH: requires the whole string to be matched as the number leaving no characters behind, whitespace after the number is trimmed;
The integer is composed of the following parts:
- an optional sign (
+or-) - an optional
0bor0Bprefix ifbaseis0or2; - an optional
0oor0Oprefix ifbaseis0or8; - an optional
0xor0Xprefix ifbaseis0or16; - a run of digits, optionally separated by
sep, there must be at least one digit between two separators.
The function parses as many digits as it can. If the base is 0 it is
determined with the prefix: (0b or 0B for binary, 0o or 0O for octal,
0x or 0X for hexadecimal and no prefix for decimal).
For bases 2 to 10 the run of digits consists of characters [0-9] from base 11
to 36 the letters [a-z] and [A-Z] are used, the casing is ignored. Hence,
valid bases are {0, 2, 3, ..., 35, 36}.
Parameters:
sv: the string view to parsebase: the integer baseflags: parsing flagssep: an digit separator, if set to 0 no separator is allowedout_num: pointer set to the parsed number, can be NULLout_rest: pointer set to the remaining part ofsv, can be NULL; any whitespace after the number is always trimmed
Returns:
true on success and false on failure. On failure out_num is set to 0 if
not NULL and out_rest is equal to sv.
Nst_sv_parse_byte#
Synopsis:
bool Nst_sv_parse_byte(Nst_StrView sv, u8 base, u32 flags, u32 sep,
u8 *out_num, Nst_StrView *out_rest)
Description:
Parse an unsigned byte from sv. Any leading whitespace is ignored. Whitespace
is determined using
Nst_unicode_is_whitespace.
The following flags are accepted and modify the behaviour of the function:
Nst_SVFLAG_CAN_OVERFLOW: allows the byte to overflow over 255 or underflow below0;Nst_SVFLAG_FULL_MATCH: requires the whole string to be matched as the number leaving no characters behind, whitespace after the number is trimmed;Nst_SVFLAG_CHAR_BYTE: allows single ASCII characters to be parsed as bytes, requires thebprefix where possible or the0hprefix in hexadecimal notation
The byte is composed of the following parts:
- an optional sign (
+or-), if there is a minus sign and overflow is not allowed only'-0'is a valid byte. - an optional
0bor0Bprefix ifbaseis0or2; - an optional
0oor0Oprefix ifbaseis0or8 - an optional
0x,0X,0hor0Hprefix ifbaseis0or16 - a run of digits, optionally separated by underscores (
_) - an optional suffix
borB, this is not considered withbase > 10asbbecomes a digit. This suffix is required whenNst_SVFLAG_CHAR_BYTEis set.
The function parses as many digits as it can. If the base is 0 it is
determined with the prefix: (0b or 0B for binary, 0o or 0O for octal,
0x or 0X for hexadecimal and no prefix for decimal).
For bases 2 to 10 the run of digits consists of characters [0-9] from base 11
to 36 the letters [a-z] and [A-Z] are used, the casing is ignored. Hence,
valid bases are {0, 2, 3, ..., 35, 36}.
Parameters:
sv: the string view to parsebase: the number baseflags: parsing flagsout_num: pointer set to the parsed number, can be NULLout_rest: pointer set to the remaining part ofsv, can be NULL; any whitespace after the number is always trimmed
Returns:
true on success and false on failure. On failure out_num is set to 0 if
not NULL and out_rest is equal to sv.
Nst_sv_parse_real#
Synopsis:
bool Nst_sv_parse_real(Nst_StrView sv, u32 flags, u32 sep, f64 *out_num,
Nst_StrView *out_rest)
Description:
Parse a double precision floating point number from sv. Any leading whitespace
is ignored. Whitespace is determined using
Nst_unicode_is_whitespace.
The following flags are accepted and modify the behaviour of the function:
Nst_SVFLAG_FULL_MATCH: requires the whole string to be matched as the number leaving no characters behind, whitespace after the number is trimmed;Nst_SVFLAG_DISABLE_SEP: if set does not allow_to be used as a separator;Nst_SVFLAG_STRICT_REAL: requires the decimal point and that one digit be inserted both before and after it.
The number is composed of the following parts:
- an optional sign (
+or-) - an optional run of digits, optionally separated by underscores (
_), required withNst_SVFLAG_STRICT_REAL - an optional dot (
.), required withNst_SVFLAG_STRICT_REAL - an optional run of digits, optionally separated by underscores (
_), required withNst_SVFLAG_STRICT_REAL - an optional exponent:
eorEfollowed by an optional sign (+or-) followed by a run of digits, optionally separated by underscores (_)
Note
At least one digit is always required between the sign and the exponent.
Parameters:
sv: the string view to parseflags: parsing flagsout_num: pointer set to the parsed number, can be NULLout_rest: pointer set to the remaining part ofsv, can be NULL; any whitespace after the number is always trimmed
Returns:
true on success and false on failure. On failure out_num is set to 0 if
not NULL and out_rest is equal to sv.
Nst_sv_compare#
Synopsis:
i32 Nst_sv_compare(Nst_StrView str1, Nst_StrView str2)
Description:
Compare two Nst_StrView's.
Returns:
The one of the following values:
> 0ifstr1 > str2== 0ifstr1 == str2< 0ifstr1 < str2
Nst_sv_lfind#
Synopsis:
isize Nst_sv_lfind(Nst_StrView str, Nst_StrView substr)
Description:
Search for substr inside str from the beginning.
Returns:
The index where the first occurrence of substr appears. If substr is not
found in str then the function returns -1.
Nst_sv_rfind#
Synopsis:
isize Nst_sv_rfind(Nst_StrView str, Nst_StrView substr)
Description:
Search for substr inside str from the end.
Returns:
The index where the last occurrence of substr appears. If substr is not
found in str then the function returns -1.
Nst_sv_ltok#
Synopsis:
Nst_StrView Nst_sv_ltok(Nst_StrView str, Nst_StrView substr)
Description:
Search for substr inside str from the beginning.
Returns:
A new string view starting from the end of the first occurrence of substr. If
substr is not found, the view will have length 0 and a NULL value.
Nst_sv_rtok#
Synopsis:
Nst_StrView Nst_sv_rtok(Nst_StrView str, Nst_StrView substr)
Description:
Search for substr inside str from the end.
Returns:
A new string view starting from the end of the last occurrence of substr. If
substr is not found, the view will have length 0 and a NULL value.
Enums#
Nst_SvNumFlags#
Synopsis:
typedef enum _Nst_SvNumFlags {
Nst_SVFLAG_CAN_OVERFLOW = 1,
Nst_SVFLAG_FULL_MATCH = 2,
Nst_SVFLAG_CHAR_BYTE = 4,
Nst_SVFLAG_STRICT_REAL = 8
} Nst_SvNumFlags
Description:
Flags for Nst_sv_parse_int,
Nst_sv_parse_byte and
Nst_sv_parse_real.