Kohi Game Engine
kstring.h File Reference

This file contains a basic C string handling library. More...

#include "defines.h"
#include "math/math_types.h"

Go to the source code of this file.

Data Structures

struct  kstring
 A kstring is a managed string for higher-level logic to use. It is safer and, in some cases quicker than a typical cstring because it maintains length/allocation information and doesn't have to use strlen on most of its internal operations. More...
 

Typedefs

typedef struct kstring kstring
 A kstring is a managed string for higher-level logic to use. It is safer and, in some cases quicker than a typical cstring because it maintains length/allocation information and doesn't have to use strlen on most of its internal operations. More...
 

Functions

KAPI u64 string_length (const char *str)
 Gets the number of bytes of the given string, minus the null terminator. More...
 
KAPI u32 string_utf8_length (const char *str)
 Gets the length of a string in UTF-8 (potentially multibyte) characters, minus the null terminator. More...
 
KAPI u64 string_nlength (const char *str, u32 max_len)
 Gets the number of bytes of the given string, minus the null terminator, but at most max_len. This function only ever looks at the bytes pointed to in str up until, but never beyond, max_len - 1. More...
 
KAPI u32 string_utf8_nlength (const char *str, u32 max_len)
 Gets the number of characters (multibyte = 1 character) of a string in UTF-8 (potentially multibyte) characters, minus the null terminator, but at most max_len. This function only ever looks at the characters pointed to in str up until, but never beyond, max_len - 1. More...
 
KAPI b8 bytes_to_codepoint (const char *bytes, u32 offset, i32 *out_codepoint, u8 *out_advance)
 Obtains bytes needed from the byte array to form a UTF-8 codepoint, also providing how many bytes the current character is. More...
 
KAPI b8 char_is_whitespace (char c)
 Indicates if the provided character is considered whitespace. More...
 
KAPI b8 codepoint_is_whitespace (i32 codepoint)
 Indicates if the provided codepoint is considered whitespace. More...
 
KAPI char * string_duplicate (const char *str)
 Duplicates the provided string. Note that this allocates new memory, which should be freed by the caller. More...
 
KAPI void string_free (const char *str)
 Frees the memory of the given string. More...
 
KAPI i64 kstr_ncmp (const char *str0, const char *str1, u32 max_len)
 
KAPI i64 kstr_ncmpi (const char *str0, const char *str1, u32 max_len)
 
KAPI b8 strings_equal (const char *str0, const char *str1)
 Case-sensitive string comparison. More...
 
KAPI b8 strings_equali (const char *str0, const char *str1)
 Case-insensitive string comparison. More...
 
KAPI b8 strings_nequal (const char *str0, const char *str1, u32 max_len)
 Case-sensitive string comparison, where comparison stops at max_len. More...
 
KAPI b8 strings_nequali (const char *str0, const char *str1, u32 max_len)
 Case-insensitive string comparison, where comparison stops at max_len. More...
 
KAPI char * string_format (const char *format,...)
 Performs string formatting against the given format string and parameters. NOTE: that this performs a dynamic allocation and should be freed by the caller. More...
 
KAPI char * string_format_v (const char *format, void *va_list)
 Performs variadic string formatting against the given format string and va_list. NOTE: that this performs a dynamic allocation and should be freed by the caller. More...
 
KAPI i32 string_format_unsafe (char *dest, const char *format,...)
 Performs string formatting to dest given format string and parameters. More...
 
KAPI i32 string_format_v_unsafe (char *dest, const char *format, void *va_list)
 Performs variadic string formatting to dest given format string and va_list. More...
 
KAPI char * string_empty (char *str)
 Empties the provided string by setting the first character to 0. More...
 
KAPI char * string_copy (char *dest, const char *source)
 Copies the string in source to dest. Does not perform any allocations. More...
 
KAPI char * string_ncopy (char *dest, const char *source, u32 max_len)
 Copies the bytes in the source buffer into the dest buffer up to the given length. Does not perform any allocations. Any remaining length after a 0 terminator will be zero-padded unless max_len is U32_MAX. More...
 
KAPI char * string_trim (char *str)
 Performs an in-place trim of the provided string. This removes all whitespace from both ends of the string. More...
 
KAPI void string_mid (char *dest, const char *source, i32 start, i32 length)
 Gets a substring of the source string between start and length or to the end of the string. If length is negative, goes to the end of the string. More...
 
KAPI i32 string_index_of (const char *str, char c)
 Returns the index of the first occurance of c in str; otherwise -1. More...
 
KAPI i32 string_last_index_of (const char *str, char c)
 Returns the index of the last occurance of c in str; otherwise -1. More...
 
KAPI i32 string_index_of_str (const char *str_0, const char *str_1)
 Returns the index of the first occurance of str_1 in str_0; otherwise -1. More...
 
KAPI b8 string_starts_with (const char *str_0, const char *str_1)
 Indicates if str_0 starts with str_1. Case-sensitive. More...
 
