kiba-engine
Data Structures | Typedefs | Functions
string.h File Reference

Custom library for interactions with strings using string views. More...

#include <kiba/allocators/allocator.h>
#include <kiba/containers/array.h>
#include <kiba/defines.h>
Include dependency graph for string.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  string
 Structure for a string. More...
 
struct  string_view
 Non owning views on actual strings. More...
 

Typedefs

typedef struct string string
 Structure for a string. More...
 
typedef struct string_view string_view
 Non owning views on actual strings. More...
 

Functions

KB_API string string_from_raw (const char *raw)
 Construct a string from a raw string. More...
 
KB_API string string_copy_raw (const char *raw, allocator *alloc)
 
KB_API string string_from_raw_n (const char *raw, usize n)
 Construct a string from n chars of raw character data. More...
 
KB_API string string_join_views (const array_of(string_view) views, const char sep, allocator *alloc)
 Creates a new single string based on all provided views separated by a separator. More...
 
KB_API string string_from_view (const string_view view, allocator *alloc)
 Creates a new string from a string view. More...
 
KB_API string string_from_views (const array_of(string_view) views, allocator *alloc)
 Creates a new single string based on all provided views. More...
 
KB_API void string_destroy (string *str)
 Free data of the string. More...
 
KB_API b8 string_is_valid (const string str)
 Check if string is valid. More...
 
KB_API usize string_index_of (const string str, const char c)
 Get the first index of a letter in a string. More...
 
KB_API string string_clone (const string str, allocator *alloc)
 Clone a complete string. More...
 
KB_API b8 string_equal (const string lhs, const string rhs)
 Check if the contents of two strings are equal. More...
 
KB_API b8 string_n_equal (const string lhs, const string rhs, usize n)
 Check if the contents of two strings are equal. More...
 
KB_API string_view string_view_from_string (const string str)
 Create a new string view of a whole string. More...
 
KB_API const char * string_view_data (const string_view view)
 Get raw pointer to beginning of the string_view data. More...
 
KB_API b8 string_view_equal (const string_view lhs, const string_view rhs)
 Check if the contents of two string views are equal. More...
 
KB_API b8 string_view_n_equal (const string_view lhs, const string_view rhs, usize n)
 Check if the contents of two string views are equal. More...
 
KB_API string_view string_view_at_offset (const string_view view, usize offset)
 Create a new string_view of an existing string_view. More...
 
KB_API string_view string_view_subview (const string_view view, usize start, usize length)
 Create a new string_view of an existing string_view. More...
 
KB_API char string_view_char_at (const string_view view, usize index)
 Get the letter at an specific index in a string_view. More...
 
KB_API usize string_view_index_of (const string_view view, const char c)
 Get the first index of a letter in a string_view. More...
 
KB_API string_view string_view_after_next_char (const string_view view, const char c)
 Create a new string_view of an existing string_view after a certain character. More...
 
KB_API string_view string_view_until_next_char (const string_view view, const char c)
 Create a new string_view of an existing string_view until a certain character. More...
 
KB_API string_view string_view_trim (const string_view view)
 Create a new string_view which does not start or end in whitespace. More...
 
KB_API b8 string_view_split (const string_view view, const char c, array_of(string_view) *arr)
 Split a single string_view into multiple based on a separator. More...
 

Detailed Description

Custom library for interactions with strings using string views.

Definition in file string.h.

Typedef Documentation

◆ string

typedef struct string string

Structure for a string.

Indicate ownership and allows the creation of string_view.

◆ string_view

typedef struct string_view string_view

Non owning views on actual strings.

Serves as a view on string data without having to copy.

Function Documentation

◆ string_clone()

KB_API string string_clone ( const string  str,
allocator alloc 
)

Clone a complete string.

Parameters
strthe string to clone
allocthe allocator to use for allocating the new string's data
Returns
on success the returned string will contain a valid pointer to data or 0 on failure

Definition at line 113 of file string.c.

Here is the call graph for this function:

◆ string_destroy()

KB_API void string_destroy ( string str)

Free data of the string.

Parameters
strthe string to destroy

Definition at line 101 of file string.c.

Here is the call graph for this function:

◆ string_equal()

