X-Git-Url: http://git.thejh.net/?p=libjh.git;a=blobdiff_plain;f=string.c;h=b0db54c0fac7f8f38df1cb9071e18c11991aa2d8;hp=21ff3a745db302663cb05b04672aa3b4e4da2952;hb=bec2b86cf36b320ebbc6f30b5f803f1681726049;hpb=e3a7f339e843d1424aef577e5634ac70343ec49c diff --git a/string.c b/string.c index 21ff3a7..b0db54c 100644 --- a/string.c +++ b/string.c @@ -1,4 +1,9 @@ +// Copyright (2013) Jann Horn +// This code is licensed under the AGPLv3. + #include +#include +HEADER #include HEADER #define streq(a,b) (!strcmp((a),(b))) @@ -11,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