KAPI b8 string_starts_withi (const char *str_0, const char *str_1)
 Indicates if str_0 starts with str_1. Case-insensitive. More...
 
KAPI void string_insert_char_at (char *dest, const char *src, u32 pos, char c)
 
KAPI void string_insert_str_at (char *dest, const char *src, u32 pos, const char *str)
 
KAPI void string_remove_at (char *dest, const char *src, u32 pos, u32 length)
 
KAPI b8 string_to_mat4 (const char *str, mat4 *out_mat)
 Attempts to parse a 4x4 matrix from the provided string. More...
 
KAPI const char * mat4_to_string (mat4 m)
 Creates a string representation of the provided matrix. NOTE: string is dynamically allocated, so the caller should free it. More...
 
KAPI b8 string_to_vec4 (const char *str, vec4 *out_vector)
 Attempts to parse a vector from the provided string. More...
 
KAPI const char * vec4_to_string (vec4 v)
 Creates a string representation of the provided vector. NOTE: string is dynamically allocated, so the caller should free it. More...
 
KAPI b8 string_to_vec3 (const char *str, vec3 *out_vector)
 Attempts to parse a vector from the provided string. More...
 
KAPI const char * vec3_to_string (vec3 v)
 Creates a string representation of the provided vector. NOTE: string is dynamically allocated, so the caller should free it. More...
 
KAPI b8 string_to_vec2 (const char *str, vec2 *out_vector)
 Attempts to parse a vector from the provided string. More...
 
KAPI const char * vec2_to_string (vec2 v)
 Creates a string representation of the provided vector. NOTE: string is dynamically allocated, so the caller should free it. More...
 
KAPI b8 string_to_f32 (const char *str, f32 *f)
 Attempts to parse a 32-bit floating-point number from the provided string. More...
 
KAPI const char * f32_to_string (f32 f)
 Creates a string representation of the provided float. NOTE: string is dynamically allocated, so the caller should free it. More...
 
KAPI b8 string_to_f64 (const char *str, f64 *f)
 Attempts to parse a 64-bit floating-point number from the provided string. More...
 
KAPI const char * f64_to_string (f64 f)
 Creates a string representation of the provided 64-bit float. NOTE: string is dynamically allocated, so the caller should free it. More...
 
KAPI b8 string_to_i8 (const char *str, i8 *i)
 Attempts to parse an 8-bit signed integer from the provided string. More...
 
KAPI const char * i8_to_string (i8 i)
 Creates a string representation of the provided integer. NOTE: string is dynamically allocated, so the caller should free it. More...
 
KAPI b8 string_to_i16 (const char *str, i16 *i)
 Attempts to parse a 16-bit signed integer from the provided string. More...
 
KAPI const char * i16_to_string (i16 i)
 Creates a string representation of the provided integer. NOTE: string is dynamically allocated, so the caller should free it. More...
 
KAPI b8 string_to_i32 (const char *str, i32 *i)
 Attempts to parse a 32-bit signed integer from the provided string. More...
 
KAPI const char * i32_to_string (i32 i)
 Creates a string representation of the provided integer. NOTE: string is dynamically allocated, so the caller should free it. More...
 
KAPI b8 string_to_i64 (const char *str, i64 *i)
 Attempts to parse a 64-bit signed integer from the provided string. More...
 
KAPI const char * i64_to_string (i64 i)
 Creates a string representation of the provided integer. NOTE: string is dynamically allocated, so the caller should free it. More...
 
KAPI b8 string_to_u8 (const char *str, u8 *u)
 Attempts to parse an 8-bit unsigned integer from the provided string. More...
 
KAPI const char * u8_to_string (u8 u)
 Creates a string representation of the provided integer. NOTE: string is dynamically allocated, so the caller should free it. More...
 
KAPI b8 string_to_u16 (const char *str, u16 *u)
 Attempts to parse a 16-bit unsigned integer from the provided string. More...
 
KAPI const char * u16_to_string (u16 u)
 Creates a string representation of the provided integer. NOTE: string is dynamically allocated, so the caller should free it. More...
 
KAPI b8 string_to_u32 (const char *str, u32 *u)
 Attempts to parse a 32-bit unsigned integer from the provided string. More...
 
KAPI const char * u32_to_string (u32 u)
 Creates a string representation of the provided integer. NOTE: string is dynamically allocated, so the caller should free it. More...
 
KAPI b8 string_to_u64 (const char *str, u64 *u)
 Attempts to parse a 64-bit unsigned integer from the provided string. More...
 
KAPI const char * u64_to_string (u64 u)
 Creates a string representation of the provided integer. NOTE: string is dynamically allocated, so the caller should free it. More...
 
KAPI b8 string_to_bool (const char *str, b8 *b)
 Attempts to parse a boolean from the provided string. "true" or "1" are considered true; anything else is false. More...
 
KAPI const char * bool_to_string (b8 b)
 Creates a string representation of the provided boolean, i.e. "false" for false/0 and "true" for everything else. NOTE: string is dynamically allocated, so the caller should free it. More...
 