KB_API b8 string_equal ( const string  lhs,
const string  rhs 
)

Check if the contents of two strings are equal.

If the length does not match they are automatically not equal.

Parameters
lhsthe left hand side of the comparison
rhsthe right hand side of the comparison
Returns
true if they have the same content and length, false otherwise

Definition at line 118 of file string.c.

Here is the call graph for this function:

◆ string_from_raw()

KB_API string string_from_raw ( const char *  raw)

Construct a string from a raw string.

Will call strlen to determine the length of the data. So the caller must ensure the string-data is NULL-terminated.

See also
string_from_raw_n
Parameters
rawthe raw string
Returns
a string which accesses the raw string's data

Definition at line 13 of file string.c.

Here is the call graph for this function:

◆ string_from_raw_n()

KB_API string string_from_raw_n ( const char *  raw,
usize  n 
)

Construct a string from n chars of raw character data.

The resulting string will not own the data so it does not try to clean it. The caller must ensure that the provided n does not exceed the character data buffer.

Note
The resulting string does not require explicit destruction as it does not own data.
Parameters
rawpointer to the beginning of the character data
nthe number of bytes to expose in the string
Returns
a string which accesses the raw data up to n chars

Definition at line 27 of file string.c.

◆ string_from_view()

KB_API string string_from_view ( const string_view  view,
allocator alloc 
)

Creates a new string from a string view.

Will allocate a new owning string and copy the data from the string_view.

Parameters
viewthe string view to copy the data from
allocthe allocator to use for the new string's data
Returns
on success the returned string will contain a valid pointer to data or 0 on failure

Definition at line 61 of file string.c.

Here is the call graph for this function:

◆ string_from_views()

