diff options
| author | Ginger Bill <bill@gingerbill.org> | 2016-09-23 14:59:58 +0100 |
|---|---|---|
| committer | Ginger Bill <bill@gingerbill.org> | 2016-09-23 14:59:58 +0100 |
| commit | a31bab5aae10757f5029b00e39beb0e3815b92b1 (patch) | |
| tree | b7d2f888cc553f4d6e4b47ff1470c303c5d05c7e /src/string.cpp | |
| parent | ee0aa7b9de907e70e00ca3ab891087c2ee86a31b (diff) | |
Unicode file loading; push_allocator & push_context
Diffstat (limited to 'src/string.cpp')
| -rw-r--r-- | src/string.cpp | 92 |
1 files changed, 91 insertions, 1 deletions
diff --git a/src/string.cpp b/src/string.cpp index 3843fa676..6a08c5aa4 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -1,13 +1,26 @@ +gb_global gbArena string_buffer_arena = {}; +gb_global gbAllocator string_buffer_allocator = {}; + +void init_string_buffer_memory() { + // NOTE(bill): This should be enough memory for file systems + gb_arena_init_from_allocator(&string_buffer_arena, gb_heap_allocator(), gb_megabytes(1)); + string_buffer_allocator = gb_arena_allocator(&string_buffer_arena); +} + // NOTE(bill): Used for UTF-8 strings typedef struct String { - u8 *text; + u8 * text; isize len; } String; // NOTE(bill): used for printf style arguments #define LIT(x) (x).len, (x).text +typedef struct String16 { + wchar_t *text; + isize len; +} String16; gb_inline String make_string(u8 *text, isize len) { @@ -20,6 +33,15 @@ gb_inline String make_string(u8 *text, isize len) { return s; } + +gb_inline String16 make_string16(wchar_t *text, isize len) { + String16 s; + s.text = text; + s.len = len; + return s; +} + + gb_inline String make_string(char *text) { return make_string(cast(u8 *)cast(void *)text, gb_strlen(text)); } @@ -133,6 +155,74 @@ b32 string_contains_char(String s, u8 c) { return false; } +// TODO(bill): Make this non-windows specific +String16 string_to_string16(gbAllocator a, String s) { + if (s.len < 1) { + return make_string16(NULL, 0); + } + + int len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, + cast(char *)s.text, s.len, NULL, 0); + if (len == 0) { + return make_string16(NULL, 0); + } + + wchar_t *text = gb_alloc_array(a, wchar_t, len+1); + + int len1 = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, + cast(char *)s.text, s.len, text, len); + if (len1 == 0) { + gb_free(a, text); + return make_string16(NULL, 0); + } + text[len] = 0; + + return make_string16(text, len-1); +} + +String string16_to_string(gbAllocator a, String16 s) { + if (s.len < 1) { + return make_string(NULL, 0); + } + + int len = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, + s.text, s.len, NULL, 0, + NULL, NULL); + if (len == 0) { + return make_string(NULL, 0); + } + + u8 *text = gb_alloc_array(a, u8, len+1); + + int len1 = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, + s.text, s.len, cast(char *)text, len, + NULL, NULL); + if (len1 == 0) { + gb_free(a, text); + return make_string(NULL, 0); + } + text[len] = 0; + + return make_string(text, len-1); +} + + + + + + + + + + + + + + + + + + b32 unquote_char(String s, u8 quote, Rune *rune, b32 *multiple_bytes, String *tail_string) { if (s.text[0] == quote && (quote == '$' || quote == '"')) { |