KAPI u32 string_split (const char *str, char delimiter, char ***str_darray, b8 trim_entries, b8 include_empty)
 Splits the given string by the delimiter provided and stores in the provided darray. Optionally trims each entry. NOTE: A string allocation occurs for each entry, and MUST be freed by the caller. More...
 
KAPI void string_cleanup_split_darray (char **str_darray)
 Cleans up string allocations in str_darray, but does not free the darray itself. More...
 
KAPI void string_cleanup_array (const char **str_array, u32 length)
 Cleans up string allocations in str_array and frees the array itself. More...
 
KAPI u32 string_nsplit (const char *str, char delimiter, u32 max_count, char **str_array, b8 trim_entries, b8 include_empty)
 Splits the given string by the delimiter provided and stores in the provided fixed-size array. Optionally trims each entry. NOTE: A string allocation occurs for each entry, and MUST be freed by the caller. More...
 
KAPI void string_cleanup_split_array (char **str_array, u32 max_count)
 Cleans up string allocations in the fixed-size str_array, but does not free the array itself. More...
 
KAPI void string_append_string (char *dest, const char *source, const char *append)
 
KAPI void string_append_int (char *dest, const char *source, i64 i)
 Appends the supplied integer to source and outputs to dest. More...
 
KAPI void string_append_float (char *dest, const char *source, f32 f)
 Appends the supplied float to source and outputs to dest. More...
 
KAPI void string_append_bool (char *dest, const char *source, b8 b)
 Appends the supplied boolean (as either "true" or "false") to source and outputs to dest. More...
 
KAPI void string_append_char (char *dest, const char *source, char c)
 Appends the supplied character to source and outputs to dest. More...
 
KAPI char * string_join (const char **strings, u32 count, char delimiter)
 Joins the array of strings given with the provided delimiter. The delimiter is not used after the final entry. More...
 
KAPI const char * string_directory_from_path (const char *path)
 Extracts the directory from a full file path. More...
 
KAPI const char * string_filename_from_path (const char *path)
 Extracts the filename (including file extension) from a full file path. More...
 
KAPI const char * string_filename_no_extension_from_path (const char *path)
 Extracts the filename (excluding file extension) from a full file path. More...
 
KAPI const char * string_extension_from_path (const char *path, b8 include_dot)
 Attempts to get the file extension from the given path. Allocates a new string which should be freed. More...
 
KAPI b8 string_parse_array_length (const char *str, u32 *out_length)
 Attempts to extract an array length from a given string. Ex: a string of sampler2D[4] will return True and set out_length to 4. More...
 
KAPI b8 string_line_get (const char *source_str, u16 max_line_length, u32 start_from, char **out_buffer, u32 *out_line_length, u8 *out_addl_advance)
 
KAPI b8 codepoint_is_lower (i32 codepoint)
 
KAPI b8 codepoint_is_upper (i32 codepoint)
 
KAPI b8 codepoint_is_alpha (i32 codepoint)
 
KAPI b8 codepoint_is_numeric (i32 codepoint)
 
KAPI b8 codepoint_is_space (i32 codepoint)
 
KAPI void string_to_lower (char *str)
 
KAPI void string_to_upper (char *str)
 
KAPI void kstring_create (kstring *out_string)
 
KAPI void kstring_from_cstring (const char *source, kstring *out_string)
 
KAPI void kstring_destroy (kstring *string)
 
KAPI u32 kstring_length (const kstring *string)
 
KAPI u32 kstring_utf8_length (const kstring *string)
 
KAPI void kstring_append_str (kstring *string, const char *s)
 
KAPI void kstring_append_kstring (kstring *string, const kstring *other)
 

Detailed Description

This file contains a basic C string handling library.

Author
Travis Vroman (travi.nosp@m.s@ko.nosp@m.hieng.nosp@m.ine..nosp@m.com)
Version
1.0
Date
2022-01-10

Typedef Documentation

◆ kstring

typedef struct kstring kstring

A kstring is a managed string for higher-level logic to use. It is safer and, in some cases quicker than a typical cstring because it maintains length/allocation information and doesn't have to use strlen on most of its internal operations.

Function Documentation

◆ bool_to_string()

KAPI const char* bool_to_string ( b8  b)

Creates a string representation of the provided boolean, i.e. "false" for false/0 and "true" for everything else. NOTE: string is dynamically allocated, so the caller should free it.

Parameters
bThe boolean to create a string from.
Returns
The string representation of the provided boolean.

◆ bytes_to_codepoint()

KAPI b8 bytes_to_codepoint ( const char *  bytes,
u32  offset,
i32 out_codepoint,
u8 out_advance 
)

Obtains bytes needed from the byte array to form a UTF-8 codepoint, also providing how many bytes the current character is.

Parameters
bytesThe byte array to choose from.
offsetThe offset in bytes to start from.
out_codepointA pointer to hold the UTF-8 codepoint.
out_advanceA pointer to hold the advance, or how many bytes the codepoint takes.
Returns
True on success; otherwise false for invalid/unsupported UTF-8.

