Consistent styling
This commit is contained in:
parent
fa56da7ba9
commit
76c1c58318
|
@ -7,8 +7,8 @@ library | latest version | category | languages | description
|
|||
**gb.h** | 0.03 | misc | C, C++ | A C helper library for C & C++
|
||||
**gb_math.h** | 0.04d | math | C, C++ | A C/C++ vector math library geared towards game development
|
||||
**gb_gl.h** | 0.01 | graphics | C, C++ | A C/C++ OpenGL Helper Library
|
||||
**gb_string.h** | 0.93 | strings | C, C++ | A better string library for C & C++ (this is built into gb.h too with custom allocator support!)
|
||||
**gb_ini.h** | 0.91a | misc | C, C++ | A simple ini file loader library for C & C++
|
||||
**gb_string.h** | 0.94 | strings | C, C++ | A better string library for C & C++ (this is built into gb.h too with custom allocator support!)
|
||||
**gb_ini.h** | 0.92 | misc | C, C++ | A simple ini file loader library for C & C++
|
||||
|
||||
|
||||
## FAQ
|
||||
|
|
146
gb_ini.h
146
gb_ini.h
|
@ -1,7 +1,8 @@
|
|||
// gb_ini.h - v0.91a - public domain ini file loader library - no warranty implied; use at your own risk
|
||||
// gb_ini.h - v0.92 - public domain ini file loader library - no warranty implied; use at your own risk
|
||||
// A Simple Ini File Loader Library for C and C++
|
||||
//
|
||||
// Version History:
|
||||
// 0.91 - New styling
|
||||
// 0.91a - Error handling fix
|
||||
// 0.91 - Add extern "C" if compiling as C++
|
||||
// 0.90 - Initial Version
|
||||
|
@ -59,17 +60,17 @@ name = Ginger Bill
|
|||
#include <stdlib.h>
|
||||
struct Library
|
||||
{
|
||||
const char* name;
|
||||
char const *name;
|
||||
int version;
|
||||
const char* license;
|
||||
const char* author;
|
||||
char const *license;
|
||||
char const *author;
|
||||
};
|
||||
|
||||
// The below macro expands to this:
|
||||
// static test_ini_handler(void* data, const char* section, const char* name, const char* value)
|
||||
// static test_ini_handlerchar const *data, char const *section, char const *name, char const *value)
|
||||
static GB_INI_HANDLER(test_ini_handler)
|
||||
{
|
||||
struct Library* lib = (struct Library*)data;
|
||||
struct Library *lib = (struct Library *)data;
|
||||
|
||||
#define TEST(s, n) (strcmp(section, s) == 0 && strcmp(name, n) == 0)
|
||||
|
||||
|
@ -89,10 +90,10 @@ static GB_INI_HANDLER(test_ini_handler)
|
|||
return 1;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
struct Library lib = {0};
|
||||
struct gb_Ini_Error err = gb_ini_parse("test.ini", &test_ini_handler, &lib);
|
||||
gbIniError err = gb_ini_parse("test.ini", &test_ini_handler, &lib);
|
||||
if (err.type != GB_INI_ERROR_NONE)
|
||||
{
|
||||
if (err.line_num > 0)
|
||||
|
@ -122,14 +123,14 @@ int main(int argc, char** argv)
|
|||
|
||||
struct Library
|
||||
{
|
||||
const char* name;
|
||||
char const *name;
|
||||
int version;
|
||||
const char* license;
|
||||
const char* author;
|
||||
char const *license;
|
||||
char const *author;
|
||||
};
|
||||
|
||||
// The below macro expands to this:
|
||||
// static test_ini_handler(void* data, const char* section, const char* name, const char* value)
|
||||
// static test_ini_handler(void *data, char const *section, char const *name, char const *value)
|
||||
static GB_INI_HANDLER(test_ini_handler)
|
||||
{
|
||||
Library* lib = (Library*)data;
|
||||
|
@ -213,8 +214,7 @@ int main(int argc, char** argv)
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
enum
|
||||
{
|
||||
enum {
|
||||
GB_INI_ERROR_NONE = 0,
|
||||
|
||||
GB_INI_ERROR_FILE_ERROR,
|
||||
|
@ -225,33 +225,27 @@ enum
|
|||
GB_INI_ERROR_COUNT,
|
||||
};
|
||||
|
||||
typedef struct gb_Ini_Error gb_Ini_Error;
|
||||
struct gb_Ini_Error
|
||||
{
|
||||
typedef struct gbIniError {
|
||||
int type;
|
||||
size_t line_num;
|
||||
};
|
||||
} gbIniError;
|
||||
|
||||
#ifndef GB_INI_HANDLER_RETURN_TYPE
|
||||
#define GB_INI_HANDLER_RETURN_TYPE
|
||||
typedef int gb_Ini_HRT;
|
||||
typedef int gbIniHRT;
|
||||
#endif
|
||||
|
||||
// This macro can be use to declare this type of function without
|
||||
// needing to define all the of the parameters
|
||||
#define GB_INI_HANDLER(func_name) gb_Ini_HRT func_name(void* data, const char* section, const char* name, const char* value)
|
||||
typedef GB_INI_HANDLER(gb_Ini_Handler);
|
||||
#define GB_INI_HANDLER(func_name) gbIniHRT func_name(void *data, char const *section, char const *name, char const *value)
|
||||
typedef GB_INI_HANDLER(gbIniHandler);
|
||||
|
||||
extern const char* GB_ERROR_STRINGS[GB_INI_ERROR_COUNT];
|
||||
extern char const *GB_ERROR_STRINGS[GB_INI_ERROR_COUNT];
|
||||
|
||||
gb_Ini_Error gb_ini_parse(const char* filename, gb_Ini_Handler* handler_func, void* data);
|
||||
gb_Ini_Error gb_ini_parse_file(FILE* file, gb_Ini_Handler* handler_func, void* data);
|
||||
gbIniError gb_ini_parse(char const *filename, gbIniHandler* handler_func, void *data);
|
||||
gbIniError gb_ini_parse_file(FILE *file, gbIniHandler* handler_func, void *data);
|
||||
|
||||
gb_inline const char*
|
||||
gb_ini_error_string(const struct gb_Ini_Error err)
|
||||
{
|
||||
return GB_ERROR_STRINGS[err.type];
|
||||
}
|
||||
gb_inline char const *gb_ini_error_string(gbIniError const err) { return GB_ERROR_STRINGS[err.type]; }
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -264,12 +258,11 @@ gb_ini_error_string(const struct gb_Ini_Error err)
|
|||
|
||||
namespace gb
|
||||
{
|
||||
typedef gb_Ini_Error Ini_Error;
|
||||
typedef gb_Ini_Handler Ini_Handler;
|
||||
typedef gbIniError IniError;
|
||||
typedef gbIniHandler IniHandler;
|
||||
|
||||
// Just a copy but with the GB_ prefix stripped
|
||||
enum
|
||||
{
|
||||
enum {
|
||||
INI_ERROR_NONE = 0,
|
||||
|
||||
INI_ERROR_FILE_ERROR,
|
||||
|
@ -280,23 +273,9 @@ enum
|
|||
// No need for enum count
|
||||
};
|
||||
|
||||
Ini_Error
|
||||
ini_parse(const char* filename, Ini_Handler* handler_func, void* data)
|
||||
{
|
||||
return gb_ini_parse(filename, handler_func, data);
|
||||
}
|
||||
|
||||
Ini_Error
|
||||
ini_parse(FILE* file, Ini_Handler* handler_func, void* data)
|
||||
{
|
||||
return gb_ini_parse_file(file, handler_func, data);
|
||||
}
|
||||
|
||||
const char*
|
||||
ini_error_string(const Ini_Error err)
|
||||
{
|
||||
return GB_ERROR_STRINGS[err.type];
|
||||
}
|
||||
inline Ini_Error ini_parse(char const *filename, Ini_Handler *handler_func, void *data) { return gb_ini_parse(filename, handler_func, data); }
|
||||
inline Ini_Error ini_parse(FILE *file, Ini_Handler *handler_func, void *data) { return gb_ini_parse_file(file, handler_func, data); }
|
||||
inline char const *ini_error_string(const Ini_Error err) { return GB_ERROR_STRINGS[err.type]; }
|
||||
|
||||
} // namespace gb
|
||||
#endif // GB_INI_CPP
|
||||
|
@ -306,7 +285,7 @@ ini_error_string(const Ini_Error err)
|
|||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
|
||||
const char* GB_ERROR_STRINGS[GB_INI_ERROR_COUNT] = {
|
||||
char const *GB_ERROR_STRINGS[GB_INI_ERROR_COUNT] = {
|
||||
"",
|
||||
|
||||
"Error in opening file",
|
||||
|
@ -316,7 +295,7 @@ const char* GB_ERROR_STRINGS[GB_INI_ERROR_COUNT] = {
|
|||
};
|
||||
|
||||
static gb_inline char*
|
||||
gb__left_whitespace_skip(const char* str)
|
||||
gb__left_whitespace_skip(char const *str)
|
||||
{
|
||||
while (*str && isspace((unsigned char)(*str)))
|
||||
str++;
|
||||
|
@ -333,12 +312,10 @@ gb__right_whitespace_strip(char* str)
|
|||
}
|
||||
|
||||
static gb_inline char*
|
||||
gb__find_char_or_comment_in_string(const char* str, char c)
|
||||
gb__find_char_or_comment_in_string(char const *str, char c)
|
||||
{
|
||||
int was_whitespace = 0;
|
||||
while (*str && *str != c &&
|
||||
!(was_whitespace && *str == ';'))
|
||||
{
|
||||
while (*str && *str != c && !(was_whitespace && *str == ';')) {
|
||||
was_whitespace = isspace((unsigned char)(*str));
|
||||
str++;
|
||||
}
|
||||
|
@ -347,7 +324,7 @@ gb__find_char_or_comment_in_string(const char* str, char c)
|
|||
}
|
||||
|
||||
static gb_inline char*
|
||||
gb__string_copy(char* dst, const char* src, size_t size)
|
||||
gb__string_copy(char* dst, char const *src, size_t size)
|
||||
{
|
||||
strncpy(dst, src, size);
|
||||
dst[size - 1] = '\0';
|
||||
|
@ -355,12 +332,12 @@ gb__string_copy(char* dst, const char* src, size_t size)
|
|||
}
|
||||
|
||||
|
||||
struct gb_Ini_Error
|
||||
gb_ini_parse(const char* filename, gb_Ini_Handler* handler_func, void* data)
|
||||
gbIniError
|
||||
gb_ini_parse(char const *filename, gbIniHandler *handler_func, void *data)
|
||||
{
|
||||
struct gb_Ini_Error err = {GB_INI_ERROR_FILE_ERROR, 0};
|
||||
gbIniError err = {GB_INI_ERROR_FILE_ERROR, 0};
|
||||
|
||||
FILE* file = fopen(filename, "r");
|
||||
FILE *file = fopen(filename, "r");
|
||||
if (!file)
|
||||
return err;
|
||||
|
||||
|
@ -369,8 +346,8 @@ gb_ini_parse(const char* filename, gb_Ini_Handler* handler_func, void* data)
|
|||
return err;
|
||||
}
|
||||
|
||||
struct gb_Ini_Error
|
||||
gb_ini_parse_file(FILE* file, gb_Ini_Handler* handler_func, void* data)
|
||||
gbIniError
|
||||
gb_ini_parse_file(FILE *file, gbIniHandler *handler_func, void *data)
|
||||
{
|
||||
char line[GB_INI_MAX_LINE_LENGTH] = "";
|
||||
size_t line_num = 0;
|
||||
|
@ -378,13 +355,12 @@ gb_ini_parse_file(FILE* file, gb_Ini_Handler* handler_func, void* data)
|
|||
char section[GB_INI_MAX_SECTION_LENGTH] = "";
|
||||
char prev_name[GB_INI_MAX_NAME_LENGTH] = "";
|
||||
|
||||
char* start;
|
||||
char* end;
|
||||
char *start;
|
||||
char *end;
|
||||
|
||||
struct gb_Ini_Error err = {GB_INI_ERROR_NONE, 0};
|
||||
struct gbIniError err = {GB_INI_ERROR_NONE, 0};
|
||||
|
||||
while (fgets(line, GB_INI_MAX_LINE_LENGTH, file) != 0)
|
||||
{
|
||||
while (fgets(line, GB_INI_MAX_LINE_LENGTH, file) != 0) {
|
||||
line_num++;
|
||||
|
||||
start = line;
|
||||
|
@ -394,8 +370,7 @@ gb_ini_parse_file(FILE* file, gb_Ini_Handler* handler_func, void* data)
|
|||
if (line_num == 1 &&
|
||||
(unsigned char)start[0] == 0xef &&
|
||||
(unsigned char)start[1] == 0xbb &&
|
||||
(unsigned char)start[2] == 0xbf)
|
||||
{
|
||||
(unsigned char)start[2] == 0xbf) {
|
||||
start += 3;
|
||||
}
|
||||
#endif
|
||||
|
@ -405,46 +380,37 @@ gb_ini_parse_file(FILE* file, gb_Ini_Handler* handler_func, void* data)
|
|||
if (start[0] == ';' || start[0] == '#')
|
||||
continue; // Allow '#' and ';' comments at start of line
|
||||
|
||||
if (start[0] == '[') // [section]
|
||||
{
|
||||
if (start[0] == '[') { // [section]
|
||||
end = gb__find_char_or_comment_in_string(start+1, ']');
|
||||
if (*end == ']')
|
||||
{
|
||||
if (*end == ']') {
|
||||
char *sect = start + 1;
|
||||
*end = '\0';
|
||||
char* sect = start + 1;
|
||||
sect = gb__left_whitespace_skip(gb__right_whitespace_strip(sect));
|
||||
|
||||
gb__string_copy(section, sect, sizeof(section));
|
||||
*prev_name = '\0';
|
||||
}
|
||||
else if (!err.type)
|
||||
{
|
||||
} else if (!err.type) {
|
||||
err.type = GB_INI_ERROR_MISSING_SECTION_BRACKET;
|
||||
err.line_num = line_num;
|
||||
}
|
||||
}
|
||||
else if (start[0] && start[0] != ';')
|
||||
{
|
||||
} else if (start[0] && start[0] != ';') {
|
||||
end = gb__find_char_or_comment_in_string(start, '=');
|
||||
if (*end == '=')
|
||||
{
|
||||
if (*end == '=') {
|
||||
char *name, *value;
|
||||
*end = '\0';
|
||||
char* name = gb__right_whitespace_strip(start);
|
||||
char* value = gb__left_whitespace_skip(end + 1);
|
||||
name = gb__right_whitespace_strip(start);
|
||||
value = gb__left_whitespace_skip(end + 1);
|
||||
end = gb__find_char_or_comment_in_string(value, '\0');
|
||||
if (*end == ';')
|
||||
*end = '\0';
|
||||
gb__right_whitespace_strip(value);
|
||||
|
||||
gb__string_copy(prev_name, name, sizeof(prev_name));
|
||||
if (!handler_func(data, section, name, value) && !err.type)
|
||||
{
|
||||
if (!handler_func(data, section, name, value) && !err.type) {
|
||||
err.type = GB_INI_ERROR_HANDLER_ERROR;
|
||||
err.line_num = line_num;
|
||||
}
|
||||
}
|
||||
else if (!err.type)
|
||||
{
|
||||
} else if (!err.type) {
|
||||
// No '=' found on name=value line
|
||||
err.type = GB_INI_ERROR_ASSIGNMENT_MISSING;
|
||||
err.line_num = line_num;
|
||||
|
|
279
gb_string.h
279
gb_string.h
|
@ -1,10 +1,11 @@
|
|||
// gb_string.h - v0.93 - public domain string library - no warranty implied; use at your own risk
|
||||
// gb_string.h - v0.94 - public domain string library - no warranty implied; use at your own risk
|
||||
// A Simple Dynamic Strings Library for C and C++
|
||||
//
|
||||
// Version History:
|
||||
// 0.94 - Remove "declare anywhere"
|
||||
// 0.93 - Fix typos and errors
|
||||
// 0.92 - Add extern "C" if compiling as C++
|
||||
// 0.91 - Remove `char* cstr` from String_Header
|
||||
// 0.91 - Remove `char * cstr` from String_Header
|
||||
// 0.90 - Initial Version
|
||||
//
|
||||
// LICENSE
|
||||
|
@ -58,7 +59,7 @@
|
|||
// {
|
||||
// size_t length;
|
||||
// size_t capacity;
|
||||
// char* cstring;
|
||||
// char * cstring;
|
||||
// };
|
||||
//
|
||||
// This library tries to augment normal C strings in a better way that is still
|
||||
|
@ -119,10 +120,10 @@
|
|||
#define GB_STRING_IMPLEMENTATION
|
||||
#include "gb_string.h"
|
||||
|
||||
int main(int argc, char** argv)
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
gb_String str = gb_make_string("Hello");
|
||||
gb_String other_str = gb_make_string_length(", ", 2);
|
||||
gbString str = gb_make_string("Hello");
|
||||
gbString other_str = gb_make_string_length(", ", 2);
|
||||
str = gb_append_string(str, other_str);
|
||||
str = gb_append_cstring(str, "world!");
|
||||
|
||||
|
@ -159,7 +160,7 @@ int main(int argc, char** argv)
|
|||
#define GB_STRING_IMPLEMENTATION
|
||||
#include "gb_string.h"
|
||||
|
||||
int main(int argc, char** argv)
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
using namespace gb;
|
||||
|
||||
|
@ -196,7 +197,7 @@ int main(int argc, char** argv)
|
|||
|
||||
#ifndef GB_ALLOC
|
||||
#define GB_ALLOC(sz) malloc(sz)
|
||||
#define GB_FREE(sz) free(sz)
|
||||
#define GB_FREE(ptr) free(ptr)
|
||||
#endif
|
||||
|
||||
#ifndef _MSC_VER
|
||||
|
@ -215,9 +216,9 @@ int main(int argc, char** argv)
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef char* gb_String;
|
||||
typedef char *gbString;
|
||||
|
||||
typedef signed int gb_bool;
|
||||
typedef int gbBool;
|
||||
#if !defined(GB_TRUE) || !defined(GB_FALSE)
|
||||
#define GB_TRUE 1
|
||||
#define GB_FALSE 0
|
||||
|
@ -225,7 +226,7 @@ typedef signed int gb_bool;
|
|||
|
||||
#ifndef GB_SIZE_TYPE
|
||||
#define GB_SIZE_TYPE
|
||||
typedef size_t gb_usize;
|
||||
typedef size_t gbUsize;
|
||||
#endif
|
||||
|
||||
#ifndef GB_NULLPTR
|
||||
|
@ -240,38 +241,37 @@ typedef size_t gb_usize;
|
|||
#endif
|
||||
#endif
|
||||
|
||||
struct gb_String_Header
|
||||
{
|
||||
gb_usize len;
|
||||
gb_usize cap;
|
||||
};
|
||||
typedef struct gbStringHeader {
|
||||
gbUsize len;
|
||||
gbUsize cap;
|
||||
} gbStringHeader;
|
||||
|
||||
#define GB_STRING_HEADER(s) ((struct gb_String_Header*)s - 1)
|
||||
#define GB_STRING_HEADER(s) ((gbStringHeader *)s - 1)
|
||||
|
||||
gb_String gb_make_string(const char* str);
|
||||
gb_String gb_make_string_length(const void* str, gb_usize len);
|
||||
void gb_free_string(gb_String str);
|
||||
gbString gb_make_string(char const *str);
|
||||
gbString gb_make_string_length(void const *str, gbUsize len);
|
||||
void gb_free_string(gbString str);
|
||||
|
||||
gb_String gb_duplicate_string(const gb_String str);
|
||||
gbString gb_duplicate_string(gbString const str);
|
||||
|
||||
gb_usize gb_string_length(const gb_String str);
|
||||
gb_usize gb_string_capacity(const gb_String str);
|
||||
gb_usize gb_string_available_space(const gb_String str);
|
||||
gbUsize gb_string_length(gbString const str);
|
||||
gbUsize gb_string_capacity(gbString const str);
|
||||
gbUsize gb_string_available_space(gbString const str);
|
||||
|
||||
void gb_clear_string(gb_String str);
|
||||
void gb_clear_string(gbString str);
|
||||
|
||||
gb_String gb_append_string_length(gb_String str, const void* other, gb_usize len);
|
||||
gb_String gb_append_string(gb_String str, const gb_String other);
|
||||
gb_String gb_append_cstring(gb_String str, const char* other);
|
||||
gbString gb_append_string_length(gbString str, void const *other, gbUsize len);
|
||||
gbString gb_append_string(gbString str, gbString const other);
|
||||
gbString gb_append_cstring(gbString str, char const *other);
|
||||
|
||||
gb_String gb_set_string(gb_String str, const char* cstr);
|
||||
gbString gb_set_string(gbString str, char const *cstr);
|
||||
|
||||
gb_String gb_string_make_space_for(gb_String str, gb_usize add_len);
|
||||
gb_usize gb_string_allocation_size(const gb_String str);
|
||||
gbString gb_string_make_space_for(gbString str, gbUsize add_len);
|
||||
gbUsize gb_string_allocation_size(gbString const str);
|
||||
|
||||
gb_bool gb_strings_are_equal(const gb_String lhs, const gb_String rhs);
|
||||
gbBool gb_strings_are_equal(gbString const lhs, gbString const rhs);
|
||||
|
||||
gb_String gb_trim_string(gb_String str, const char* cut_set);
|
||||
gbString gb_trim_string(gbString str, char const *cut_set);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -286,117 +286,57 @@ gb_String gb_trim_string(gb_String str, const char* cut_set);
|
|||
|
||||
namespace gb
|
||||
{
|
||||
typedef gb_String String;
|
||||
typedef gb_usize usize;
|
||||
|
||||
gb_inline String make_string(const char* str = "")
|
||||
{
|
||||
return gb_make_string(str);
|
||||
}
|
||||
|
||||
gb_inline String make_string(const void* str, usize len)
|
||||
{
|
||||
return gb_make_string_length(str, len);
|
||||
}
|
||||
|
||||
gb_inline void free_string(String& str)
|
||||
{
|
||||
gb_free_string(str);
|
||||
str = GB_NULLPTR;
|
||||
}
|
||||
|
||||
gb_inline String duplicate_string(const String str)
|
||||
{
|
||||
return gb_duplicate_string(str);
|
||||
}
|
||||
|
||||
gb_inline usize string_length(const String str)
|
||||
{
|
||||
return gb_string_length(str);
|
||||
}
|
||||
|
||||
gb_inline usize string_capacity(const String str)
|
||||
{
|
||||
return gb_string_capacity(str);
|
||||
}
|
||||
|
||||
gb_inline usize string_available_space(const String str)
|
||||
{
|
||||
return gb_string_available_space(str);
|
||||
}
|
||||
|
||||
gb_inline void clear_string(String str)
|
||||
{
|
||||
gb_clear_string(str);
|
||||
}
|
||||
|
||||
gb_inline void append_string_length(String& str, const void* other, usize len)
|
||||
{
|
||||
str = gb_append_string_length(str, other, len);
|
||||
}
|
||||
|
||||
gb_inline void append_string(String& str, const String other)
|
||||
{
|
||||
str = gb_append_string(str, other);
|
||||
}
|
||||
|
||||
gb_inline void append_cstring(String& str, const char* other)
|
||||
{
|
||||
str = gb_append_cstring(str, other);
|
||||
}
|
||||
|
||||
gb_inline void set_string(String& str, const char* cstr)
|
||||
{
|
||||
str = gb_set_string(str, cstr);
|
||||
}
|
||||
|
||||
gb_inline void string_make_space_for(String& str, usize add_len)
|
||||
{
|
||||
str = gb_string_make_space_for(str, add_len);
|
||||
}
|
||||
|
||||
gb_inline usize string_allocation_size(const String str)
|
||||
{
|
||||
return gb_string_allocation_size(str);
|
||||
}
|
||||
|
||||
gb_inline bool strings_are_equal(const String lhs, const String rhs)
|
||||
{
|
||||
return gb_strings_are_equal(lhs, rhs) == GB_TRUE;
|
||||
}
|
||||
|
||||
gb_inline void trim_string(String& str, const char* cut_set)
|
||||
{
|
||||
str = gb_trim_string(str, cut_set);
|
||||
}
|
||||
typedef gbString String;
|
||||
typedef gbUsize usize;
|
||||
|
||||
gb_inline String make_string(char const *str = "") { return gb_make_string(str); }
|
||||
gb_inline String make_string(void const *str, usize len) { return gb_make_string_length(str, len); }
|
||||
gb_inline void free_string(String& str) { gb_free_string(str); str = GB_NULLPTR; }
|
||||
gb_inline String duplicate_string(const String str) { return gb_duplicate_string(str); }
|
||||
gb_inline usize string_length(const String str) { return gb_string_length(str); }
|
||||
gb_inline usize string_capacity(const String str) { return gb_string_capacity(str); }
|
||||
gb_inline usize string_available_space(const String str) { return gb_string_available_space(str); }
|
||||
gb_inline void clear_string(String str) { gb_clear_string(str); }
|
||||
gb_inline void append_string_length(String& str, void const *other, usize len) { str = gb_append_string_length(str, other, len); }
|
||||
gb_inline void append_string(String& str, const String other) { str = gb_append_string(str, other); }
|
||||
gb_inline void append_cstring(String& str, char const *other) { str = gb_append_cstring(str, other); }
|
||||
gb_inline void set_string(String& str, char const *cstr) { str = gb_set_string(str, cstr); }
|
||||
gb_inline void string_make_space_for(String& str, usize add_len) { str = gb_string_make_space_for(str, add_len); }
|
||||
gb_inline usize string_allocation_size(const String str) { return gb_string_allocation_size(str); }
|
||||
gb_inline bool strings_are_equal(const String lhs, const String rhs) { return gb_strings_are_equal(lhs, rhs) == GB_TRUE; }
|
||||
gb_inline void trim_string(String& str, char const *cut_set) { str = gb_trim_string(str, cut_set); }
|
||||
} // namespace gb
|
||||
#endif // GB_STRING_CPP
|
||||
#endif // GB_STRING_H
|
||||
#ifdef GB_STRING_IMPLEMENTATION
|
||||
static void gb_set_string_length(gb_String str, gb_usize len)
|
||||
static void
|
||||
gb_set_string_length(gbString str, gbUsize len)
|
||||
{
|
||||
GB_STRING_HEADER(str)->len = len;
|
||||
}
|
||||
|
||||
static void gb_set_string_capacity(gb_String str, gb_usize cap)
|
||||
static void
|
||||
gb_set_string_capacity(gbString str, gbUsize cap)
|
||||
{
|
||||
GB_STRING_HEADER(str)->cap = cap;
|
||||
}
|
||||
|
||||
|
||||
gb_String gb_make_string_length(const void* init_str, gb_usize len)
|
||||
gbString
|
||||
gb_make_string_length(void const *init_str, gbUsize len)
|
||||
{
|
||||
gb_usize header_size = sizeof(struct gb_String_Header);
|
||||
void* ptr = GB_ALLOC(header_size + len + 1);
|
||||
gbString str;
|
||||
gbStringHeader *header;
|
||||
gbUsize header_size = sizeof(gbStringHeader);
|
||||
void *ptr = GB_ALLOC(header_size + len + 1);
|
||||
if (!init_str)
|
||||
memset(ptr, 0, header_size + len + 1);
|
||||
|
||||
if (ptr == GB_NULLPTR)
|
||||
return GB_NULLPTR;
|
||||
|
||||
gb_String str = (char*)ptr + header_size;
|
||||
struct gb_String_Header* header = GB_STRING_HEADER(str);
|
||||
str = (char *)ptr + header_size;
|
||||
header = GB_STRING_HEADER(str);
|
||||
header->len = len;
|
||||
header->cap = len;
|
||||
if (len && init_str)
|
||||
|
@ -406,52 +346,59 @@ gb_String gb_make_string_length(const void* init_str, gb_usize len)
|
|||
return str;
|
||||
}
|
||||
|
||||
gb_String gb_make_string(const char* str)
|
||||
gbString
|
||||
gb_make_string(char const *str)
|
||||
{
|
||||
gb_usize len = str ? strlen(str) : 0;
|
||||
gbUsize len = str ? strlen(str) : 0;
|
||||
return gb_make_string_length(str, len);
|
||||
}
|
||||
|
||||
void gb_free_string(gb_String str)
|
||||
void
|
||||
gb_free_string(gbString str)
|
||||
{
|
||||
if (str == GB_NULLPTR)
|
||||
return;
|
||||
|
||||
GB_FREE((struct gb_String_Header*)str - 1);
|
||||
GB_FREE((gbStringHeader *)str - 1);
|
||||
}
|
||||
|
||||
gb_String gb_duplicate_string(const gb_String str)
|
||||
gbString
|
||||
gb_duplicate_string(gbString const str)
|
||||
{
|
||||
return gb_make_string_length(str, gb_string_length(str));
|
||||
}
|
||||
|
||||
gb_usize gb_string_length(const gb_String str)
|
||||
gbUsize
|
||||
gb_string_length(gbString const str)
|
||||
{
|
||||
return GB_STRING_HEADER(str)->len;
|
||||
}
|
||||
|
||||
gb_usize gb_string_capacity(const gb_String str)
|
||||
gbUsize
|
||||
gb_string_capacity(gbString const str)
|
||||
{
|
||||
return GB_STRING_HEADER(str)->cap;
|
||||
}
|
||||
|
||||
gb_usize gb_string_available_space(const gb_String str)
|
||||
gbUsize
|
||||
gb_string_available_space(gbString const str)
|
||||
{
|
||||
struct gb_String_Header* h = GB_STRING_HEADER(str);
|
||||
gbStringHeader *h = GB_STRING_HEADER(str);
|
||||
if (h->cap > h->len)
|
||||
return h->cap - h->len;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void gb_clear_string(gb_String str)
|
||||
void
|
||||
gb_clear_string(gbString str)
|
||||
{
|
||||
gb_set_string_length(str, 0);
|
||||
str[0] = '\0';
|
||||
}
|
||||
|
||||
gb_String gb_append_string_length(gb_String str, const void* other, gb_usize other_len)
|
||||
gbString gb_append_string_length(gbString str, void const *other, gbUsize other_len)
|
||||
{
|
||||
gb_usize curr_len = gb_string_length(str);
|
||||
gbUsize curr_len = gb_string_length(str);
|
||||
|
||||
str = gb_string_make_space_for(str, other_len);
|
||||
if (str == GB_NULLPTR)
|
||||
|
@ -464,21 +411,20 @@ gb_String gb_append_string_length(gb_String str, const void* other, gb_usize oth
|
|||
return str;
|
||||
}
|
||||
|
||||
gb_String gb_append_string(gb_String str, const gb_String other)
|
||||
gbString gb_append_string(gbString str, gbString const other)
|
||||
{
|
||||
return gb_append_string_length(str, other, gb_string_length(other));
|
||||
}
|
||||
|
||||
gb_String gb_append_cstring(gb_String str, const char* other)
|
||||
gbString gb_append_cstring(gbString str, char const *other)
|
||||
{
|
||||
return gb_append_string_length(str, other, strlen(other));
|
||||
}
|
||||
|
||||
gb_String gb_set_string(gb_String str, const char* cstr)
|
||||
gbString gb_set_string(gbString str, char const *cstr)
|
||||
{
|
||||
gb_usize len = strlen(cstr);
|
||||
if (gb_string_capacity(str) < len)
|
||||
{
|
||||
gbUsize len = strlen(cstr);
|
||||
if (gb_string_capacity(str) < len) {
|
||||
str = gb_string_make_space_for(str, len - gb_string_length(str));
|
||||
if (str == GB_NULLPTR)
|
||||
return GB_NULLPTR;
|
||||
|
@ -491,8 +437,9 @@ gb_String gb_set_string(gb_String str, const char* cstr)
|
|||
return str;
|
||||
}
|
||||
|
||||
static gb_inline void* gb__string_realloc(void* ptr, gb_usize old_size, gb_usize new_size)
|
||||
static gb_inline void *gb__string_realloc(void *ptr, gbUsize old_size, gbUsize new_size)
|
||||
{
|
||||
void *new_ptr;
|
||||
if (!ptr)
|
||||
return GB_ALLOC(new_size);
|
||||
|
||||
|
@ -502,7 +449,7 @@ static gb_inline void* gb__string_realloc(void* ptr, gb_usize old_size, gb_usize
|
|||
if (old_size == new_size)
|
||||
return ptr;
|
||||
|
||||
void* new_ptr = GB_ALLOC(new_size);
|
||||
new_ptr = GB_ALLOC(new_size);
|
||||
if (!new_ptr)
|
||||
return GB_NULLPTR;
|
||||
|
||||
|
@ -513,45 +460,47 @@ static gb_inline void* gb__string_realloc(void* ptr, gb_usize old_size, gb_usize
|
|||
return new_ptr;
|
||||
}
|
||||
|
||||
gb_String gb_string_make_space_for(gb_String str, gb_usize add_len)
|
||||
gbString gb_string_make_space_for(gbString str, gbUsize add_len)
|
||||
{
|
||||
gb_usize len = gb_string_length(str);
|
||||
gb_usize new_len = len + add_len;
|
||||
gbUsize len = gb_string_length(str);
|
||||
gbUsize new_len = len + add_len;
|
||||
void *ptr, *new_ptr;
|
||||
gbUsize available, old_size, new_size;
|
||||
|
||||
gb_usize available = gb_string_available_space(str);
|
||||
available = gb_string_available_space(str);
|
||||
if (available >= add_len) // Return if there is enough space left
|
||||
return str;
|
||||
|
||||
|
||||
void* ptr = (char*)str - sizeof(struct gb_String_Header);
|
||||
gb_usize old_size = sizeof(struct gb_String_Header) + gb_string_length(str) + 1;
|
||||
gb_usize new_size = sizeof(struct gb_String_Header) + new_len + 1;
|
||||
ptr = (char *)str - sizeof(gbStringHeader);
|
||||
old_size = sizeof(gbStringHeader) + gb_string_length(str) + 1;
|
||||
new_size = sizeof(gbStringHeader) + new_len + 1;
|
||||
|
||||
void* new_ptr = gb__string_realloc(ptr, old_size, new_size);
|
||||
new_ptr = gb__string_realloc(ptr, old_size, new_size);
|
||||
if (new_ptr == GB_NULLPTR)
|
||||
return GB_NULLPTR;
|
||||
str = (char*)new_ptr + sizeof(struct gb_String_Header);
|
||||
str = (char *)new_ptr + sizeof(gbStringHeader);
|
||||
|
||||
gb_set_string_capacity(str, new_len);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
gb_usize gb_string_allocation_size(const gb_String s)
|
||||
gbUsize gb_string_allocation_size(gbString const s)
|
||||
{
|
||||
gb_usize cap = gb_string_capacity(s);
|
||||
return sizeof(struct gb_String_Header) + cap;
|
||||
gbUsize cap = gb_string_capacity(s);
|
||||
return sizeof(gbStringHeader) + cap;
|
||||
}
|
||||
|
||||
gb_bool gb_strings_are_equal(const gb_String lhs, const gb_String rhs)
|
||||
gbBool gb_strings_are_equal(gbString const lhs, gbString const rhs)
|
||||
{
|
||||
gb_usize lhs_len = gb_string_length(lhs);
|
||||
gb_usize rhs_len = gb_string_length(rhs);
|
||||
gbUsize lhs_len, rhs_len, i;
|
||||
lhs_len = gb_string_length(lhs);
|
||||
rhs_len = gb_string_length(rhs);
|
||||
if (lhs_len != rhs_len)
|
||||
return GB_FALSE;
|
||||
|
||||
for (gb_usize i = 0; i < lhs_len; i++)
|
||||
{
|
||||
for (i = 0; i < lhs_len; i++) {
|
||||
if (lhs[i] != rhs[i])
|
||||
return GB_FALSE;
|
||||
}
|
||||
|
@ -559,12 +508,10 @@ gb_bool gb_strings_are_equal(const gb_String lhs, const gb_String rhs)
|
|||
return GB_TRUE;
|
||||
}
|
||||
|
||||
gb_String gb_trim_string(gb_String str, const char* cut_set)
|
||||
gbString gb_trim_string(gbString str, char const *cut_set)
|
||||
{
|
||||
char* start;
|
||||
char* end;
|
||||
char* start_pos;
|
||||
char* end_pos;
|
||||
char *start, *end, *start_pos, *end_pos;
|
||||
gbUsize len;
|
||||
|
||||
start_pos = start = str;
|
||||
end_pos = end = str + gb_string_length(str) - 1;
|
||||
|
@ -574,7 +521,7 @@ gb_String gb_trim_string(gb_String str, const char* cut_set)
|
|||
while (end_pos > start_pos && strchr(cut_set, *end_pos))
|
||||
end_pos--;
|
||||
|
||||
gb_usize len = (start_pos > end_pos) ? 0 : ((end_pos - start_pos)+1);
|
||||
len = (start_pos > end_pos) ? 0 : ((end_pos - start_pos)+1);
|
||||
|
||||
if (str != start_pos)
|
||||
memmove(str, start_pos, len);
|
||||
|
|
Loading…
Reference in New Issue