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

  • ptr A pointer to the string's bytes.
  • len The 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

  • listElt The string element is linked into its parent string list through this field.
  • string The 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

  • list A linked-list of oc_str8_elt.
  • eltCount The number of elements in list.
  • len The 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

  • ptr A pointer to the underlying 16-bits character array.
  • len The 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

  • listElt The string element is linked into its parent string list through this field.
  • string The string for this element.

oc_str16_list

typedef struct oc_str16_list
{
    oc_list list;
    u64 eltCount;
    u64 len;
} oc_str16_list;

Fields

  • list A linked-list of oc_str16_elt.
  • eltCount The number of elements in list.
  • len The 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

  • ptr A pointer to the underlying 32-bits character array.
  • len The 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

  • listElt The string element is linked into its parent string list through this field.
  • string The string for this element.

oc_str32_list

typedef struct oc_str32_list
{
    oc_list list;
    u64 eltCount;
    u64 len;
} oc_str32_list;

Fields

  • list A linked-list of oc_str32_elt.
  • eltCount The number of elements in list.
  • len The 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

  • s A 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

  • s The oc_str8 string 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

  • s The oc_str8 string to expand.

oc_str8_list_first

#define oc_str8_list_first(sl)

Get the first string of a string list.

Parameters

  • sl The 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

  • sl The 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

  • sl The string list.
  • elt The name of the loop iterator.

oc_str8_list_empty

#define oc_str8_list_empty(sl)

Checks if a string list is empty.

Parameters

  • sl The 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

  • sl The 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

  • sl The 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

  • sl The string list.
  • elt The name of the loop iterator.

oc_str16_list_empty

#define oc_str16_list_empty(sl)

Checks if a string list is empty.

Parameters

  • sl The 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

  • sl The 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

  • sl The 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

  • sl The string list.
  • elt The name of the loop iterator.

oc_str32_list_empty

#define oc_str32_list_empty(sl)

Checks if a string list is empty.

Parameters

  • sl The 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

  • buffer A 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

  • start The inclusive start index of the slice.
  • end The 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

  • len The length of the buffer.
  • buffer The 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

  • str A 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

  • arena The arena on which to copy the contents of the input string.
  • s The 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

  • arena The arena on which to copy the slice of the input string.
  • s The input string.
  • start The inclusive start index of the slice.
  • end The 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

  • arena The arena on which to allocate the contents of the string.
  • format A null-terminated format string. The format of that string is the same as that of the C sprintf() family of functions.
  • args A 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

  • arena The 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

  • s1 The first string to compare.
  • s2 The 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

  • arena The arena on which to copy the contents of string and the null terminator.
  • string The 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

  • arena A memory arena on which to allocate the new string element.
  • list The string list to link the new element into.
  • str The string of the new element. Note that the contents of the string is not copied. The new string element refers to the same bytes as str.

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

  • arena The arena on which to allocate the contents of the string, as well as the string element.
  • list The string list on which to append the new element.
  • format A null-terminated format string. The format of that string is the same as that of the C sprintf() 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

  • arena An arena on which to allocate the contents of the new string.
  • list A string list containing the elements to combine
  • prefix A prefix that is pasted at the beginning of the string.
  • separator A separator that is pasted between each element of the input string list.
  • suffix A 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

  • arena The arena on which to allocate the new string.
  • list A 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

  • arena The arena on which to allocate the elements of the string list.
  • str The input string.
  • separators A 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

  • len The length of the input buffer, in characters.
  • buffer The 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

  • s The input string.
  • start The inclusive start index of the slice.
  • end The 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

  • arena The arena on which to copy the input buffer.
  • len The length of the buffer.
  • buffer An 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

  • arena The arena on which to copy the contents of the input string.
  • s The 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

  • arena The arena on which to copy the slice of the input string's contents.
  • s The input string.
  • start The inclusive start index of the slice.
  • end The 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

  • arena A memory arena on which to allocate the new string element.
  • list The string list to link the new element into.
  • str The string of the new element. Note that the contents of the string is not copied. The new string element refers to the same bytes as str.

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

  • arena The arena on which to allocate the new string.
  • list A 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

  • arena The arena on which to allocate the elements of the string list.
  • str The input string.
  • separators A 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

  • len The length of the input buffer, in characters.
  • buffer The 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

  • s The input string.
  • start The inclusive start index of the slice.
  • end The 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

  • arena The arena on which to copy the input buffer.
  • len The length of the buffer.
  • buffer An 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

  • arena The arena on which to copy the contents of the input string.
  • s The 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

  • arena The arena on which to copy the slice of the input string's contents.
  • s The input string.
  • start The inclusive start index of the slice.
  • end The 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

  • arena A memory arena on which to allocate the new string element.
  • list The string list to link the new element into.
  • str The string of the new element. Note that the contents of the string is not copied. The new string element refers to the same bytes as str.

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

  • arena The arena on which to allocate the new string.
  • list A 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

  • arena The arena on which to allocate the elements of the string list.
  • str The input string.
  • separators A list of separators used to split the input string.

Return

The resulting string list.