◆ char_is_whitespace()

KAPI b8 char_is_whitespace ( char  c)

Indicates if the provided character is considered whitespace.

Parameters
cThe character to examine.
Returns
True if whitespace; otherwise false.

◆ codepoint_is_alpha()

KAPI b8 codepoint_is_alpha ( i32  codepoint)

Indicates if provided codepoint is alpha-numeric. Regular ASCII and western European high-ascii characters only.

◆ codepoint_is_lower()

KAPI b8 codepoint_is_lower ( i32  codepoint)

Indicates if provided codepoint is lower-case. Regular ASCII and western European high-ascii characters only.

◆ codepoint_is_numeric()

KAPI b8 codepoint_is_numeric ( i32  codepoint)

Indicates if provided codepoint is numeric. Regular ASCII and western European high-ascii characters only.

◆ codepoint_is_space()

KAPI b8 codepoint_is_space ( i32  codepoint)

Indicates if the given codepoint is considered to be a space. Includes ' ', \f \r
\t and \v.

◆ codepoint_is_upper()

KAPI b8 codepoint_is_upper ( i32  codepoint)

Indicates if provided codepoint is upper-case. Regular ASCII and western European high-ascii characters only.

◆ codepoint_is_whitespace()

KAPI b8 codepoint_is_whitespace ( i32  codepoint)

Indicates if the provided codepoint is considered whitespace.

Parameters
codepointThe codepoint to examine.
Returns
True if whitespace; otherwise false.

◆ f32_to_string()

KAPI const char* f32_to_string ( f32  f)

Creates a string representation of the provided float. NOTE: string is dynamically allocated, so the caller should free it.

Parameters
fThe float to convert to string.
Returns
The string representation of the provided float.

◆ f64_to_string()

KAPI const char* f64_to_string ( f64  f)

Creates a string representation of the provided 64-bit float. NOTE: string is dynamically allocated, so the caller should free it.

Parameters
fThe 64-bit float to convert to string.
Returns
The string representation of the provided 64-bit float.

◆ i16_to_string()

KAPI const char* i16_to_string ( i16  i)

Creates a string representation of the provided integer. NOTE: string is dynamically allocated, so the caller should free it.

Parameters
iThe integer to create a string from.
Returns
The string representation of the provided integer.

◆ i32_to_string()

KAPI const char* i32_to_string ( i32  i)

Creates a string representation of the provided integer. NOTE: string is dynamically allocated, so the caller should free it.

Parameters
iThe integer to create a string from.
Returns
The string representation of the provided integer.

◆ i64_to_string()

KAPI const char* i64_to_string ( i64  i)

Creates a string representation of the provided integer. NOTE: string is dynamically allocated, so the caller should free it.

Parameters
iThe integer to create a string from.
Returns
The string representation of the provided integer.

◆ i8_to_string()

KAPI const char* i8_to_string ( i8  i)

Creates a string representation of the provided integer. NOTE: string is dynamically allocated, so the caller should free it.

Parameters
iThe integer to create a string from.
Returns
The string representation of the provided integer.

◆ kstr_ncmp()

KAPI i64 kstr_ncmp ( const char *  str0,
const char *  str1,
u32  max_len 
)

◆ kstr_ncmpi()

KAPI i64 kstr_ncmpi ( const char *  str0,
const char *  str1,
u32  max_len 
)

◆ kstring_append_kstring()

KAPI void kstring_append_kstring ( kstring string,
const kstring other 
)

◆ kstring_append_str()

KAPI void kstring_append_str ( kstring string,
const char *  s 
)

◆ kstring_create()

KAPI void kstring_create ( kstring out_string)

◆ kstring_destroy()

KAPI void kstring_destroy ( kstring string)

◆ kstring_from_cstring()

KAPI void kstring_from_cstring ( const char *  source,
kstring out_string 
)

◆ kstring_length()

KAPI u32 kstring_length ( const kstring string)

◆ kstring_utf8_length()

KAPI u32 kstring_utf8_length ( const kstring string)

◆ mat4_to_string()

KAPI const char* mat4_to_string ( mat4  m)

Creates a string representation of the provided matrix. NOTE: string is dynamically allocated, so the caller should free it.

Parameters
mThe matrix to convert to string.
Returns
The string representation of the matrix.

◆ string_append_bool()

KAPI void string_append_bool ( char *  dest,
const char *  source,
b8  b 
)

Appends the supplied boolean (as either "true" or "false") to source and outputs to dest.

Parameters
destThe destination for the string.
sourceThe string to be appended to.
bThe boolean to be appended.

◆ string_append_char()

KAPI void string_append_char ( char *  dest,
const char *  source,
char  c 
)

Appends the supplied character to source and outputs to dest.

Parameters
destThe destination for the string.
sourceThe string to be appended to.
cThe character to be appended.

◆ string_append_float()

KAPI void string_append_float ( char *  dest,
const char *  source,
f32  f 
)

Appends the supplied float to source and outputs to dest.

