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