/* This software is licensed by the MIT License, see LICENSE file */ /* Copyright © 2022 Gregory Lirent */ #include "include.h" char* string_at(const str_t* s, ssize_t i) { char *e, *p; size_t n, l; if (is_null(s->buffer) || !*s->buffer) return nullptr; n = strasciilen(s->buffer); e = s->buffer + n; if (i > n) { p = s->buffer + n; e = p + strlen(p); do { p = next_char(p); } while (--i && p < e); return (!i) ? p : nullptr; } else if (i < 0 && n < (l = strlen(s->buffer))) { p = s->buffer + l; do { p = prev_char(p); } while (++i && p >= s->buffer); return (!i) ? p : nullptr; } else if (i < 0 && (i += l) < 0) i = 0; return s->buffer + i; } _Bool string_slice(str_t* x, str_t* s, ssize_t i, size_t c, _Bool cut) { char *e, *p, *v; memset(x, 0, sizeof(*x)); if (!c) return true; p = string_at(s, i); if (is_null(p) || (e = p + strlen(p)) > p + c) return false; v = p; do { v = next_char(v); } while (--c && v < e); if (!c) { x->buffer = strndup(p, v - p); if (cut) { memmove(p, v, strlen(v) + 1); } return true; } else return false; } /*#####################################################################################################################*/ ssize_t libcdsb_string_indexof_cstring(const str_t* s, const char* a) { char *e, *p; size_t n; if (is_null(s->buffer) || is_null(a) || !*s->buffer || !*a) { return 0; } if (!is_null(p = strstr(s->buffer, a))) { n = strasciilen(s->buffer); e = s->buffer + n; if (e >= p) return p - s->buffer; do { e = next_char(e); ++n; } while (e < p); if (e != p) { /* Trying to find index of inconsistent string part * It is not make a sense on that abstract level */ } else return n; } return -1; } ssize_t libcdsb_string_indexof_char(const str_t* s, int ac) { size_t n; char* e; char a[5] = { 0 }; char* p = tochar_unicode(a, ac); if (is_null(s->buffer) || !*s->buffer || is_null(p)) { return 0; } if (!is_null(p = strstr(s->buffer, a))) { n = strasciilen(s->buffer); e = s->buffer + n; if (e >= p) return p - s->buffer; do { e = next_char(e); ++n; } while (e < p); if (e != p) { /* Trying to find index of inconsistent string part * It is not make a sense on that abstract level */ } else return n; } return -1; } /*#####################################################################################################################*/ size_t libcdsb_string_count_cstring(const str_t* s, const char* a) { char* p; size_t n, c; if (is_null(s->buffer) || is_null(a) || !*s->buffer || !*a) { return 0; } n = strlen(a); p = s->buffer; c = 0; while (!is_null(p = strstr(p, a))) { p += n; ++c; } return c; } size_t libcdsb_string_count_char(const str_t* s, int ac) { size_t n, c; char a[5] = {0}; char* p = tochar_unicode(a, ac); if (is_null(s->buffer) || !*s->buffer || is_null(p)) { return 0; } n = p - a; p = s->buffer; c = 0; while (!is_null(p = strstr(p, a))) { p += n; ++c; } return c; }