Parameters
destThe destination for the string.
sourceThe string to be appended to.
fThe float to be appended.

◆ string_append_int()

KAPI void string_append_int ( char *  dest,
const char *  source,
i64  i 
)

Appends the supplied integer to source and outputs to dest.

Parameters
destThe destination for the string.
sourceThe string to be appended to.
iThe integer to be appended.

◆ string_append_string()

KAPI void string_append_string ( char *  dest,
const char *  source,
const char *  append 
)

Appends append to source and returns a new string.

Parameters
destThe destination string.
sourceThe string to be appended to.
appendThe string to append to source.
Returns
A new string containing the concatenation of the two strings.

◆ string_cleanup_array()

KAPI void string_cleanup_array ( const char **  str_array,
u32  length 
)

Cleans up string allocations in str_array and frees the array itself.

NOTE: Not for use with darrays! Use string_cleanup_split_darray() instead or memory will be leaked.

Parameters
str_arrayThe array to be cleaned up and freed.
lengthThe number of string elements in the array.

◆ string_cleanup_split_array()

KAPI void string_cleanup_split_array ( char **  str_array,
u32  max_count 
)

Cleans up string allocations in the fixed-size str_array, but does not free the array itself.

Parameters
str_darrayThe fixed-size array to be cleaned up.
max_countThe number of entries (and thus the size) of the fixed-size array.

◆ string_cleanup_split_darray()

KAPI void string_cleanup_split_darray ( char **  str_darray)

Cleans up string allocations in str_darray, but does not free the darray itself.

Parameters
str_darrayThe darray to be cleaned up.

◆ string_copy()

KAPI char* string_copy ( char *  dest,
const char *  source 
)

Copies the string in source to dest. Does not perform any allocations.

Parameters
destThe destination string.
sourceThe source string.
Returns
A pointer to the destination string.

◆ string_directory_from_path()

KAPI const char* string_directory_from_path ( const char *  path)

Extracts the directory from a full file path.

Parameters
pathThe full path to extract from.
Returns
The the directory.

◆ string_duplicate()

KAPI char* string_duplicate ( const char *  str)

Duplicates the provided string. Note that this allocates new memory, which should be freed by the caller.

Parameters
strThe string to be duplicated.
Returns
A pointer to a newly-created character array (string).

◆ string_empty()

KAPI char* string_empty ( char *  str)

Empties the provided string by setting the first character to 0.

Parameters
strThe string to be emptied.
Returns
A pointer to str.

◆ string_extension_from_path()

KAPI const char* string_extension_from_path ( const char *  path,
b8  include_dot 
)

Attempts to get the file extension from the given path. Allocates a new string which should be freed.

NOTE: This function dynamically allocates string memory. The string should be freed by the caller.

Parameters
pathThe full path to extract from.
include_dotIndicates if the '.' should be included in the output.
Returns
The extension on success; otherwise 0.

◆ string_filename_from_path()

KAPI const char* string_filename_from_path ( const char *  path)

Extracts the filename (including file extension) from a full file path.

NOTE: This function dynamically allocates string memory. The string should be freed by the caller.

Parameters
pathThe full path to extract from.
Returns
The filename with extension.

◆ string_filename_no_extension_from_path()

KAPI const char* string_filename_no_extension_from_path ( const char *  path)

Extracts the filename (excluding file extension) from a full file path.

NOTE: This function dynamically allocates string memory. The string should be freed by the caller.

Parameters
pathThe full path to extract from.
Returns
The filename without extension.

◆ string_format()

KAPI char* string_format ( const char *  format,
  ... 
)

Performs string formatting against the given format string and parameters. NOTE: that this performs a dynamic allocation and should be freed by the caller.

Parameters
formatThe format string to use for the operation
...The format arguments.
Returns
The newly-formatted string (dynamically allocated).

◆ string_format_unsafe()

KAPI i32 string_format_unsafe ( char *  dest,
const char *  format,
  ... 
)

Performs string formatting to dest given format string and parameters.

Deprecated:
Note
This version of the function is unsafe. Use string_format() instead.
Parameters
destThe destination for the formatted string.
formatThe format string to use for the operation
...The format arguments.
Returns
The length of the newly-formatted string.

◆ string_format_v()

KAPI char* string_format_v ( const char *  format,
void *  va_list 
)

Performs variadic string formatting against the given format string and va_list. NOTE: that this performs a dynamic allocation and should be freed by the caller.

Parameters
formatThe string to be formatted.
va_listThe variadic argument list.
Returns
The newly-formatted string (dynamically allocated).

◆ string_format_v_unsafe()

KAPI i32 string_format_v_unsafe ( char *  dest,
const char *  format,
void *  va_list 
)

Performs variadic string formatting to dest given format string and va_list.

Deprecated:
Note
This version of the function is unsafe. Use string_format() instead.
Parameters
destThe destination for the formatted string.
formatThe string to be formatted.
va_listThe variadic argument list.
Returns
The size of the data written.

◆ string_free()

KAPI void string_free ( const char *  str)

Frees the memory of the given string.

