X-Git-Url: http://git.thejh.net/?p=libjh.git;a=blobdiff_plain;f=string.c;h=b0db54c0fac7f8f38df1cb9071e18c11991aa2d8;hp=c91e6d221bfc2a383c1b530be256aaaafe809199;hb=c1adc4723f53f078f27951a95cd8b5aa8fd98367;hpb=3b41b5c082c2c3719fbbcfc4e381fee04879dc1d diff --git a/string.c b/string.c index c91e6d2..b0db54c 100644 --- a/string.c +++ b/string.c @@ -2,6 +2,8 @@ // This code is licensed under the AGPLv3. #include +#include +HEADER #include HEADER #define streq(a,b) (!strcmp((a),(b))) @@ -14,6 +16,112 @@ PUBLIC_FN int count_char_occurences(char *s, char c) { return n; } +// For big buffers. +PUBLIC_FN size_t count_char_occurences_in_buf(char *b, size_t bl, char c) { + char *be = b+bl; + size_t res = 0; + + #ifdef __SSE2__ + #include + + // do it the simple way until we get to the next 16-byte-aligned address + while ((((uint64_t)b)&0xf) && b + + // do it the simple way until we get to the next 16-byte-aligned address + while ((((uint64_t)b)&0xf) && bstr_len) return 0; return streq(str+str_len-sub_len, sub); } + +PUBLIC_FN char **buf_to_linearray(char *buf, ssize_t buflen) { + if (buflen == -1) buflen = strlen(buf); + size_t linecount = count_char_occurences_in_buf(buf, buflen, '\n')+1; + char **ret = malloc(linecount * sizeof(char*) + 1); + ret[linecount] = NULL; + if (ret == NULL) return NULL; + char **r = ret; + *(r++) = buf; /* first line starts at byte zero */ + char *b = buf; + char *be = buf+buflen; + while (b