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 #include "strings/kname.h"
17 #include "strings/kstring_id.h"
18 
19 #include <stdio.h>
20 
29 KAPI u64 string_length(const char* str);
30 
39 KAPI u32 string_utf8_length(const char* str);
40 
49 KAPI u64 string_nlength(const char* str, u32 max_len);
50 
59 KAPI u32 string_utf8_nlength(const char* str, u32 max_len);
60 
71 KAPI b8 bytes_to_codepoint(const char* bytes, u32 offset, i32* out_codepoint, u8* out_advance);
72 
80 
88 
95 KAPI char* string_duplicate(const char* str);
96 
102 KAPI void string_free(const char* str);
103 
104 KAPI i64 kstr_ncmp(const char* str0, const char* str1, u32 max_len);
105 
106 KAPI i64 kstr_ncmpi(const char* str0, const char* str1, u32 max_len);
107 
114 KAPI b8 strings_equal(const char* str0, const char* str1);
115 
122 KAPI b8 strings_equali(const char* str0, const char* str1);
123 
132 KAPI b8 strings_nequal(const char* str0, const char* str1, u32 max_len);
133 
142 KAPI b8 strings_nequali(const char* str0, const char* str1, u32 max_len);
143 
158 KAPI char* string_format(const char* format, ...);
159 
168 KAPI char* string_format_v(const char* format, va_list va_listp);
169 
179 KDEPRECATED("This version of string format is legacy, and unsafe. Use string_nformat() or string_format() instead.")
180 KAPI i32 string_format_unsafe(char* dest, const char* format, ...);
181 
191 KDEPRECATED("This version of string format variadic is legacy, and unsafe. Use string_nformat_v() or string_format_v() instead.")
192 KAPI i32 string_format_v_unsafe(char* dest, const char* format, void* va_list);
193 
203 KAPI i32 string_nformat(char* dest, u32 max_len, const char* format, ...);
204 
214 KAPI i32 string_nformat_v(char* dest, u32 max_len, const char* format, void* va_list);
215 
222 KAPI char* string_empty(char* str);
223 
230 KAPI char* string_copy(char* dest, const char* source);
231 
241 KAPI char* string_ncopy(char* dest, const char* source, u32 max_len);
242 
251 KAPI char* string_trim(char* str);
252 
260 KAPI void string_mid(char* dest, const char* source, i32 start, i32 length);
261 
269 KAPI i32 string_index_of(const char* str, char c);
270 
278 KAPI i32 string_last_index_of(const char* str, char c);
279 
287 KAPI i32 string_index_of_str(const char* str_0, const char* str_1);
288 
296 KAPI b8 string_starts_with(const char* str_0, const char* str_1);
297 
305 KAPI b8 string_starts_withi(const char* str_0, const char* str_1);
306 
307 KAPI void string_insert_char_at(char* dest, const char* src, u32 pos, char c);
308 KAPI void string_insert_str_at(char* dest, const char* src, u32 pos, const char* str);
309 KAPI void string_remove_at(char* dest, const char* src, u32 pos, u32 length);
310 
320 KAPI i32 string_replace_char(char* str, char find, char replace);
321 
331 KAPI u32 string_replace_char_all(char* str, char find, char replace);
332 
340 KAPI b8 string_to_mat4(const char* str, mat4* out_mat);
341 
349 KAPI const char* mat4_to_string(mat4 m);
350 
358 KAPI b8 string_to_rect_2di(const char* str, rect_2di* rect);
359 
367 KAPI const char* rect_2di_to_string(rect_2di rect);
368 
376 KAPI b8 string_to_vec4(const char* str, vec4* out_vector);
377 
385 KAPI const char* vec4_to_string(vec4 v);
386 
394 KAPI b8 string_to_vec3(const char* str, vec3* out_vector);
395 
403 KAPI const char* vec3_to_string(vec3 v);
404 
412 KAPI b8 string_to_vec2(const char* str, vec2* out_vector);
413 
421 KAPI const char* vec2_to_string(vec2 v);
422 
430 KAPI b8 string_to_f32(const char* str, f32* f);
431 
439 KAPI const char* f32_to_string(f32 f);
440 
448 KAPI b8 string_to_f64(const char* str, f64* f);
449 
457 KAPI const char* f64_to_string(f64 f);
458 
466 KAPI b8 string_to_i8(const char* str, i8* i);
467 
475 KAPI const char* i8_to_string(i8 i);
476 
484 KAPI b8 string_to_i16(const char* str, i16* i);
485 
493 KAPI const char* i16_to_string(i16 i);
494 
502 KAPI b8 string_to_i32(const char* str, i32* i);
503 
511 KAPI const char* i32_to_string(i32 i);
512 
520 KAPI b8 string_to_i64(const char* str, i64* i);
521 
529 KAPI const char* i64_to_string(i64 i);
530 
538 KAPI b8 string_to_u8(const char* str, u8* u);
539 
547 KAPI const char* u8_to_string(u8 u);
548 
556 KAPI b8 string_to_u16(const char* str, u16* u);
557 
565 KAPI const char* u16_to_string(u16 u);
566 
574 KAPI b8 string_to_u32(const char* str, u32* u);
575 
583 KAPI const char* u32_to_string(u32 u);
584 
592 KAPI b8 string_to_u64(const char* str, u64* u);
593 
601 KAPI const char* u64_to_string(u64 u);
602 
611 KAPI b8 string_to_bool(const char* str, b8* b);
612 
621 KAPI const char* bool_to_string(b8 b);
622 
636 KAPI u32 string_split(const char* str, char delimiter, char*** str_darray, b8 trim_entries, b8 include_empty, b8 escape_strings);
637 
644 KAPI void string_cleanup_split_darray(char** str_darray);
645 
654 KAPI void string_cleanup_array(const char** str_array, u32 length);
655 
669 KAPI u32 string_nsplit(const char* str, char delimiter, u32 max_count, char** str_array, b8 trim_entries, b8 include_empty);
670 
678 KAPI void string_cleanup_split_array(char** str_array, u32 max_count);
679 
687 KAPI void string_append_string(char* dest, const char* source, const char* append);
688 
696 KAPI void string_append_int(char* dest, const char* source, i64 i);
697 
705 KAPI void string_append_float(char* dest, const char* source, f32 f);
706 
714 KAPI void string_append_bool(char* dest, const char* source, b8 b);
715 
723 KAPI void string_append_char(char* dest, const char* source, char c);
724 
737 KAPI char* string_join(const char** strings, u32 count, char delimiter);
738 
739 KAPI char* kstring_id_join(const kstring_id* strings, u32 count, char delimiter);
740 
741 KAPI char* kname_join(const kname* strings, u32 count, char delimiter);
742 
749 KAPI const char* string_directory_from_path(const char* path);
750 
759 KAPI const char* string_filename_from_path(const char* path);
760 
769 KAPI const char* string_filename_no_extension_from_path(const char* path);
770 
780 KAPI const char* string_extension_from_path(const char* path, b8 include_dot);
781 
788 KAPI b8 string_parse_array_length(const char* str, u32* out_length);
789 
790 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);
791 
802 
806 KAPI void string_to_lower(char* str);
810 KAPI void string_to_upper(char* str);
811 
812 // ----------------------
813 // KString implementation
814 // ----------------------
815 
822 typedef struct kstring {
828  char* data;
830 
831 KAPI void kstring_create(kstring* out_string);
832 KAPI void kstring_from_cstring(const char* source, kstring* out_string);
834 
837 
838 KAPI void kstring_append_str(kstring* string, const char* s);
839 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:209
unsigned int u32
Unsigned 32-bit integer.
Definition: defines.h:27
signed char i8
Signed 8-bit integer.
Definition: defines.h:35
_Bool b8
8-bit boolean type
Definition: defines.h:60
float f32
32-bit floating point number
Definition: defines.h:49
double f64
64-bit floating point number
Definition: defines.h:52
signed int i32
Signed 32-bit integer.
Definition: defines.h:41
unsigned short u16
Unsigned 16-bit integer.
Definition: defines.h:24
signed short i16
Signed 16-bit integer.
Definition: defines.h:38
unsigned long long u64
Unsigned 64-bit integer.
Definition: defines.h:30
signed long long i64
Signed 64-bit integer.
Definition: defines.h:44
unsigned char u8
Unsigned 8-bit integer.
Definition: defines.h:21
This files contains an implementation of knames.
u64 kname
A kname is a string hash made for quick comparisons versus traditional string comparisons.
Definition: kname.h:36
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 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 u32 string_replace_char_all(char *str, char find, char replace)
Replaces all instances of char find with the one provided (replace). Done in place.
const char const char void * va_list
Definition: kstring.h:192
KAPI void kstring_create(kstring *out_string)
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 char * kname_join(const kname *strings, u32 count, char delimiter)
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 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 char * string_format_v(const char *format, va_list va_listp)
Performs variadic string formatting against the given format string and va_list. NOTE: that this perf...
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 i32 string_replace_char(char *str, char find, char replace)
Replaces the first instance of char find with the one provided (replace). Done in place.
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 i32 string_nformat_v(char *dest, u32 max_len, const char *format, void *va_list)
Performs variadic string formatting to dest given format string up to max_len length in bytes and va_...
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 b8 string_to_rect_2di(const char *str, rect_2di *rect)
Attempts to parse a rect_2di from the provided string.
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 i32 string_nformat(char *dest, u32 max_len, const char *format,...)
Performs string formatting to dest given format string up to max_len length in bytes and parameters.
const char * format
Definition: kstring.h:180
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...
KDEPRECATED("This version of string format is legacy, and unsafe. Use string_nformat() or string_format() instead.") KAPI i32 string_format_unsafe(char *dest
Performs string formatting to dest given format string and parameters.
KAPI u32 string_split(const char *str, char delimiter, char ***str_darray, b8 trim_entries, b8 include_empty, b8 escape_strings)
Splits the given string by the delimiter provided and stores in the provided darray....
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 const char * rect_2di_to_string(rect_2di rect)
Creates a string representation of the provided rectangle. NOTE: string is dynamically allocated,...
KAPI b8 codepoint_is_lower(i32 codepoint)
KAPI char * string_format(const char *format,...)
Performs string formatting against the given format string and parameters.
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 char * kstring_id_join(const kstring_id *strings, u32 count, char delimiter)
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 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,...
This files contains an implementation of kstring_ids.
u64 kstring_id
A kstring_id is a string hash made for quick comparisons versus traditional string comparisons.
Definition: kstring_id.h:32
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:822
u32 length
The current length of the string in bytes.
Definition: kstring.h:824
u32 allocated
The amount of currently allocated memory. Always accounts for a null terminator.
Definition: kstring.h:826
char * data
The raw string data.
Definition: kstring.h:828
a 4x4 matrix, typically used to represent object transformations.
Definition: math_types.h:383
A 2-element vector.
Definition: math_types.h:31
A 3-element vector.
Definition: math_types.h:117
A 4-element vector.
Definition: math_types.h:229
A 4-element integer-based vector.
Definition: math_types.h:574