Parameters
strThe string to be freed.

◆ string_index_of()

KAPI i32 string_index_of ( const char *  str,
char  c 
)

Returns the index of the first occurance of c in str; otherwise -1.

Parameters
strThe string to be scanned.
cThe character to search for.
Returns
The index of the first occurance of c; otherwise -1 if not found.

◆ string_index_of_str()

KAPI i32 string_index_of_str ( const char *  str_0,
const char *  str_1 
)

Returns the index of the first occurance of str_1 in str_0; otherwise -1.

Parameters
str_0The string to be scanned.
str_1The substring to search for.
Returns
The index of the first occurance of str_1; otherwise -1 if not found.

◆ string_insert_char_at()

KAPI void string_insert_char_at ( char *  dest,
const char *  src,
u32  pos,
char  c 
)

◆ string_insert_str_at()

KAPI void string_insert_str_at ( char *  dest,
const char *  src,
u32  pos,
const char *  str 
)

◆ string_join()

KAPI char* string_join ( const char **  strings,
u32  count,
char  delimiter 
)

Joins the array of strings given with the provided delimiter. The delimiter is not used after the final entry.

NOTE: This function dynamically allocates string memory. The string should be freed by the caller.

Parameters
stringsThe array of strings to be joined.
countThe number of strings to be joined.
delimiterThe delimiter character to join with.
Returns
The joined string. Should be freed by the caller.

◆ string_last_index_of()

KAPI i32 string_last_index_of ( const char *  str,
char  c 
)

Returns the index of the last occurance of c in str; otherwise -1.

Parameters
strThe string to be scanned.
cThe character to search for.
Returns
The index of the last occurance of c; otherwise -1 if not found.

◆ string_length()

KAPI u64 string_length ( const char *  str)

Gets the number of bytes of the given string, minus the null terminator.

NOTE: For strings without a null terminator, use string_nlength instead.

Parameters
strThe string whose length to obtain.
Returns
The length of the string.

◆ string_line_get()

KAPI b8 string_line_get ( const char *  source_str,
u16  max_line_length,
u32  start_from,
char **  out_buffer,
u32 out_line_length,
u8 out_addl_advance 
)

◆ string_mid()

KAPI void string_mid ( char *  dest,
const char *  source,
i32  start,
i32  length 
)

Gets a substring of the source string between start and length or to the end of the string. If length is negative, goes to the end of the string.

Done by placing zeroes in the string at relevant points.

Parameters
strThe string to be trimmed.

◆ string_ncopy()

KAPI char* string_ncopy ( char *  dest,
const char *  source,
u32  max_len 
)

Copies the bytes in the source buffer into the dest buffer up to the given length. Does not perform any allocations. Any remaining length after a 0 terminator will be zero-padded unless max_len is U32_MAX.

Parameters
destA pointer to the destination buffer. Must be at least max_len large.
sourceA constant pointer to the source buffer.
lengthThe maximum number of bytes to be copied.
Returns
A pointer to the destination string.

◆ string_nlength()

KAPI u64 string_nlength ( const char *  str,
u32  max_len 
)

Gets the number of bytes of the given string, minus the null terminator, but at most max_len. This function only ever looks at the bytes pointed to in str up until, but never beyond, max_len - 1.

Parameters
strThe string whose length to obtain.
max_lenThe maximum number of bytes to examine in the string.
Returns
The length of the string, at most max_len.

◆ string_nsplit()

KAPI u32 string_nsplit ( const char *  str,
char  delimiter,
u32  max_count,
char **  str_array,
b8  trim_entries,
b8  include_empty 
)

Splits the given string by the delimiter provided and stores in the provided fixed-size array. Optionally trims each entry. NOTE: A string allocation occurs for each entry, and MUST be freed by the caller.

Parameters
strThe string to be split.
delimiterThe character to split by.
max_countThe maximum number of entries to split.
str_darrayA fixed-size array of char arrays. Must be large enough to hold max_count entries.
trim_entriesTrims each entry if true.
include_emptyIndicates if empty entries should be included.
Returns
The number of entries yielded by the split operation.

◆ string_parse_array_length()

KAPI b8 string_parse_array_length ( const char *  str,
u32 out_length 
)

Attempts to extract an array length from a given string. Ex: a string of sampler2D[4] will return True and set out_length to 4.

Parameters
strThe string to examine.
out_lengthA pointer to hold the length, if extracted successfully.
Returns
True if an array length was found and parsed; otherwise false.

◆ string_remove_at()

KAPI void string_remove_at ( char *  dest,
const char *  src,
u32  pos,
u32  length 
)

◆ string_split()

KAPI u32 string_split ( const char *  str,
char  delimiter,
char ***  str_darray,
b8  trim_entries,
b8  include_empty 
)

Splits the given string by the delimiter provided and stores in the provided darray. Optionally trims each entry. NOTE: A string allocation occurs for each entry, and MUST be freed by the caller.

