GB String
GB String
Version History:
0.95b - Fix issue #21
0.95a - Change brace style because why not?
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
How to use:
Do this:
#define GB_STRING_IMPLEMENTATION
before you include this file in *one* C++ file to create the
implementation
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
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");
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;
};
+--------+-----------------------+-----------------+
| 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:
printf("%s\n", gb_str);
printf("%s\n", string->cstr);
printf("%s\n", get_cstring(string));
Disadvantages:
* In the C version of these functions, many return the new string. i.e.
In the C++ version, this is made easier with the use of references.
i.e.
gb::append_cstring(str, "another string");
*/
/* Examples: */
/* C example */
#if 0
#include <stdio.h>
#include <stdlib.h>
#define GB_STRING_IMPLEMENTATION
#include "gb_string.h"
gb_free_string(str);
gb_free_string(other_str);
}
#endif
/* C++ example */
#if 0
#include <stdio.h>
#include <stdlib.h>
#define GB_STRING_CPP
#define GB_STRING_IMPLEMENTATION
#include "gb_string.h"
set_string(str, "Hello");
set_string(other_str, "Pizza");
if (strings_are_equal(str, other_str))
printf("Not called\n");
else
printf("Called\n");
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)
#define GB_FREE(ptr) free(ptr)
#endif
#ifndef _MSC_VER
#ifdef __cplusplus
#define gb_inline inline
#else
#define gb_inline
#endif
#else
#define gb_inline __forceinline
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifndef GB_SIZE_TYPE
#define GB_SIZE_TYPE
typedef size_t gbUsize;
#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
#ifdef __cplusplus
}
#endif
#define GB_STRING_CPP
#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
{
typedef gbString String;
typedef gbUsize usize;
return str;
}
return str;
}
return str;
}
if (old_size == new_size)
return ptr;
new_ptr = GB_ALLOC(new_size);
if (!new_ptr)
return GB_NULLPTR;
GB_FREE(ptr);
return new_ptr;
}
available = gb_string_available_space(str);
if (available >= add_len) /* Return if there is enough space left */
return str;
gb_set_string_capacity(str, new_len);
return str;
}
return GB_TRUE;
}
if (str != start_pos)
memmove(str, start_pos, len);
str[len] = '\0';
gb_set_string_length(str, len);
return str;
}
#endif /* GB_STRING_IMPLEMENTATION */