123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- /******************************************************************************
- Copyright (c) 2013 by Hugh Bailey <[email protected]>
- This software is provided 'as-is', without any express or implied
- warranty. In no event will the authors be held liable for any damages
- arising from the use of this software.
- Permission is granted to anyone to use this software for any purpose,
- including commercial applications, and to alter it and redistribute it
- freely, subject to the following restrictions:
- 1. The origin of this software must not be misrepresented; you must not
- claim that you wrote the original software. If you use this software
- in a product, an acknowledgment in the product documentation would be
- appreciated but is not required.
- 2. Altered source versions must be plainly marked as such, and must not be
- misrepresented as being the original software.
- 3. This notice may not be removed or altered from any source
- distribution.
- ******************************************************************************/
- #ifndef DSTR_H
- #define DSTR_H
- #include <string.h>
- #include <stdarg.h>
- #include "c99defs.h"
- #include "bmem.h"
- /*
- * Dynamic string
- *
- * Helper struct/functions for dynamically sizing string buffers.
- */
- #ifdef __cplusplus
- extern "C" {
- #endif
- struct strref;
- struct dstr {
- char *array;
- size_t len; /* number of characters, excluding null terminator */
- size_t capacity;
- };
- EXPORT int astrcmpi(const char *str1, const char *str2);
- EXPORT int wstrcmpi(const wchar_t *str1, const wchar_t *str2);
- EXPORT int astrcmp_n(const char *str1, const char *str2, size_t n);
- EXPORT int wstrcmp_n(const wchar_t *str1, const wchar_t *str2, size_t n);
- EXPORT int astrcmpi_n(const char *str1, const char *str2, size_t n);
- EXPORT int wstrcmpi_n(const wchar_t *str1, const wchar_t *str2, size_t n);
- EXPORT char *strdepad(char *str);
- EXPORT wchar_t *wcsdepad(wchar_t *str);
- static inline void dstr_init(struct dstr *dst);
- static inline void dstr_init_move(struct dstr *dst, struct dstr *src);
- static inline void dstr_init_move_array(struct dstr *dst, char *str);
- static inline void dstr_init_copy(struct dstr *dst, const char *src);
- static inline void dstr_init_copy_dstr(struct dstr *dst,
- const struct dstr *src);
- EXPORT void dstr_init_strref(struct dstr *dst, const struct strref *src);
- static inline void dstr_free(struct dstr *dst);
- static inline void dstr_array_free(struct dstr *array, const size_t count);
- static inline void dstr_move(struct dstr *dst, struct dstr *src);
- static inline void dstr_move_array(struct dstr *dst, char *str);
- EXPORT void dstr_copy(struct dstr *dst, const char *array);
- static inline void dstr_copy_dstr(struct dstr *dst, const struct dstr *src);
- EXPORT void dstr_copy_strref(struct dstr *dst, const struct strref *src);
- EXPORT void dstr_ncopy(struct dstr *dst, const char *array,
- const size_t len);
- EXPORT void dstr_ncopy_dstr(struct dstr *dst, const struct dstr *src,
- const size_t len);
- static inline void dstr_resize(struct dstr *dst, const size_t num);
- static inline void dstr_reserve(struct dstr *dst, const size_t num);
- static inline bool dstr_isempty(const struct dstr *str);
- static inline void dstr_cat(struct dstr *dst, const char *array);
- EXPORT void dstr_cat_dstr(struct dstr *dst, const struct dstr *str);
- EXPORT void dstr_cat_strref(struct dstr *dst, const struct strref *str);
- static inline void dstr_cat_ch(struct dstr *dst, char ch);
- EXPORT void dstr_ncat(struct dstr *dst, const char *array, const size_t len);
- EXPORT void dstr_ncat_dstr(struct dstr *dst, const struct dstr *str,
- const size_t len);
- EXPORT void dstr_cat_strref(struct dstr *dst, const struct strref *str);
- EXPORT void dstr_insert(struct dstr *dst, const size_t idx,
- const char *array);
- EXPORT void dstr_insert_dstr(struct dstr *dst, const size_t idx,
- const struct dstr *str);
- EXPORT void dstr_insert_ch(struct dstr *dst, const size_t idx,
- const char ch);
- EXPORT void dstr_remove(struct dstr *dst, const size_t idx, const size_t count);
- EXPORT void dstr_printf(struct dstr *dst, const char *format, ...);
- EXPORT void dstr_catf(struct dstr *dst, const char *format, ...);
- EXPORT void dstr_vprintf(struct dstr *dst, const char *format, va_list args);
- EXPORT void dstr_vcatf(struct dstr *dst, const char *format, va_list args);
- EXPORT void dstr_safe_printf(struct dstr *dst, const char *format,
- const char *val1, const char *val2, const char *val3,
- const char *val4);
- static inline const char *dstr_find(const struct dstr *str,
- const char *find);
- EXPORT void dstr_replace(struct dstr *str, const char *find,
- const char *replace);
- static inline int dstr_cmp(const struct dstr *str1, const char *str2);
- static inline int dstr_cmpi(const struct dstr *str1, const char *str2);
- static inline int dstr_ncmp(const struct dstr *str1, const char *str2,
- const size_t n);
- static inline int dstr_ncmpi(const struct dstr *str1, const char *str2,
- const size_t n);
- EXPORT void dstr_depad(struct dstr *dst);
- EXPORT void dstr_left(struct dstr *dst, const struct dstr *str,
- const size_t pos);
- EXPORT void dstr_mid(struct dstr *dst, const struct dstr *str,
- const size_t start, const size_t count);
- EXPORT void dstr_right(struct dstr *dst, const struct dstr *str,
- const size_t pos);
- EXPORT void dstr_from_mbs(struct dstr *dst, const char *mbstr);
- EXPORT char *dstr_to_mbs(const struct dstr *str);
- EXPORT void dstr_from_wcs(struct dstr *dst, const wchar_t *wstr);
- EXPORT wchar_t *dstr_to_wcs(const struct dstr *str);
- /* ------------------------------------------------------------------------- */
- static inline void dstr_init(struct dstr *dst)
- {
- dst->array = NULL;
- dst->len = 0;
- dst->capacity = 0;
- }
- static inline void dstr_init_move_array(struct dstr *dst, char *str)
- {
- dst->array = str;
- dst->len = (!str) ? 0 : strlen(str);
- }
- static inline void dstr_init_move(struct dstr *dst, struct dstr *src)
- {
- *dst = *src;
- dstr_init(src);
- }
- static inline void dstr_init_copy(struct dstr *dst, const char *str)
- {
- dstr_init(dst);
- dstr_copy(dst, str);
- }
- static inline void dstr_init_copy_dstr(struct dstr *dst, const struct dstr *src)
- {
- dstr_init(dst);
- dstr_copy_dstr(dst, src);
- }
- static inline void dstr_free(struct dstr *dst)
- {
- bfree(dst->array);
- dst->array = NULL;
- dst->len = 0;
- dst->capacity = 0;
- }
- static inline void dstr_array_free(struct dstr *array, const size_t count)
- {
- size_t i;
- for (i = 0; i < count; i++)
- dstr_free(array+i);
- }
- static inline void dstr_move_array(struct dstr *dst, char *str)
- {
- dstr_free(dst);
- dst->array = str;
- dst->len = (!str) ? 0 : strlen(str);
- }
- static inline void dstr_move(struct dstr *dst, struct dstr *src)
- {
- dstr_free(dst);
- dstr_init_move(dst, src);
- }
- static inline void dstr_ensure_capacity(struct dstr *dst, const size_t new_size)
- {
- size_t new_cap;
- if (new_size <= dst->capacity)
- return;
- new_cap = (!dst->capacity) ? new_size : dst->capacity*2;
- if (new_size > new_cap)
- new_cap = new_size;
- dst->array = (char*)brealloc(dst->array, new_cap);
- dst->capacity = new_cap;
- }
- static inline void dstr_copy_dstr(struct dstr *dst, const struct dstr *src)
- {
- if (dst->array)
- dstr_free(dst);
- dstr_ensure_capacity(dst, src->len + 1);
- memcpy(dst->array, src->array, src->len + 1);
- dst->len = src->len;
- }
- static inline void dstr_reserve(struct dstr *dst, const size_t capacity)
- {
- if (capacity == 0 || capacity <= dst->len)
- return;
- dst->array = (char*)brealloc(dst->array, capacity);
- dst->capacity = capacity;
- }
- static inline void dstr_resize(struct dstr *dst, const size_t num)
- {
- if (!num) {
- dstr_free(dst);
- return;
- }
- dstr_ensure_capacity(dst, num + 1);
- dst->array[num] = 0;
- dst->len = num;
- }
- static inline bool dstr_isempty(const struct dstr *str)
- {
- if (!str->array || !str->len)
- return true;
- if (!*str->array)
- return true;
- return false;
- }
- static inline void dstr_cat(struct dstr *dst, const char *array)
- {
- size_t len;
- if (!array || !*array)
- return;
- len = strlen(array);
- dstr_ncat(dst, array, len);
- }
- static inline void dstr_cat_ch(struct dstr *dst, char ch)
- {
- dstr_ensure_capacity(dst, ++dst->len + 1);
- dst->array[dst->len-1] = ch;
- dst->array[dst->len] = 0;
- }
- static inline const char *dstr_find(const struct dstr *str,
- const char *find)
- {
- return strstr(str->array, find);
- }
- static inline int dstr_cmp(const struct dstr *str1, const char *str2)
- {
- return strcmp(str1->array, str2);
- }
- static inline int dstr_cmpi(const struct dstr *str1, const char *str2)
- {
- return astrcmpi(str1->array, str2);
- }
- static inline int dstr_ncmp(const struct dstr *str1, const char *str2,
- const size_t n)
- {
- return astrcmp_n(str1->array, str2, n);
- }
- static inline int dstr_ncmpi(const struct dstr *str1, const char *str2,
- const size_t n)
- {
- return astrcmpi_n(str1->array, str2, n);
- }
- #ifdef __cplusplus
- }
- #endif
- #endif
|