Parameters
strThe string to be split.
delimiterThe character to split by.
str_darrayA pointer to a darray of char arrays to hold the entries. NOTE: must be a darray.
trim_entriesTrims each entry if true.
include_emptyIndicates if empty entries should be included.
Returns
The number of entries yielded by the split operation.

◆ string_starts_with()

KAPI b8 string_starts_with ( const char *  str_0,
const char *  str_1 
)

Indicates if str_0 starts with str_1. Case-sensitive.

Parameters
str_0The string to be scanned.
str_1The substring to search for.
Returns
True if str_0 starts with str_1; otherwise false.

◆ string_starts_withi()

KAPI b8 string_starts_withi ( const char *  str_0,
const char *  str_1 
)

Indicates if str_0 starts with str_1. Case-insensitive.

Parameters
str_0The string to be scanned.
str_1The substring to search for.
Returns
True if str_0 starts with str_1; otherwise false.

◆ string_to_bool()

KAPI b8 string_to_bool ( const char *  str,
b8 b 
)

Attempts to parse a boolean from the provided string. "true" or "1" are considered true; anything else is false.

Parameters
strThe string to parse from. "true" or "1" are considered true; anything else is false.
bA pointer to the boolean to write to.
Returns
True if parsed successfully; otherwise false.

◆ string_to_f32()

KAPI b8 string_to_f32 ( const char *  str,
f32 f 
)

Attempts to parse a 32-bit floating-point number from the provided string.

Parameters
strThe string to parse from. Should not be postfixed with 'f'.
fA pointer to the float to write to.
Returns
True if parsed successfully; otherwise false.

◆ string_to_f64()

KAPI b8 string_to_f64 ( const char *  str,
f64 f 
)

Attempts to parse a 64-bit floating-point number from the provided string.

Parameters
strThe string to parse from.
fA pointer to the float to write to.
Returns
True if parsed successfully; otherwise false.

◆ string_to_i16()

KAPI b8 string_to_i16 ( const char *  str,
i16 i 
)

Attempts to parse a 16-bit signed integer from the provided string.

Parameters
strThe string to parse from.
iA pointer to the int to write to.
Returns
True if parsed successfully; otherwise false.

◆ string_to_i32()

KAPI b8 string_to_i32 ( const char *  str,
i32 i 
)

Attempts to parse a 32-bit signed integer from the provided string.

Parameters
strThe string to parse from.
iA pointer to the int to write to.
Returns
True if parsed successfully; otherwise false.

◆ string_to_i64()

KAPI b8 string_to_i64 ( const char *  str,
i64 i 
)

Attempts to parse a 64-bit signed integer from the provided string.

Parameters
strThe string to parse from.
iA pointer to the int to write to.
Returns
True if parsed successfully; otherwise false.

◆ string_to_i8()

KAPI b8 string_to_i8 ( const char *  str,
i8 i 
)

Attempts to parse an 8-bit signed integer from the provided string.

Parameters
strThe string to parse from.
iA pointer to the int to write to.
Returns
True if parsed successfully; otherwise false.

◆ string_to_lower()

KAPI void string_to_lower ( char *  str)

Converts string in-place to uppercase. Regular ASCII and western European high-ascii characters only.

◆ string_to_mat4()

KAPI b8 string_to_mat4 ( const char *  str,
mat4 out_mat 
)

Attempts to parse a 4x4 matrix from the provided string.

Parameters
strThe string to parse from. Should be space delimited. (i.e "1.0 1.0 ... 1.0")
out_matA pointer to the matrix to write to.
Returns
True if parsed successfully; otherwise false.

◆ string_to_u16()

KAPI b8 string_to_u16 ( const char *  str,
u16 u 
)

Attempts to parse a 16-bit unsigned integer from the provided string.

Parameters
strThe string to parse from.
uA pointer to the int to write to.
Returns
True if parsed successfully; otherwise false.

◆ string_to_u32()

KAPI b8 string_to_u32 ( const char *  str,
u32 u 
)

Attempts to parse a 32-bit unsigned integer from the provided string.

Parameters
strThe string to parse from.
uA pointer to the int to write to.
Returns
True if parsed successfully; otherwise false.

◆ string_to_u64()

KAPI b8 string_to_u64 ( const char *  str,
u64 u 
)

Attempts to parse a 64-bit unsigned integer from the provided string.

Parameters
strThe string to parse from.
uA pointer to the int to write to.
Returns
True if parsed successfully; otherwise false.

◆ string_to_u8()

KAPI b8 string_to_u8 ( const char *  str,
u8 u 
)

Attempts to parse an 8-bit unsigned integer from the provided string.

Parameters
strThe string to parse from.
uA pointer to the int to write to.
Returns
True if parsed successfully; otherwise false.

◆ string_to_upper()

KAPI void string_to_upper ( char *  str)

Converts string in-place to uppercase. Regular ASCII and western European high-ascii characters only.

◆ string_to_vec2()

KAPI b8 string_to_vec2 ( const char *  str,
vec2 out_vector 
)

Attempts to parse a vector from the provided string.

