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