Strings
String slices and string lists.
Types
oc_str8
typedef struct oc_str8
{
char* ptr;
size_t len;
} oc_str8;
A type representing a string of bytes.
Fields
ptrA pointer to the string's bytes.lenThe length of the string.
oc_str8_elt
typedef struct oc_str8_elt
{
oc_list_elt listElt;
oc_str8 string;
} oc_str8_elt;
A type representing an element of a string list.
Fields
listEltThe string element is linked into its parent string list through this field.stringThe string for this element.
oc_str8_list
typedef struct oc_str8_list
{
oc_list list;
u64 eltCount;
u64 len;
} oc_str8_list;
A type representing a string list.
Fields
listA linked-list ofoc_str8_elt.eltCountThe number of elements inlist.lenThe total length of the string list, which is the sum of the lengths over all elements.
oc_str16
typedef struct oc_str16
{
u16* ptr;
size_t len;
} oc_str16;
A type describing a string of 16-bits characters (typically used for UTF-16).
Fields
ptrA pointer to the underlying 16-bits character array.lenThe length of the string, in 16-bits characters.
oc_str16_elt
typedef struct oc_str16_elt
{
oc_list_elt listElt;
oc_str16 string;
} oc_str16_elt;
A type representing an element of an oc_str16 list.
Fields
listEltThe string element is linked into its parent string list through this field.stringThe string for this element.
oc_str16_list
typedef struct oc_str16_list
{
oc_list list;
u64 eltCount;
u64 len;
} oc_str16_list;
Fields
listA linked-list ofoc_str16_elt.eltCountThe number of elements inlist.lenThe total length of the string list, which is the sum of the lengths over all elements.
oc_str32
typedef struct oc_str32
{
u32* ptr;
size_t len;
} oc_str32;
A type describing a string of 32-bits characters (typically used for UTF-32 codepoints).
Fields
ptrA pointer to the underlying 32-bits character array.lenThe length of the string, in 32-bits characters.
oc_str32_elt
typedef struct oc_str32_elt
{
oc_list_elt listElt;
oc_str32 string;
} oc_str32_elt;
A type representing an element of an oc_str32 list.
Fields
listEltThe string element is linked into its parent string list through this field.stringThe string for this element.
oc_str32_list
typedef struct oc_str32_list
{
oc_list list;
u64 eltCount;
u64 len;
} oc_str32_list;
Fields
listA linked-list ofoc_str32_elt.eltCountThe number of elements inlist.lenThe total length of the string list, which is the sum of the lengths over all elements.
Macros
OC_STR8
#define OC_STR8(s)
Makes an oc_str8 string from a C null-terminated string.
Parameters
sA null-terminated string.
Return
An oc_str8 struct pointing to s.
oc_str8_lp
#define oc_str8_lp(s)
Expands a string s to s.len, s.ptr
You can use this macro when calling functions that expect a size_t length and pointer arguments.
Parameters
sTheoc_str8string to expand.
oc_str8_ip
#define oc_str8_ip(s)
Expands a string s to (int)s.len, s.ptr.
You can use this macro when calling functions that expect an i32 length and pointer arguments, for example:
printf(".*s", oc_str8_lp(s));
Parameters
sTheoc_str8string to expand.
oc_str8_list_first
#define oc_str8_list_first(sl)
Get the first string of a string list.
Parameters
slThe string list
Return
An oc_str8 referring to the first string of the string list, or an empty string if the list is empty.
oc_str8_list_last
#define oc_str8_list_last(sl)
Get the last string of a string list.
Parameters
slThe string list.
Return
An oc_str8 referring to the last string of the string list, or an empty string if the list is empty.
oc_str8_list_for
#define oc_str8_list_for(sl, elt)
Iterate through the strings of a string list.
This macro creates a loop with an oc_str8 iterator named after the elt macro parameter. You should follow it by the loop body, in which you can use the iterator to access each element.
Parameters
slThe string list.eltThe name of the loop iterator.
oc_str8_list_empty
#define oc_str8_list_empty(sl)
Checks if a string list is empty.
Parameters
slThe string list.
Return
true if the list is empty, false otherwise.
oc_str16_list_first
#define oc_str16_list_first(sl)
Get the first string of a string list.
Parameters
slThe string list
Return
An oc_str16 referring to the first string of the string list, or an empty string if the list is empty.
oc_str16_list_last
#define oc_str16_list_last(sl)
Get the last string of a string list.
Parameters
slThe string list.
Return
An oc_str16 referring to the last string of the string list, or an empty string if the list is empty.
oc_str16_list_for
#define oc_str16_list_for(sl, elt)
Iterate through the strings of a string list.
This macro creates a loop with an oc_str16 iterator named after the elt macro parameter. You should follow it by the loop body, in which you can use the iterator to access each element.
Parameters
slThe string list.eltThe name of the loop iterator.
oc_str16_list_empty
#define oc_str16_list_empty(sl)
Checks if a string list is empty.
Parameters
slThe string list.
Return
true if the list is empty, false otherwise.
oc_str32_list_first
#define oc_str32_list_first(sl)
Get the first string of a string list.
Parameters
slThe string list
Return
An oc_str32 referring to the first string of the string list, or an empty string if the list is empty.
oc_str32_list_last
#define oc_str32_list_last(sl)
Get the last string of a string list.
Parameters
slThe string list.
Return
An oc_str32 referring to the last string of the string list, or an empty string if the list is empty.
oc_str32_list_for
#define oc_str32_list_for(sl, elt)
Iterate through the strings of a string list.
This macro creates a loop with an oc_str32 iterator named after the elt macro parameter. You should follow it by the loop body, in which you can use the iterator to access each element.
Parameters
slThe string list.eltThe name of the loop iterator.
oc_str32_list_empty
#define oc_str32_list_empty(sl)
Checks if a string list is empty.
Parameters
slThe string list.
Return
true if the list is empty, false otherwise.
Functions
oc_str8_from_buffer
oc_str8 oc_str8_from_buffer(u64 len, char* buffer);
Make a string from a bytes buffer and a length.
Parameters
bufferA buffer of bytes.
Return
The resulting string.
oc_str8_slice
oc_str8 oc_str8_slice(oc_str8 s, u64 start, u64 end);
Make a string from a slice of another string. The resulting string designates some subsequence of the input string.
Parameters
startThe inclusive start index of the slice.endThe exclusive end index of the slice.
Return
The resulting string slice.
Note
The slice covers indices from start to end-1. start must be inferior or equal to end, and end must be inferior or equal to `s.len.
oc_str8_push_buffer
oc_str8 oc_str8_push_buffer(oc_arena* arena, u64 len, char* buffer);
Pushes a copy of a buffer to an arena, and makes a string refering to that copy.
Parameters
lenThe length of the buffer.bufferThe buffer to copy.
Return
A string designating the copy of the input buffer.
oc_str8_push_cstring
oc_str8 oc_str8_push_cstring(oc_arena* arena, const char* str);
Pushes a copy of a C null-terminated string to an arena, and makes a string referring to that copy.
Parameters
strA null-terminated string.
Return
A string referring to the copy of the input string.
oc_str8_push_copy
oc_str8 oc_str8_push_copy(oc_arena* arena, oc_str8 s);
Copy the contents of a string on an arena and make a new string referring to the copied bytes.
Parameters
arenaThe arena on which to copy the contents of the input string.sThe input string.
Return
A string referring to the copy of the contents of the input string.
oc_str8_push_slice
oc_str8 oc_str8_push_slice(oc_arena* arena, oc_str8 s, u64 start, u64 end);
Make a copy of a string slice. This function copies a subsequence of the input string onto an arena, and returns a new string referring to the copied content.
Parameters
arenaThe arena on which to copy the slice of the input string.sThe input string.startThe inclusive start index of the slice.endThe exclusive end index of the slice.
Note
The slice covers indices from start to end-1. start must be inferior or equal to end, and end must be inferior or equal to `s.len.
oc_str8_pushfv
oc_str8 oc_str8_pushfv(oc_arena* arena, const char* format, va_list args);
Build a string from a null-terminated format string and a variadic argument list, similar to vasprintf().
Parameters
arenaThe arena on which to allocate the contents of the string.formatA null-terminated format string. The format of that string is the same as that of the Csprintf()family of functions.argsA variadic argument list or arguments referenced by the format string.
Return
The result string built from the format string and variadic arguments. The backing memory of the string is allocated on arena.
oc_str8_pushf
oc_str8 oc_str8_pushf(oc_arena* arena, const char* format, ...);
Build a string from a null-terminated format string and variadic arguments, similar to asprintf().
Parameters
arenaThe arena on which to allocate the contents of the string....Additional arguments referenced by the format string.
Return
The result string built from the format string and variadic arguments. The backing memory of the string is allocated on arena.
oc_str8_cmp
i32 oc_str8_cmp(oc_str8 s1, oc_str8 s2);
Lexicographically compare the contents of two strings.
Parameters
s1The first string to compare.s2The second string to compare.
Return
This function returns -1 if s1 is less than s2, +1 if s1 is greater than s2, and 0 if s1 and s2 are equal.
oc_str8_to_cstring
char* oc_str8_to_cstring(oc_arena* arena, oc_str8 string);
Create a null-terminated C-string from an oc_str8 string.
Parameters
arenaThe arena on which to copy the contents ofstringand the null terminator.stringThe input string.
Return
A null-terminated copy of the input string's contents. This copy is allocated on arena.
oc_str8_list_push
void oc_str8_list_push(oc_arena* arena, oc_str8_list* list, oc_str8 str);
Push a string element to the back of a string list. This creates a oc_str8_elt element referring to the contents of the input string, and links that element at the end of the string list.
Parameters
arenaA memory arena on which to allocate the new string element.listThe string list to link the new element into.strThe string of the new element. Note that the contents of the string is not copied. The new string element refers to the same bytes asstr.
oc_str8_list_pushf
void oc_str8_list_pushf(oc_arena* arena, oc_str8_list* list, const char* format, ...);
Build a string from a null-terminated format string an variadic arguments, and append it to a string list.
Parameters
arenaThe arena on which to allocate the contents of the string, as well as the string element.listThe string list on which to append the new element.formatA null-terminated format string. The format of that string is the same as that of the Csprintf()family of functions....Additional arguments references by the format string.
oc_str8_list_collate
oc_str8 oc_str8_list_collate(oc_arena* arena, oc_str8_list list, oc_str8 prefix, oc_str8 separator, oc_str8 suffix);
Build a string by combining the elements of a string list with a prefix, a suffix, and separators.
Parameters
arenaAn arena on which to allocate the contents of the new string.listA string list containing the elements to combineprefixA prefix that is pasted at the beginning of the string.separatorA separator that is pasted between each element of the input string list.suffixA suffix that is pasted at the end of the string.
Return
The resulting string, whose contents is allocated on arena.
oc_str8_list_join
oc_str8 oc_str8_list_join(oc_arena* arena, oc_str8_list list);
Build a string by joining the elements of a string list.
Parameters
arenaThe arena on which to allocate the new string.listA string list containing the elements of join.
Return
The resulting string, whose contents is allocated on arena.
oc_str8_split
oc_str8_list oc_str8_split(oc_arena* arena, oc_str8 str, oc_str8_list separators);
Split a list into a string list according to separators.
No string copies are made. The elements of the resulting string list refer to subsequences of the input string.
Parameters
arenaThe arena on which to allocate the elements of the string list.strThe input string.separatorsA list of separators used to split the input string.
Return
The resulting string list.
oc_str16_from_buffer
oc_str16 oc_str16_from_buffer(u64 len, u16* buffer);
Make an oc_str16 string from a buffer of 16-bit characters.
Parameters
lenThe length of the input buffer, in characters.bufferThe 16-bits characters buffer.
Return
The result string, referencing the input buffer's characters.
oc_str16_slice
oc_str16 oc_str16_slice(oc_str16 s, u64 start, u64 end);
Make an oc_str16 string from a slice of another oc_str16 string.
Parameters
sThe input string.startThe inclusive start index of the slice.endThe exclusive end index of the slice.
Return
The resulting string slice.
Note
The slice covers indices from start to end-1. start must be inferior or equal to end, and end must be inferior or equal to `s.len.
oc_str16_push_buffer
oc_str16 oc_str16_push_buffer(oc_arena* arena, u64 len, u16* buffer);
Copy the content of a 16-bit character buffer on an arena and make a new oc_str16 referencing the copied contents.
Parameters
arenaThe arena on which to copy the input buffer.lenThe length of the buffer.bufferAn input buffer of 16-bit characters.
Return
The resulting string.
oc_str16_push_copy
oc_str16 oc_str16_push_copy(oc_arena* arena, oc_str16 s);
Copy the contents of an oc_str16 string and make a new string referencing the copied contents.
Parameters
arenaThe arena on which to copy the contents of the input string.sThe input string.
Return
The resulting string referencing the copy of the input string.
oc_str16_push_slice
oc_str16 oc_str16_push_slice(oc_arena* arena, oc_str16 s, u64 start, u64 end);
Copy a slice of an oc_str16 string an make a new string referencing the copies contents.
Parameters
arenaThe arena on which to copy the slice of the input string's contents.sThe input string.startThe inclusive start index of the slice.endThe exclusive end index of the slice.
Return
The resulting string referencing the copied contents.
Note
The slice covers indices from start to end-1. start must be inferior or equal to end, and end must be inferior or equal to `s.len.
oc_str16_list_push
void oc_str16_list_push(oc_arena* arena, oc_str16_list* list, oc_str16 str);
Push a string element to the back of a string list. This creates a oc_str16_elt element referring to the contents of the input string, and links that element at the end of the string list.
Parameters
arenaA memory arena on which to allocate the new string element.listThe string list to link the new element into.strThe string of the new element. Note that the contents of the string is not copied. The new string element refers to the same bytes asstr.
oc_str16_list_join
oc_str16 oc_str16_list_join(oc_arena* arena, oc_str16_list list);
Build a string by joining the elements of a string list.
Parameters
arenaThe arena on which to allocate the new string.listA string list containing the elements of join.
Return
The resulting string, whose content is allocated on arena.
oc_str16_split
oc_str16_list oc_str16_split(oc_arena* arena, oc_str16 str, oc_str16_list separators);
Split a list into a string list according to separators.
No string copies are made. The elements of the resulting string list refer to subsequences of the input string.
Parameters
arenaThe arena on which to allocate the elements of the string list.strThe input string.separatorsA list of separators used to split the input string.
Return
The resulting string list.
oc_str32_from_buffer
oc_str32 oc_str32_from_buffer(u64 len, u32* buffer);
Make an oc_str32 string from a buffer of 32-bit characters.
Parameters
lenThe length of the input buffer, in characters.bufferThe 32-bits characters buffer.
Return
The result string, referencing the input buffer's characters.
oc_str32_slice
oc_str32 oc_str32_slice(oc_str32 s, u64 start, u64 end);
Make an oc_str32 string from a slice of another oc_str32 string.
Parameters
sThe input string.startThe inclusive start index of the slice.endThe exclusive end index of the slice.
Return
The resulting string slice.
Note
The slice covers indices from start to end-1. start must be inferior or equal to end, and end must be inferior or equal to `s.len.
oc_str32_push_buffer
oc_str32 oc_str32_push_buffer(oc_arena* arena, u64 len, u32* buffer);
Copy the content of a 32-bit character buffer on an arena and make a new oc_str32 referencing the copied contents.
Parameters
arenaThe arena on which to copy the input buffer.lenThe length of the buffer.bufferAn input buffer of 32-bit characters.
Return
The resulting string.
oc_str32_push_copy
oc_str32 oc_str32_push_copy(oc_arena* arena, oc_str32 s);
Copy the contents of an oc_str32 string and make a new string referencing the copied contents.
Parameters
arenaThe arena on which to copy the contents of the input string.sThe input string.
Return
The resulting string referencing the copy of the input string.
oc_str32_push_slice
oc_str32 oc_str32_push_slice(oc_arena* arena, oc_str32 s, u64 start, u64 end);
Copy a slice of an oc_str32 string an make a new string referencing the copies contents.
Parameters
arenaThe arena on which to copy the slice of the input string's contents.sThe input string.startThe inclusive start index of the slice.endThe exclusive end index of the slice.
Return
The resulting string referencing the copied contents.
Note
The slice covers indices from start to end-1. start must be inferior or equal to end, and end must be inferior or equal to `s.len.
oc_str32_list_push
void oc_str32_list_push(oc_arena* arena, oc_str32_list* list, oc_str32 str);
Push a string element to the back of a string list. This creates a oc_str32_elt element referring to the contents of the input string, and links that element at the end of the string list.
Parameters
arenaA memory arena on which to allocate the new string element.listThe string list to link the new element into.strThe string of the new element. Note that the contents of the string is not copied. The new string element refers to the same bytes asstr.
oc_str32_list_join
oc_str32 oc_str32_list_join(oc_arena* arena, oc_str32_list list);
Build a string by joining the elements of a string list.
Parameters
arenaThe arena on which to allocate the new string.listA string list containing the elements of join.
Return
The resulting string, whose contents is allocated on arena.
oc_str32_split
oc_str32_list oc_str32_split(oc_arena* arena, oc_str32 str, oc_str32_list separators);
Split a list into a string list according to separators.
No string copies are made. The elements of the resulting string list refer to subsequences of the input string.
Parameters
arenaThe arena on which to allocate the elements of the string list.strThe input string.separatorsA list of separators used to split the input string.
Return
The resulting string list.