Parameters
strThe string to parse from. Should be space-delimited. (i.e. "1.0 2.0")
out_vectorA pointer to the vector to write to.
Returns
True if parsed successfully; otherwise false.

◆ string_to_vec3()

KAPI b8 string_to_vec3 ( const char *  str,
vec3 out_vector 
)

Attempts to parse a vector from the provided string.

Parameters
strThe string to parse from. Should be space-delimited. (i.e. "1.0 2.0 3.0")
out_vectorA pointer to the vector to write to.
Returns
True if parsed successfully; otherwise false.

◆ string_to_vec4()

KAPI b8 string_to_vec4 ( const char *  str,
vec4 out_vector 
)

Attempts to parse a vector from the provided string.

Parameters
strThe string to parse from. Should be space-delimited. (i.e. "1.0 2.0 3.0 4.0")
out_vectorA pointer to the vector to write to.
Returns
True if parsed successfully; otherwise false.

◆ string_trim()

KAPI char* string_trim ( char *  str)

Performs an in-place trim of the provided string. This removes all whitespace from both ends of the string.

Done by placing zeroes in the string at relevant points.

Parameters
strThe string to be trimmed.
Returns
A pointer to the trimmed string.

◆ string_utf8_length()

KAPI u32 string_utf8_length ( const char *  str)

Gets the length of a string in UTF-8 (potentially multibyte) characters, minus the null terminator.

NOTE: For strings without a null terminator, use string_utf8_nlength instead.

Parameters
strThe string to examine.
Returns
The UTF-8 length of the string.

◆ string_utf8_nlength()

KAPI u32 string_utf8_nlength ( const char *  str,
u32  max_len 
)

Gets the number of characters (multibyte = 1 character) of a string in UTF-8 (potentially multibyte) characters, minus the null terminator, but at most max_len. This function only ever looks at the characters pointed to in str up until, but never beyond, max_len - 1.

Parameters
strThe string to examine.
max_lenThe maximum number of characters to examine in the string.
Returns
The number of multibyte characters in the string, at most max_len.

◆ strings_equal()

KAPI b8 strings_equal ( const char *  str0,
const char *  str1 
)

Case-sensitive string comparison.

Parameters
str0The first string to be compared.
str1The second string to be compared.
Returns
True if the same, otherwise false.

◆ strings_equali()

KAPI b8 strings_equali ( const char *  str0,
const char *  str1 
)

Case-insensitive string comparison.

Parameters
str0The first string to be compared.
str1The second string to be compared.
Returns
True if the same, otherwise false.

◆ strings_nequal()

KAPI b8 strings_nequal ( const char *  str0,
const char *  str1,
u32  max_len 
)

Case-sensitive string comparison, where comparison stops at max_len.

Parameters
str0The first string to be compared.
str1The second string to be compared.
max_lenThe maximum number of bytes to be compared.
Returns
True if the same, otherwise false.

◆ strings_nequali()

KAPI b8 strings_nequali ( const char *  str0,
const char *  str1,
u32  max_len 
)

Case-insensitive string comparison, where comparison stops at max_len.

Parameters
str0The first string to be compared.
str1The second string to be compared.
max_lenThe maximum number of bytes to be compared.
Returns
True if the same, otherwise false.

◆ u16_to_string()

KAPI const char* u16_to_string ( u16  u)

Creates a string representation of the provided integer. NOTE: string is dynamically allocated, so the caller should free it.

Parameters
uThe integer to create a string from.
Returns
The string representation of the provided integer.

◆ u32_to_string()

KAPI const char* u32_to_string ( u32  u)

Creates a string representation of the provided integer. NOTE: string is dynamically allocated, so the caller should free it.

Parameters
uThe integer to create a string from.
Returns
The string representation of the provided integer.

◆ u64_to_string()

KAPI const char* u64_to_string ( u64  u)

Creates a string representation of the provided integer. NOTE: string is dynamically allocated, so the caller should free it.

Parameters
uThe integer to create a string from.
Returns
The string representation of the provided integer.

◆ u8_to_string()

KAPI const char* u8_to_string ( u8  u)

Creates a string representation of the provided integer. NOTE: string is dynamically allocated, so the caller should free it.

Parameters
uThe integer to create a string from.
Returns
The string representation of the provided integer.

◆ vec2_to_string()

KAPI const char* vec2_to_string ( vec2  v)

Creates a string representation of the provided vector. NOTE: string is dynamically allocated, so the caller should free it.

Parameters
vThe vector to convert to string.
Returns
The string representation of the vector.

◆ vec3_to_string()

KAPI const char* vec3_to_string ( vec3  v)

Creates a string representation of the provided vector. NOTE: string is dynamically allocated, so the caller should free it.

Parameters
vThe vector to convert to string.
Returns
The string representation of the vector.

◆ vec4_to_string()

KAPI const char* vec4_to_string ( vec4  v)

Creates a string representation of the provided vector. NOTE: string is dynamically allocated, so the caller should free it.

Parameters
vThe vector to convert to string.
Returns
The string representation of the vector.