string: fix, make it compile on non-SSE2 machines
[libjh.git] / string.c
1 // Copyright (2013) Jann Horn <jann@thejh.net>
2 // This code is licensed under the AGPLv3.
3
4 #include <string.h>
5 HEADER #include <stdint.h>
6
7 HEADER #define streq(a,b) (!strcmp((a),(b)))
8
9 HEADER #define TPRINTF(name, ...)                                               \
10 HEADER   char *name;                                                            \
11 HEADER   do {                                                                   \
12 HEADER     int __tprintf_size = snprintf(NULL, 0, __VA_ARGS__);                 \
13 HEADER     assert(__tprintf_size != -1);                                        \
14 HEADER     name = alloca(__tprintf_size+1);                                     \
15 HEADER     int __tprintf_size2 = snprintf(name, __tprintf_size+1, __VA_ARGS__); \
16 HEADER     assert(__tprintf_size == __tprintf_size2);                           \
17 HEADER   } while (0); //////////////////////////////////////////////////////////
18
19 PUBLIC_FN int count_char_occurences(char *s, char c) {
20   int n=0;
21   while (*s) {
22     if (*s==c) n++;
23     s++;
24   }
25   return n;
26 }
27
28 // For big buffers.
29 PUBLIC_FN size_t count_char_occurences_in_buf(char *b, size_t bl, char c) {
30   char *be = b+bl;
31   size_t res = 0;
32   
33   #ifdef __SSE2__
34   #include <emmintrin.h>
35   
36   // do it the simple way until we get to the next 16-byte-aligned address
37   while ((((uint64_t)b)&0xf) && b<be) if (*(b++)==c) res++;
38   
39   // the aligned end is the last 8-byte-aligned byte IN this buffer
40   char *bea = (char *) (((uint64_t)be-1)&~0xf);
41   // prepare a 128-bit value that contains 16 times `c`
42   __m128i cx;
43   memset(&cx, c, 16);
44   // we have an 16-byte-aligned buffer ready – let's do it!
45   __m128i *bi = (__m128i *)b;
46   while (((char*)bi)<bea) {
47     // This intrinsic does a byte-wise compare, storing the results byte-wise,
48     // too. 0xff means equal, 0x00 means not equal.
49     __m128i r = _mm_cmpeq_epi8(cx, *bi);
50     int64_t *r_64 = (int64_t*)&r;
51     if ((r_64[0]|r_64[1])) {
52       char *r_8 = (char *)&r;
53       // we have at least one hit in those 16 chars. narrow it down to eight,
54       // then check those eight
55       if (r_64[0]) {
56         if (r_8[ 0]) res++;   if (r_8[ 1]) res++;
57         if (r_8[ 2]) res++;   if (r_8[ 3]) res++;
58         if (r_8[ 4]) res++;   if (r_8[ 5]) res++;
59         if (r_8[ 6]) res++;   if (r_8[ 7]) res++;
60       }
61       if (r_64[1]) {
62         if (r_8[ 8]) res++;   if (r_8[ 9]) res++;
63         if (r_8[10]) res++;   if (r_8[11]) res++;
64         if (r_8[12]) res++;   if (r_8[13]) res++;
65         if (r_8[14]) res++;   if (r_8[15]) res++;
66       }
67     }
68     bi++;
69   }
70   
71   // do the last few bytes the slow way, too
72   b = (char *)bi;
73   #endif
74   
75   // this is also the fallback in case the CPU can't do this
76   while (b<be) if (*(b++)==c) res++;
77   
78   return res;
79 }
80
81 // For big buffers.
82 PUBLIC_FN int count_and_replace_char_occurences_in_buf(char *b, size_t bl, char c, char new_c) {
83   char *be = b+bl;
84   int res = 0;
85   
86   #ifdef __SSE2__
87   #include <emmintrin.h>
88   
89   // do it the simple way until we get to the next 16-byte-aligned address
90   while ((((uint64_t)b)&0xf) && b<be) if (*(b++)==c) res++;
91   
92   // the aligned end is the last 8-byte-aligned byte IN this buffer
93   char *bea = (char *) (((uint64_t)be-1)&~0xf);
94   // prepare a 128-bit value that contains 16 times `c`
95   __m128i cx;
96   memset(&cx, c, 16);
97   // we have an 16-byte-aligned buffer ready – let's do it!
98   __m128i *bi = (__m128i *)b;
99   while (((char*)bi)<bea) {
100     // This intrinsic does a byte-wise compare, storing the results byte-wise,
101     // too. 0xff means equal, 0x00 means not equal.
102     __m128i r = _mm_cmpeq_epi8(cx, *bi);
103     int64_t *r_64 = (int64_t*)&r;
104     if ((r_64[0]|r_64[1])) {
105       char *r_8 = (char *)&r;
106       // we have at least one hit in those 16 chars. narrow it down to eight,
107       // then check those eight
108       if (r_64[0]) {
109         if (r_8[ 0]) r_8[ 0]=new_c, res++;   if (r_8[ 1]) r_8[ 1]=new_c, res++;
110         if (r_8[ 2]) r_8[ 2]=new_c, res++;   if (r_8[ 3]) r_8[ 3]=new_c, res++;
111         if (r_8[ 0]) r_8[ 4]=new_c, res++;   if (r_8[ 1]) r_8[ 5]=new_c, res++;
112         if (r_8[ 2]) r_8[ 6]=new_c, res++;   if (r_8[ 3]) r_8[ 7]=new_c, res++;
113       }
114       if (r_64[1]) {
115         if (r_8[ 8]) r_8[ 8]=new_c, res++;   if (r_8[ 9]) r_8[ 9]=new_c, res++;
116         if (r_8[10]) r_8[10]=new_c, res++;   if (r_8[11]) r_8[11]=new_c, res++;
117         if (r_8[12]) r_8[12]=new_c, res++;   if (r_8[13]) r_8[13]=new_c, res++;
118         if (r_8[14]) r_8[14]=new_c, res++;   if (r_8[15]) r_8[15]=new_c, res++;
119       }
120     }
121     bi++;
122   }
123   
124   // do the last few bytes the slow way, too
125   b = (char *)bi;
126   #endif
127   
128   // this is also the fallback in case the CPU can't do this
129   while (b<be) if (*(b++)==c) res++;
130   
131   return res;
132 }
133
134 // memcpy plus terminating nullbyte
135 PUBLIC_FN void *memcpyn(void *d, const void *s, size_t n) {
136   memcpy(d, s, n);
137   char *d_ = d;
138   d_[n] = '\0';
139   return d;
140 }
141
142 // Wipe out whitespace characters at the end of str using nullbytes.
143 PUBLIC_FN void trim_end(char *str, char *whitespace) {
144   for (char *p = str+strlen(str)-1; p>=str; p--) {
145     if (!strchr(whitespace, *p)) break;
146     *p = '\0';
147     p--;
148   }
149 }
150
151 PUBLIC_FN int ends_with(char *str, char *sub) {
152   size_t str_len = strlen(str);
153   size_t sub_len = strlen(sub);
154   if (sub_len>str_len) return 0;
155   return streq(str+str_len-sub_len, sub);
156 }
157
158 PUBLIC_FN char **buf_to_linearray(char *buf, ssize_t buflen) {
159   if (buflen == -1) buflen = strlen(buf);
160   size_t linecount = count_char_occurences_in_buf(buf, buflen, '\n')+1;
161   char **ret = malloc(linecount * sizeof(char*) + 1);
162   ret[linecount] = NULL;
163   if (ret == NULL) return NULL;
164   char **r = ret;
165   *(r++) = buf; /* first line starts at byte zero */
166   char *b = buf;
167   char *be = buf+buflen;
168   while (b<be) {
169     if (*b == '\n') {
170       *b = '\0';
171       *(r++) = b+1;
172     }
173     b++;
174   }
175   return ret;
176 }