KB_API string string_from_views ( const array_of(string_view views,
allocator alloc 
)

Creates a new single string based on all provided views.

Will allocate a new owning string and copy all the data from the string views.

Parameters
viewsan array of views to join
allocthe allocator to use for allocating the new string's data
Returns
on success the returned string will contain a valid pointer to data or 0 on failure

Definition at line 77 of file string.c.

Here is the call graph for this function:

◆ string_index_of()

KB_API usize string_index_of ( const string  str,
const char  c 
)

Get the first index of a letter in a string.

Parameters
strthe string to search through
cthe character to look for
Returns
the index of the first occurrance of the letter inside the string, USIZE_MAX otherwise

Definition at line 111 of file string.c.

Here is the call graph for this function:

◆ string_is_valid()

KB_API b8 string_is_valid ( const string  str)

Check if string is valid.

Parameters
strthe string to check for validity
Returns
only returns true if data is a valid pointer and length is > 0, false otherwise

Definition at line 109 of file string.c.

Here is the call graph for this function:

◆ string_join_views()

KB_API string string_join_views ( const array_of(string_view views,
const char  sep,
allocator alloc 
)

Creates a new single string based on all provided views separated by a separator.

Will allocate a new owning string and copy all the data from the string views.

Parameters
viewsan array of views to join
septhe separator to place between the views' data
allocthe allocator to use for allocating the new string's data
Returns
on success the returned string will contain a valid pointer to data or 0 on failure

Definition at line 35 of file string.c.

Here is the call graph for this function:

◆ string_n_equal()

KB_API b8 string_n_equal ( const string  lhs,
const string  rhs,
usize  n 
)

Check if the contents of two strings are equal.

If one of the strings does not have at least the requested length they are automatically not equal.

Parameters
lhsthe left hand side of the comparison
rhsthe right hand side of the comparison
nthe length up to which should be compared
Returns
true if they have the same content until index n - 1, false otherwise

Definition at line 122 of file string.c.

Here is the call graph for this function:

◆ string_view_after_next_char()

KB_API string_view string_view_after_next_char ( const string_view  view,
const char  c 
)

Create a new string_view of an existing string_view after a certain character.

The resulting string_view starts after the first occurance of the specified letter and have the remaining length to the end of the base string_view.

Parameters
viewthe view to base the new view on
cthe character to start after
Returns
a new string_view which is valid if the base view is valid and the character was found

Definition at line 201 of file string.c.

Here is the call graph for this function:

◆ string_view_at_offset()

KB_API string_view string_view_at_offset ( const string_view  view,
usize  offset 
)

Create a new string_view of an existing string_view.

The resulting string_view will be at the specified offset and have the remaining length to the end of the base string_view.

Parameters
viewthe view to base the new view on
offsetthe offset into the base string view
Returns
a new string_view which is valid if the base view is valid and the offset does not exceed the length

Definition at line 165 of file string.c.

◆ string_view_char_at()

KB_API char string_view_char_at ( const string_view  view,
usize  index 
)

Get the letter at an specific index in a string_view.

Parameters
viewthe string_view to index into
indexthe index to look at
Returns
the character at the index if valid, -1 otherwise

Definition at line 183 of file string.c.

◆ string_view_data()

KB_API const char* string_view_data ( const string_view  view)

Get raw pointer to beginning of the string_view data.

Returns pointer into the underlying string at the offset of the view.

Parameters
viewthe view to get the data from
Returns
the pointer into the string at the offset or KB_NULL if the view is not valid

Definition at line 135 of file string.c.

◆ string_view_equal()

KB_API b8 string_view_equal ( const string_view  lhs,
const string_view  rhs 
)

Check if the contents of two string views are equal.

If the length does not match they are automatically not equal.

Parameters
lhsthe left hand side of the comparison
rhsthe right hand side of the comparison
Returns
true if they have the same content and length, false otherwise

Definition at line 137 of file string.c.

◆ string_view_from_string()

KB_API string_view string_view_from_string ( const string  str)

Create a new string view of a whole string.

The resulting string_view will be at offset 0 and have the same length as the string it is defined on.

Parameters
strthe string to define the view on
Returns
a new string_view which is valid if the string holds valid data

Definition at line 126 of file string.c.

◆ string_view_index_of()

KB_API usize string_view_index_of ( const string_view  view,
const char  c 
)

Get the first index of a letter in a string_view.

Parameters
viewthe string_view to search through
cthe character to look for
Returns
the index of the first occurrance of the letter inside the string_view if found, USIZE_MAX otherwise

Definition at line 190 of file string.c.

◆ string_view_n_equal()

KB_API b8 string_view_n_equal ( const string_view  lhs,
const string_view  rhs,
usize  n 
)

Check if the contents of two string views are equal.

If one of the string views does not have at least the requested length they are automatically not equal.

Parameters
lhsthe left hand side of the comparison
rhsthe right hand side of the comparison
nthe length up to which should be compared
Returns
true if they have the same content until index n - 1, false otherwise

Definition at line 149 of file string.c.

◆ string_view_split()

KB_API b8 string_view_split ( const string_view  view,
const char  c,
array_of(string_view) *  arr 
)

Split a single string_view into multiple based on a separator.

The resulting string_views will be added to the array.

Parameters
viewthe string_view to split
cthe character to split on
arrthe array to add the string_views to
Returns
true if split is successful, false otherwise

Definition at line 241 of file string.c.

Here is the call graph for this function:

◆ string_view_subview()

KB_API string_view string_view_subview ( const string_view  view,
usize  start,
usize  length 
)

Create a new string_view of an existing string_view.

The resulting string_view will be at the specified offset and have the specified length.

Parameters
viewthe view to base the new view on
startthe offset into the base string view
lengththe desired length of the new view
Returns
a new string_view which is valid if the base view is valid and start + length does not exceed the base's length

Definition at line 174 of file string.c.

◆ string_view_trim()

KB_API string_view string_view_trim ( const string_view  view)

Create a new string_view which does not start or end in whitespace.

See also
character_is_whitespace
Parameters
viewthe base string view to trim
Returns
a new string_view on the trimmed data

Definition at line 221 of file string.c.

◆ string_view_until_next_char()

KB_API string_view string_view_until_next_char ( const string_view  view,
const char  c 
)

Create a new string_view of an existing string_view until a certain character.

The resulting string_view starts at the beginning of the base view and ends before the first occurance of the specified character.

Parameters
viewthe view to base the new view on
cthe character to end before
Returns
a new string_view which is valid if the base view is valid and the character was found

Definition at line 211 of file string.c.

Here is the call graph for this function: