/* This software is licensed by the MIT License, see LICENSE file */ /* Copyright © 2022 Gregory Lirent */ #include "include.h" char* at_string(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; } ssize_t libcdsb_string_indexof(const str_t* x, const char* s) { char *e, *p; size_t n; if (is_null(x->buffer) || is_null(s) || !*x->buffer || !*s) { return 0; } if (!is_null(p = strstr(x->buffer, s))) { n = strasciilen(x->buffer); e = x->buffer + n; if (e >= p) return p - x->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; }