gb/gb_string.h

542 lines
14 KiB
C
Raw Normal View History

2016-04-28 10:02:07 -04:00
/* gb_string.h - v0.95 - public domain string library - no warranty implied; use at your own risk
A Simple Dynamic Strings Library for C and C++
Version History:
0.95 - C90 Support
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.90 - Initial Version
LICENSE
This software is in the public domain. Where that dedication is not
recognized, you are granted a perpetual, irrevocable license to copy,
distribute, and modify this file as you see fit.
How to use:
Do this:
#define GB_STRING_IMPLEMENTATION
before you include this file in *one* C++ file to create the implementation
i.e. it should look like this:
#include ...
#include ...
#include ...
#define GB_STRING_IMPLEMENTATION
#include "gb_string.h"
You can #define GB_ALLOC, and GB_FREE to avoid using malloc,free.
If you prefer to use C++, you can use all the same functions in a
namespace instead, do this:
#define GB_STRING_CPP
before you include the header file
i.e it should look like this:
#define GB_STRING_CPP
#include "gb_string.h"
The C++ version has the advantage that you do not need to reassign variables
i.e.
C version
str = gb_append_cstring(str, "another string");
C++ version
gb::append_cstring(str, "another string");
This could be achieved in C by passing a pointer to the string but for
simplicity and consistency, reassigning the variable is better.
Reasoning:
By default, strings in C are null terminated which means you have to count
the number of character up to the null character to calculate the length.
Many "better" C string libraries will create a struct for a string.
i.e.
struct String {
size_t length;
size_t capacity;
char * cstring;
};
This library tries to augment normal C strings in a better way that is still
compatible with C-style strings.
+--------+-----------------------+-----------------+
| Header | Binary C-style String | Null Terminator |
+--------+-----------------------+-----------------+
|
+-> Pointer returned by functions
Due to the meta-data being stored before the string pointer and every gb string
having an implicit null terminator, gb strings are full compatible with c-style
strings and read-only functions.
Advantages:
* gb strings can be passed to C-style string functions without accessing a struct
member of calling a function, i.e.
printf("%s\n", gb_str);
Many other libraries do either of these:
printf("%s\n", string->cstr);
printf("%s\n", get_cstring(string));
* You can access each character just like a C-style string:
printf("%c %c\n", str[0], str[13]);
* gb strings are singularly allocated. The meta-data is next to the character
array which is better for the cache.
Disadvantages:
* In the C version of these functions, many return the new string. i.e.
str = gb_append_cstring(str, "another string");
In the C++ version, this is made easier with the use of references. i.e.
gb::append_cstring(str, "another string");
* Custom allocators must redefine GB_ALLOC and GB_FREE which can be annoying.
realloc is not used for compatibility with many custom allocators that do not
have a reallocation function.
* This is not compatible with the "gb.h" gbString. That version is a better version
as it allows for custom allocators.
*/
/* Examples: */
/* C example */
2015-09-26 05:59:05 -04:00
#if 0
2015-09-27 12:40:52 -04:00
#include <stdio.h>
#include <stdlib.h>
2015-09-26 05:59:05 -04:00
#define GB_STRING_IMPLEMENTATION
#include "gb_string.h"
2016-04-24 16:43:42 -04:00
int main(int argc, char **argv)
2015-09-26 05:59:05 -04:00
{
2016-04-24 16:43:42 -04:00
gbString str = gb_make_string("Hello");
gbString other_str = gb_make_string_length(", ", 2);
2015-09-26 05:59:05 -04:00
str = gb_append_string(str, other_str);
str = gb_append_cstring(str, "world!");
printf("%s\n", str); // Hello, world!
printf("str length = %d\n", gb_string_length(str));
str = gb_set_string(str, "Potato soup");
printf("%s\n", str); // Potato soup
str = gb_set_string(str, "Hello");
other_str = gb_set_string(other_str, "Pizza");
if (gb_strings_are_equal(str, other_str))
printf("Not called\n");
else
printf("Called\n");
str = gb_set_string(str, "Ab.;!...AHello World ??");
str = gb_trim_string(str, "Ab.;!. ?");
printf("%s\n", str); // "Hello World"
gb_free_string(str);
gb_free_string(other_str);
}
#endif
2016-04-28 10:02:07 -04:00
/* C++ example */
2015-09-26 05:59:05 -04:00
#if 0
2015-09-27 12:40:52 -04:00
#include <stdio.h>
#include <stdlib.h>
2015-09-26 05:59:05 -04:00
#define GB_STRING_CPP
#define GB_STRING_IMPLEMENTATION
#include "gb_string.h"
2016-04-24 16:43:42 -04:00
int main(int argc, char **argv)
2015-09-26 05:59:05 -04:00
{
using namespace gb;
String str = make_string("Hello");
String other_str = make_string(", ", 2);
append_string(str, other_str);
append_cstring(str, "world!");
2016-04-28 10:02:07 -04:00
printf("%s\n", str); /* Hello, world! */
2015-09-26 05:59:05 -04:00
printf("str length = %d\n", string_length(str));
set_string(str, "Potato soup");
2016-04-28 10:02:07 -04:00
printf("%s\n", str); /* Potato soup */
2015-09-26 05:59:05 -04:00
set_string(str, "Hello");
set_string(other_str, "Pizza");
if (strings_are_equal(str, other_str))
printf("Not called\n");
else
printf("Called\n");
set_string(str, "Ab.;!...AHello World ??");
trim_string(str, "Ab.;!. ?");
2016-04-28 10:02:07 -04:00
printf("%s\n", str); /* "Hello World" */
2015-09-26 05:59:05 -04:00
free_string(str);
free_string(other_str);
}
#endif
#ifndef GB_STRING_INCLUDE_GB_STRING_H
#define GB_STRING_INCLUDE_GB_STRING_H
#ifndef GB_ALLOC
#define GB_ALLOC(sz) malloc(sz)
2016-04-24 16:43:42 -04:00
#define GB_FREE(ptr) free(ptr)
2015-09-26 05:59:05 -04:00
#endif
#ifndef _MSC_VER
#ifdef __cplusplus
#define gb_inline inline
#else
#define gb_inline
#endif
#else
#define gb_inline __forceinline
#endif
2016-04-28 10:02:07 -04:00
#include <string.h> /* Needed for memcpy and cstring functions */
2015-09-26 05:59:05 -04:00
2015-09-27 05:29:13 -04:00
#ifdef __cplusplus
extern "C" {
#endif
2016-04-24 16:43:42 -04:00
typedef char *gbString;
2015-09-26 05:59:05 -04:00
2016-04-24 16:43:42 -04:00
typedef int gbBool;
2015-09-26 05:59:05 -04:00
#if !defined(GB_TRUE) || !defined(GB_FALSE)
#define GB_TRUE 1
#define GB_FALSE 0
#endif
#ifndef GB_SIZE_TYPE
#define GB_SIZE_TYPE
2016-04-24 16:43:42 -04:00
typedef size_t gbUsize;
2015-09-26 05:59:05 -04:00
#endif
#ifndef GB_NULLPTR
#if __cplusplus
#if __cplusplus >= 201103L
#define GB_NULLPTR nullptr
#else
#define GB_NULLPTR 0
#endif
#else
#define GB_NULLPTR (void*)0
#endif
#endif
2016-04-24 16:43:42 -04:00
typedef struct gbStringHeader {
gbUsize len;
gbUsize cap;
} gbStringHeader;
2015-09-26 05:59:05 -04:00
2016-04-24 16:43:42 -04:00
#define GB_STRING_HEADER(s) ((gbStringHeader *)s - 1)
2015-09-26 05:59:05 -04:00
2016-04-24 16:43:42 -04:00
gbString gb_make_string(char const *str);
gbString gb_make_string_length(void const *str, gbUsize len);
void gb_free_string(gbString str);
2015-09-26 05:59:05 -04:00
2016-04-24 16:43:42 -04:00
gbString gb_duplicate_string(gbString const str);
2015-09-26 05:59:05 -04:00
2016-04-24 16:43:42 -04:00
gbUsize gb_string_length(gbString const str);
gbUsize gb_string_capacity(gbString const str);
gbUsize gb_string_available_space(gbString const str);
2015-09-26 05:59:05 -04:00
2016-04-24 16:43:42 -04:00
void gb_clear_string(gbString str);
2015-09-26 05:59:05 -04:00
2016-04-24 16:43:42 -04:00
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);
2015-09-26 05:59:05 -04:00
2016-04-24 16:43:42 -04:00
gbString gb_set_string(gbString str, char const *cstr);
2015-09-26 05:59:05 -04:00
2016-04-24 16:43:42 -04:00
gbString gb_string_make_space_for(gbString str, gbUsize add_len);
gbUsize gb_string_allocation_size(gbString const str);
2015-09-26 05:59:05 -04:00
2016-04-24 16:43:42 -04:00
gbBool gb_strings_are_equal(gbString const lhs, gbString const rhs);
2015-09-26 05:59:05 -04:00
2016-04-24 16:43:42 -04:00
gbString gb_trim_string(gbString str, char const *cut_set);
2015-09-26 05:59:05 -04:00
2015-09-27 05:29:13 -04:00
#ifdef __cplusplus
}
#endif
2015-09-27 12:40:52 -04:00
#if defined(GB_STRING_CPP)
#if !defined(__cplusplus)
#error You need to compile as C++ for the C++ version of gb_string.h to work
#endif
namespace gb
{
2016-04-24 16:43:42 -04:00
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); }
2016-04-28 10:02:07 -04:00
} /* namespace gb */
#endif /* GB_STRING_CPP */
#endif /* GB_STRING_H */
2015-09-26 05:59:05 -04:00
#ifdef GB_STRING_IMPLEMENTATION
2016-04-24 16:43:42 -04:00
static void
gb_set_string_length(gbString str, gbUsize len)
2015-09-26 05:59:05 -04:00
{
GB_STRING_HEADER(str)->len = len;
}
2016-04-24 16:43:42 -04:00
static void
gb_set_string_capacity(gbString str, gbUsize cap)
2015-09-26 05:59:05 -04:00
{
GB_STRING_HEADER(str)->cap = cap;
}
2016-04-24 16:43:42 -04:00
gbString
gb_make_string_length(void const *init_str, gbUsize len)
2015-09-26 05:59:05 -04:00
{
2016-04-24 16:43:42 -04:00
gbString str;
gbStringHeader *header;
gbUsize header_size = sizeof(gbStringHeader);
void *ptr = GB_ALLOC(header_size + len + 1);
2015-09-26 05:59:05 -04:00
if (!init_str)
memset(ptr, 0, header_size + len + 1);
if (ptr == GB_NULLPTR)
return GB_NULLPTR;
2016-04-24 16:43:42 -04:00
str = (char *)ptr + header_size;
header = GB_STRING_HEADER(str);
2015-09-26 05:59:05 -04:00
header->len = len;
header->cap = len;
if (len && init_str)
memcpy(str, init_str, len);
str[len] = '\0';
return str;
}
2016-04-24 16:43:42 -04:00
gbString
gb_make_string(char const *str)
2015-09-26 05:59:05 -04:00
{
2016-04-24 16:43:42 -04:00
gbUsize len = str ? strlen(str) : 0;
2015-09-26 05:59:05 -04:00
return gb_make_string_length(str, len);
}
2016-04-24 16:43:42 -04:00
void
gb_free_string(gbString str)
2015-09-26 05:59:05 -04:00
{
if (str == GB_NULLPTR)
return;
2016-04-24 16:43:42 -04:00
GB_FREE((gbStringHeader *)str - 1);
2015-09-26 05:59:05 -04:00
}
2016-04-24 16:43:42 -04:00
gbString
gb_duplicate_string(gbString const str)
2015-09-26 05:59:05 -04:00
{
return gb_make_string_length(str, gb_string_length(str));
}
2016-04-24 16:43:42 -04:00
gbUsize
gb_string_length(gbString const str)
2015-09-26 05:59:05 -04:00
{
return GB_STRING_HEADER(str)->len;
}
2016-04-24 16:43:42 -04:00
gbUsize
gb_string_capacity(gbString const str)
2015-09-26 05:59:05 -04:00
{
return GB_STRING_HEADER(str)->cap;
}
2016-04-24 16:43:42 -04:00
gbUsize
gb_string_available_space(gbString const str)
2015-09-26 05:59:05 -04:00
{
2016-04-24 16:43:42 -04:00
gbStringHeader *h = GB_STRING_HEADER(str);
2015-09-26 05:59:05 -04:00
if (h->cap > h->len)
return h->cap - h->len;
return 0;
}
2016-04-24 16:43:42 -04:00
void
gb_clear_string(gbString str)
2015-09-26 05:59:05 -04:00
{
gb_set_string_length(str, 0);
str[0] = '\0';
}
2016-04-24 16:43:42 -04:00
gbString gb_append_string_length(gbString str, void const *other, gbUsize other_len)
2015-09-26 05:59:05 -04:00
{
2016-04-24 16:43:42 -04:00
gbUsize curr_len = gb_string_length(str);
2015-09-26 05:59:05 -04:00
str = gb_string_make_space_for(str, other_len);
if (str == GB_NULLPTR)
return GB_NULLPTR;
memcpy(str + curr_len, other, other_len);
str[curr_len + other_len] = '\0';
gb_set_string_length(str, curr_len + other_len);
return str;
}
2016-04-24 16:43:42 -04:00
gbString gb_append_string(gbString str, gbString const other)
2015-09-26 05:59:05 -04:00
{
return gb_append_string_length(str, other, gb_string_length(other));
}
2016-04-24 16:43:42 -04:00
gbString gb_append_cstring(gbString str, char const *other)
2015-09-26 05:59:05 -04:00
{
return gb_append_string_length(str, other, strlen(other));
}
2016-04-24 16:43:42 -04:00
gbString gb_set_string(gbString str, char const *cstr)
2015-09-26 05:59:05 -04:00
{
2016-04-24 16:43:42 -04:00
gbUsize len = strlen(cstr);
if (gb_string_capacity(str) < len) {
2015-09-26 05:59:05 -04:00
str = gb_string_make_space_for(str, len - gb_string_length(str));
if (str == GB_NULLPTR)
return GB_NULLPTR;
}
memcpy(str, cstr, len);
str[len] = '\0';
gb_set_string_length(str, len);
return str;
}
2016-04-24 16:43:42 -04:00
static gb_inline void *gb__string_realloc(void *ptr, gbUsize old_size, gbUsize new_size)
2015-09-26 05:59:05 -04:00
{
2016-04-24 16:43:42 -04:00
void *new_ptr;
2015-09-26 05:59:05 -04:00
if (!ptr)
return GB_ALLOC(new_size);
if (new_size < old_size)
new_size = old_size;
if (old_size == new_size)
return ptr;
2016-04-24 16:43:42 -04:00
new_ptr = GB_ALLOC(new_size);
2015-09-26 05:59:05 -04:00
if (!new_ptr)
return GB_NULLPTR;
memcpy(new_ptr, ptr, old_size);
GB_FREE(ptr);
return new_ptr;
}
2016-04-24 16:43:42 -04:00
gbString gb_string_make_space_for(gbString str, gbUsize add_len)
2015-09-26 05:59:05 -04:00
{
2016-04-24 16:43:42 -04:00
gbUsize len = gb_string_length(str);
gbUsize new_len = len + add_len;
void *ptr, *new_ptr;
gbUsize available, old_size, new_size;
2015-09-26 05:59:05 -04:00
2016-04-24 16:43:42 -04:00
available = gb_string_available_space(str);
2016-04-28 10:02:07 -04:00
if (available >= add_len) /* Return if there is enough space left */
2015-09-26 05:59:05 -04:00
return str;
2016-04-24 16:43:42 -04:00
ptr = (char *)str - sizeof(gbStringHeader);
old_size = sizeof(gbStringHeader) + gb_string_length(str) + 1;
new_size = sizeof(gbStringHeader) + new_len + 1;
2015-09-26 05:59:05 -04:00
2016-04-24 16:43:42 -04:00
new_ptr = gb__string_realloc(ptr, old_size, new_size);
2015-09-26 05:59:05 -04:00
if (new_ptr == GB_NULLPTR)
return GB_NULLPTR;
2016-04-24 16:43:42 -04:00
str = (char *)new_ptr + sizeof(gbStringHeader);
2015-09-26 05:59:05 -04:00
gb_set_string_capacity(str, new_len);
return str;
}
2016-04-24 16:43:42 -04:00
gbUsize gb_string_allocation_size(gbString const s)
2015-09-26 05:59:05 -04:00
{
2016-04-24 16:43:42 -04:00
gbUsize cap = gb_string_capacity(s);
return sizeof(gbStringHeader) + cap;
2015-09-26 05:59:05 -04:00
}
2016-04-24 16:43:42 -04:00
gbBool gb_strings_are_equal(gbString const lhs, gbString const rhs)
2015-09-26 05:59:05 -04:00
{
2016-04-24 16:43:42 -04:00
gbUsize lhs_len, rhs_len, i;
lhs_len = gb_string_length(lhs);
rhs_len = gb_string_length(rhs);
2015-09-26 05:59:05 -04:00
if (lhs_len != rhs_len)
return GB_FALSE;
2016-04-24 16:43:42 -04:00
for (i = 0; i < lhs_len; i++) {
2015-09-26 05:59:05 -04:00
if (lhs[i] != rhs[i])
return GB_FALSE;
}
return GB_TRUE;
}
2016-04-24 16:43:42 -04:00
gbString gb_trim_string(gbString str, char const *cut_set)
2015-09-26 05:59:05 -04:00
{
2016-04-24 16:43:42 -04:00
char *start, *end, *start_pos, *end_pos;
gbUsize len;
2015-09-26 05:59:05 -04:00
start_pos = start = str;
end_pos = end = str + gb_string_length(str) - 1;
while (start_pos <= end && strchr(cut_set, *start_pos))
start_pos++;
while (end_pos > start_pos && strchr(cut_set, *end_pos))
end_pos--;
2016-04-24 16:43:42 -04:00
len = (start_pos > end_pos) ? 0 : ((end_pos - start_pos)+1);
2015-09-26 05:59:05 -04:00
if (str != start_pos)
memmove(str, start_pos, len);
str[len] = '\0';
gb_set_string_length(str, len);
return str;
}
2016-04-28 10:02:07 -04:00
#endif /* GB_STRING_IMPLEMENTATION */
2015-09-26 05:59:05 -04:00