libcdsb/src/string/compute.c
2022-08-22 12:13:20 +03:00

61 lines
1.0 KiB
C

/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "include.h"
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;
}
hash_t string_hash(const str_t* s) {
hash_t hash = 0;
size_t nmemb = string_nmemb(s);
if (nmemb > 0)
hash = s->buffer[0];
if (nmemb > 1)
hash += s->buffer[nmemb-1];
hash ^= nmemb;
return hash + VTYPE_STRING;
}
size_t libcdsb_string_count(const str_t* x, const char* s, size_t sn) {
char* p;
size_t c;
if (is_null(x->buffer) || is_null(s) || !*x->buffer || !*s) {
return 0;
}
if (!sn) sn = strlen(s);
p = x->buffer;
c = 0;
while (!is_null(p = strstr(p, s))) {
p += sn;
++c;
}
return c;
}