Kohi Game Engine
kstring.h
Go to the documentation of this file.
1 
12 #pragma once
13 
14 #include "defines.h"
15 #include "math/math_types.h"
16 
25 KAPI u64 string_length(const char* str);
26 
35 KAPI u32 string_utf8_length(const char* str);
36 
45 KAPI u64 string_nlength(const char* str, u32 max_len);
46 
55 KAPI u32 string_utf8_nlength(const char* str, u32 max_len);
56 
67 KAPI b8 bytes_to_codepoint(const char* bytes, u32 offset, i32* out_codepoint, u8* out_advance);
68 
76 
84 
91 KAPI char* string_duplicate(const char* str);
92 
98 KAPI void string_free(const char* str);
99 
100 KAPI i64 kstr_ncmp(const char* str0, const char* str1, u32 max_len);
101 
102 KAPI i64 kstr_ncmpi(const char* str0, const char* str1, u32 max_len);
103 
110 KAPI b8 strings_equal(const char* str0, const char* str1);
111 
118 KAPI b8 strings_equali(const char* str0, const char* str1);
119 
128 KAPI b8 strings_nequal(const char* str0, const char* str1, u32 max_len);
129 
138 KAPI b8 strings_nequali(const char* str0, const char* str1, u32 max_len);
139 
148 KAPI char* string_format(const char* format, ...);
149 
158 KAPI char* string_format_v(const char* format, void* va_list);
159 
169 KDEPRECATED("This version of string format is legacy, and unsafe. Use string_format() instead.")
170 KAPI i32 string_format_unsafe(char* dest, const char* format, ...);
171 
181 KDEPRECATED("This version of string format variadic is legacy, and unsafe. Use string_format_v() instead.")
182 KAPI i32 string_format_v_unsafe(char* dest, const char* format, void* va_list);
183 
190 KAPI char* string_empty(char* str);
191 
198 KAPI char* string_copy(char* dest, const char* source);
199 
209 KAPI char* string_ncopy(char* dest, const char* source, u32 max_len);
210 
219 KAPI char* string_trim(char* str);
220 
228 KAPI void string_mid(char* dest, const char* source, i32 start, i32 length);
229 
237 KAPI i32 string_index_of(const char* str, char c);
238 
246 KAPI i32 string_last_index_of(const char* str, char c);
247 
255 KAPI i32 string_index_of_str(const char* str_0, const char* str_1);
256 
264 KAPI b8 string_starts_with(const char* str_0, const char* str_1);
265 
273 KAPI b8 string_starts_withi(const char* str_0, const char* str_1);
274 
275 KAPI void string_insert_char_at(char* dest, const char* src, u32 pos, char c);
276 KAPI void string_insert_str_at(char* dest, const char* src, u32 pos, const char* str);
277 KAPI void string_remove_at(char* dest, const char* src, u32 pos, u32 length);
278 
286 KAPI b8 string_to_mat4(const char* str, mat4* out_mat);
287 
295 KAPI const char* mat4_to_string(mat4 m);
296 
304 KAPI b8 string_to_vec4(const char* str, vec4* out_vector);
305 
313 KAPI const char* vec4_to_string(vec4 v);
314 
322 KAPI b8 string_to_vec3(const char* str, vec3* out_vector);
323 
331 KAPI const char* vec3_to_string(vec3 v);
332 
340 KAPI b8 string_to_vec2(const char* str, vec2* out_vector);
341 
349 KAPI const char* vec2_to_string(vec2 v);
350 
358 KAPI b8 string_to_f32(const char* str, f32* f);
359 
367 KAPI const char* f32_to_string(f32 f);
368 
376 KAPI b8 string_to_f64(const char* str, f64* f);
377 
385 KAPI const char* f64_to_string(f64 f);
386 
394 KAPI b8 string_to_i8(const char* str, i8* i);
395 
403 KAPI const char* i8_to_string(i8 i);
404 
412 KAPI b8 string_to_i16(const char* str, i16* i);
413 
421 KAPI const char* i16_to_string(i16 i);
422 
430 KAPI b8 string_to_i32(const char* str, i32* i);
431 
439 KAPI const char* i32_to_string(i32 i);
440 
448 KAPI b8 string_to_i64(const char* str, i64* i);
449 
457 KAPI const char* i64_to_string(i64 i);
458 
466 KAPI b8 string_to_u8(const char* str, u8* u);
467 
475 KAPI const char* u8_to_string(u8 u);
476 
484 KAPI b8 string_to_u16(const char* str, u16* u);
485 
493 KAPI const char* u16_to_string(u16 u);
494 
502 KAPI b8 string_to_u32(const char* str, u32* u);
503 
511 KAPI const char* u32_to_string(u32 u);
512 
520 KAPI b8 string_to_u64(const char* str, u64* u);
521 
529 KAPI const char* u64_to_string(u64 u);
530 
539 KAPI b8 string_to_bool(const char* str, b8* b);
540 
549 KAPI const char* bool_to_string(b8 b);
550 
563 KAPI u32 string_split(const char* str, char delimiter, char*** str_darray, b8 trim_entries, b8 include_empty);
564 
571 KAPI void string_cleanup_split_darray(char** str_darray);
572 
581 KAPI void string_cleanup_array(const char** str_array, u32 length);
582 
596 KAPI u32 string_nsplit(const char* str, char delimiter, u32 max_count, char** str_array, b8 trim_entries, b8 include_empty);
597 
605 KAPI void string_cleanup_split_array(char** str_array, u32 max_count);
606 
614 KAPI void string_append_string(char* dest, const char* source, const char* append);
615 
623 KAPI void string_append_int(char* dest, const char* source, i64 i);
624 
632 KAPI void string_append_float(char* dest, const char* source, f32 f);
633 
641 KAPI void string_append_bool(char* dest, const char* source, b8 b);
642 
650 KAPI void string_append_char(char* dest, const char* source, char c);
651 
664 KAPI char* string_join(const char** strings, u32 count, char delimiter);
665 
672 KAPI const char* string_directory_from_path(const char* path);
673 
682 KAPI const char* string_filename_from_path(const char* path);
683 
692 KAPI const char* string_filename_no_extension_from_path(const char* path);
693 
703 KAPI const char* string_extension_from_path(const char* path, b8 include_dot);
704 
711 KAPI b8 string_parse_array_length(const char* str, u32* out_length);
712 
713 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);
714 
725 
729 KAPI void string_to_lower(char* str);
733 KAPI void string_to_upper(char* str);
734 
735 // ----------------------
736 // KString implementation
737 // ----------------------
738 
745 typedef struct kstring {
751  char* data;
753 
754 KAPI void kstring_create(kstring* out_string);
755 KAPI void kstring_from_cstring(const char* source, kstring* out_string);
757 
760 
761 KAPI void kstring_append_str(kstring* string, const char* s);
762 KAPI void kstring_append_kstring(kstring* string, const kstring* other);
This file contains global type definitions which are used throughout the entire engine and applicatio...
#define KAPI
Import/export qualifier.
Definition: defines.h:205
unsigned int u32
Unsigned 32-bit integer.
Definition: defines.h:25
signed char i8
Signed 8-bit integer.
Definition: defines.h:33
_Bool b8
8-bit boolean type
Definition: defines.h:58
float f32
32-bit floating point number
Definition: defines.h:47
double f64
64-bit floating point number
Definition: defines.h:50
signed int i32
Signed 32-bit integer.
Definition: defines.h:39
unsigned short u16
Unsigned 16-bit integer.
Definition: defines.h:22
signed short i16
Signed 16-bit integer.
Definition: defines.h:36
unsigned long long u64
Unsigned 64-bit integer.
Definition: defines.h:28
signed long long i64
Signed 64-bit integer.
Definition: defines.h:42
unsigned char u8
Unsigned 8-bit integer.
Definition: defines.h:19
KAPI i64 kstr_ncmpi(const char *str0, const char *str1, u32 max_len)
KAPI b8 codepoint_is_whitespace(i32 codepoint)
Indicates if the provided codepoint is considered whitespace.
KAPI b8 string_to_i16(const char *str, i16 *i)
Attempts to parse a 16-bit signed integer from the provided string.
KAPI void string_append_float(char *dest, const char *source, f32 f)
Appends the supplied float to source and outputs to dest.
KAPI b8 codepoint_is_alpha(i32 codepoint)
KAPI const char * i32_to_string(i32 i)
Creates a string representation of the provided integer. NOTE: string is dynamically allocated,...
KAPI b8 strings_nequali(const char *str0, const char *str1, u32 max_len)
Case-insensitive string comparison, where comparison stops at max_len.
KAPI b8 strings_equali(const char *str0, const char *str1)
Case-insensitive string comparison.
KAPI b8 string_to_u64(const char *str, u64 *u)
Attempts to parse a 64-bit unsigned integer from the provided string.
KAPI void string_append_string(char *dest, const char *source, const char *append)
KAPI const char * f64_to_string(f64 f)
Creates a string representation of the provided 64-bit float. NOTE: string is dynamically allocated,...
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....
KAPI char * string_copy(char *dest, const char *source)
Copies the string in source to dest. Does not perform any allocations.
KAPI b8 string_to_u16(const char *str, u16 *u)
Attempts to parse a 16-bit unsigned integer from the provided string.
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 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.
KAPI u32 kstring_utf8_length(const kstring *string)
KAPI b8 string_starts_with(const char *str_0, const char *str_1)
Indicates if str_0 starts with str_1. Case-sensitive.
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) ...
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....
KAPI const char * vec4_to_string(vec4 v)
Creates a string representation of the provided vector. NOTE: string is dynamically allocated,...
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...
KAPI i32 string_format_unsafe(char *dest, const char *format,...)
Performs string formatting to dest given format string and parameters.
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 Tru...
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....
KAPI void string_to_upper(char *str)
KAPI b8 string_to_i8(const char *str, i8 *i)
Attempts to parse an 8-bit signed integer from the provided string.
KAPI b8 strings_equal(const char *str0, const char *str1)
Case-sensitive string comparison.
KAPI void string_append_char(char *dest, const char *source, char c)
Appends the supplied character to source and outputs to dest.
KAPI void kstring_from_cstring(const char *source, kstring *out_string)
KAPI const char * vec3_to_string(vec3 v)
Creates a string representation of the provided vector. NOTE: string is dynamically allocated,...
KAPI b8 string_starts_withi(const char *str_0, const char *str_1)
Indicates if str_0 starts with str_1. Case-insensitive.
KAPI b8 string_to_u32(const char *str, u32 *u)
Attempts to parse a 32-bit unsigned integer from the provided string.
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.
KAPI char * string_trim(char *str)
Performs an in-place trim of the provided string. This removes all whitespace from both ends of the s...
KAPI void string_to_lower(char *str)
KAPI b8 string_to_vec2(const char *str, vec2 *out_vector)
Attempts to parse a vector from the provided string.
KAPI b8 string_to_f32(const char *str, f32 *f)
Attempts to parse a 32-bit floating-point number from the provided string.
KAPI void kstring_create(kstring *out_string)
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....
KAPI const char * string_filename_no_extension_from_path(const char *path)
Extracts the filename (excluding file extension) from a full file path.
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 ever...
KAPI const char * i16_to_string(i16 i)
Creates a string representation of the provided integer. NOTE: string is dynamically allocated,...
KAPI const char * string_filename_from_path(const char *path)
Extracts the filename (including file extension) from a full file path.
KAPI const char * u32_to_string(u32 u)
Creates a string representation of the provided integer. NOTE: string is dynamically allocated,...
KAPI void kstring_destroy(kstring *string)
KAPI u32 string_utf8_length(const char *str)
Gets the length of a string in UTF-8 (potentially multibyte) characters, minus the null terminator.
KAPI void kstring_append_str(kstring *string, const char *s)
KAPI void string_free(const char *str)
Frees the memory of the given string.
struct kstring kstring
A kstring is a managed string for higher-level logic to use. It is safer and, in some cases quicker t...
KAPI const char * mat4_to_string(mat4 m)
Creates a string representation of the provided matrix. NOTE: string is dynamically allocated,...
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 perf...
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.
KAPI u32 kstring_length(const kstring *string)
KAPI const char * u64_to_string(u64 u)
Creates a string representation of the provided integer. NOTE: string is dynamically allocated,...
KAPI const char * i8_to_string(i8 i)
Creates a string representation of the provided integer. NOTE: string is dynamically allocated,...
KAPI const char * vec2_to_string(vec2 v)
Creates a string representation of the provided vector. NOTE: string is dynamically allocated,...
KAPI b8 codepoint_is_numeric(i32 codepoint)
KAPI b8 string_to_f64(const char *str, f64 *f)
Attempts to parse a 64-bit floating-point number from the provided string.
KAPI const char * string_directory_from_path(const char *path)
Extracts the directory from a full file path.
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....
KAPI b8 string_to_mat4(const char *str, mat4 *out_mat)
Attempts to parse a 4x4 matrix from the provided string.
KAPI b8 char_is_whitespace(char c)
Indicates if the provided character is considered whitespace.
KAPI const char * u16_to_string(u16 u)
Creates a string representation of the provided integer. NOTE: string is dynamically allocated,...
KAPI b8 string_to_i64(const char *str, i64 *i)
Attempts to parse a 64-bit signed integer from the provided string.
KAPI const char * f32_to_string(f32 f)
Creates a string representation of the provided float. NOTE: string is dynamically allocated,...
KAPI void kstring_append_kstring(kstring *string, const kstring *other)
KAPI u64 string_length(const char *str)
Gets the number of bytes of the given string, minus the null terminator.
KAPI i32 string_index_of(const char *str, char c)
Returns the index of the first occurance of c in str; otherwise -1.
KAPI b8 strings_nequal(const char *str0, const char *str1, u32 max_len)
Case-sensitive string comparison, where comparison stops at max_len.
KAPI b8 string_to_u8(const char *str, u8 *u)
Attempts to parse an 8-bit unsigned integer from the provided string.
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 fin...
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 els...
KAPI char * string_empty(char *str)
Empties the provided string by setting the first character to 0.
KAPI void string_insert_char_at(char *dest, const char *src, u32 pos, char c)
KAPI b8 string_to_i32(const char *str, i32 *i)
Attempts to parse a 32-bit signed integer from the provided string.
KAPI b8 string_to_vec4(const char *str, vec4 *out_vector)
Attempts to parse a vector from the provided string.
KAPI i64 kstr_ncmp(const char *str0, const char *str1, u32 max_len)
KAPI b8 codepoint_is_space(i32 codepoint)
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.
KAPI b8 codepoint_is_lower(i32 codepoint)
KAPI char * string_format(const char *format,...)
Performs string formatting against the given format string and parameters. NOTE: that this performs a...
KAPI i32 string_last_index_of(const char *str, char c)
Returns the index of the last occurance of c in str; otherwise -1.
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 void string_cleanup_split_darray(char **str_darray)
Cleans up string allocations in str_darray, but does not free the darray itself.
KAPI char * string_duplicate(const char *str)
Duplicates the provided string. Note that this allocates new memory, which should be freed by the cal...
KAPI const char * u8_to_string(u8 u)
Creates a string representation of the provided integer. NOTE: string is dynamically allocated,...
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.
KAPI void string_cleanup_array(const char **str_array, u32 length)
Cleans up string allocations in str_array and frees the array itself.
KAPI b8 codepoint_is_upper(i32 codepoint)
KAPI void string_append_int(char *dest, const char *source, i64 i)
Appends the supplied integer to source and outputs to dest.
KAPI b8 string_to_vec3(const char *str, vec3 *out_vector)
Attempts to parse a vector from the provided string.
KAPI const char * i64_to_string(i64 i)
Creates a string representation of the provided integer. NOTE: string is dynamically allocated,...
Contains various math types required for the engine.
A kstring is a managed string for higher-level logic to use. It is safer and, in some cases quicker t...
Definition: kstring.h:745
u32 length
The current length of the string in bytes.
Definition: kstring.h:747
u32 allocated
The amount of currently allocated memory. Always accounts for a null terminator.
Definition: kstring.h:749
char * data
The raw string data.
Definition: kstring.h:751
a 4x4 matrix, typically used to represent object transformations.
Definition: math_types.h:195
A 2-element vector.
Definition: math_types.h:19
A 3-element vector.
Definition: math_types.h:49
A 4-element vector.
Definition: math_types.h:89