/* This software is licensed by the MIT License, see LICENSE file */ /* Copyright © 2022 Gregory Lirent */ #include "include.h" size_t string_nmemb(const str_t* s) { return (!is_null(s->buffer)) ? strlen(s->buffer) : 0; } size_t string_size(const str_t* s) { size_t n; char* p; if (is_null(s->buffer) || !*s->buffer) return 0; n = strasciilen(s->buffer); p = s->buffer + n; while (*p) { p = next_char(p); ++n; } return n; } void string_init(str_t* x, const char* s) { size_t n = (!is_null(s)) ? strlen(s) : 0; if (n) x->buffer = strndup(s, n); else memset(x, 0, sizeof(*x)); } void string_free(str_t* x) { free(x->buffer); memset(x, 0, sizeof(*x)); } int string_compare(const str_t* s0, const str_t* s1) { ssize_t n0, n1; if (s0 == s1) return 0; n0 = (!is_null(s0->buffer)) ? strlen(s0->buffer) : 0; n1 = (!is_null(s1->buffer)) ? strlen(s1->buffer) : 0; n0 -= n1; if (n0 || !n1) return n0; return memcmp(s0->buffer, s1->buffer, n1); } /*#####################################################################################################################*/ bool libcdsb_string_concat_cstring(str_t* x, const char* s) { size_t n; size_t xn; if ((n = (!is_null(s)) ? strlen(s) : 0)) { xn = (!is_null(x->buffer)) ? strlen(x->buffer) : 0; x->buffer = realloc(x->buffer, xn + ++n); memcpy(x->buffer + xn, s, n); return true; } return false; } bool libcdsb_string_concat_char(str_t* x, int chr) { size_t xn; size_t n; char *e; char s[5] = {0}; if (!is_null(e = tochar_unicode(s, chr))) { xn = (!is_null(x->buffer)) ? strlen(x->buffer) : 0; n = e - s; x->buffer = realloc(x->buffer, xn + ++n); memcpy(x->buffer + xn, s, n); return true; } return false; } /*#####################################################################################################################*/ str_t string_copy(const str_t* s) { str_t x = { .buffer = 0 }; size_t n = (!is_null(s->buffer)) ? strlen(s->buffer) : 0; if (n) x.buffer = strndup(s->buffer, n); return x; } str_t* string_duplicate(const str_t* s) { str_t* x = calloc(sizeof(*x), 1); size_t n = (!is_null(s->buffer)) ? strlen(s->buffer) : 0; if (n) x->buffer = strndup(s->buffer, n); return x; } void string_copy_init(str_t* x, const str_t* s) { size_t n = (!is_null(s->buffer)) ? strlen(s->buffer) : 0; if (n) x->buffer = strndup(s->buffer, n); else memset(x, 0, sizeof(*x)); }