forked from cirosantilli/cpp-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_h.c
350 lines (264 loc) · 7.22 KB
/
string_h.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
# string.h
String and array operations.
# str vs mem
The `str` prefixed functions uses '\0' to see ther string ends
so callers don't need to give lengths.
Many of the functions exist both on `str` and `mem` forms,
where the `mem` form also takes a size.
*/
#include "common.h"
int main(void) {
/*
# memcpy
Copy one array into another.
Potentially faster than a for loop like:
for(i=0; i<3; i++){
is2[i] = is[i];
}
Since in some architectures this can be implemented with more efficient instructions
than a naive for, and your compiler may not be smart enough to optimize this if you use a for.
If overlap, undefined behavior. Use memmove in that case.
*/
{
{
int is[] = { 0, 1, 2 };
int is2[sizeof(is)/sizeof(is[0])];
memcpy(is2, is, sizeof(is));
assert(memcmp(is, is2, sizeof(is)) == 0);
}
#if __STDC_VERSION__ >= 199901L
/* Compound literal copy. */
{
int is[3];
memcpy(&is, &(int []){ 0, 1, 2 }, sizeof(is));
assert(memcmp(is, &(int []){ 0, 1, 2 }, sizeof(is)) == 0);
}
#endif
}
/*
# memmove
Same as memcpy, but overlap may happen, thus slower.
*/
{
int is[] = { 0, 1, 2, 3, 4, 5 };
int is2[] = { 0, 1, 0, 1, 2, 5 };
memmove(is + 2, is, 3 * sizeof(int));
assert(memcmp(is, is2, sizeof(is)) == 0);
}
/*
# strcpy
Copy one string (up to first '\0') into another location.
If they overlap, undefined behaviour.
Could be more efficient than a for loop since it could
tell the compiler to use a better specialized instruction.
*/
{
char cs[] = "abc";
char cs2[4];
char cs3[1];
strcpy(cs2, cs);
strcpy(cs2, "abc");
/* BAD: no born checking as always */
/*strcpy(cs3, "abc");*/
}
/*
# strlen
Get string length (up to first '\0').
*/
{
char cs[] = "abc";
assert(strlen(cs) == 3);
}
/*
# strncpy
strcpy with maximum chars to copy.
*/
/*
# memcmp
Compare arrays like strcmp.
# memcmp vs for loop
# strcmp vs for loop
memcmp may be is faster than for loop because
the compiler may optimize it better.
On x86, the naive optimization is:
repe cmpsb
but on GCC 4.8 for example it still uses the glibc as it is even faster!
TODO how?
One catch: float NaN.
*/
{
int is[] = { 0, 1, 2 };
int is2[] = { 0, 1, 2 };
/* Compares addresses, not data! */
assert(is != is2);
assert(memcmp(is, is2, 3 * sizeof(int)) == 0);
is[1] = 0;
assert(memcmp(is, is2, 3 * sizeof(int)) < 0);
is[1] = 2;
assert(memcmp(is, is2, 3 * sizeof(int)) > 0);
#if __STDC_VERSION__ >= 199901L
/* memcmp with compound literals. */
{
int is[] = { 2, 0, 1 };
assert(memcmp(is, &(int [3]){ 2, 0, 1 }, 3 * sizeof(int)) == 0);
}
#endif
}
/*
# strcmp
Compare two strings.
*/
{
/* Equality. */
assert(strcmp("abc", "abc") == 0);
/* Smaller. */
assert(strcmp("abc", "dbc") < 0);
/* Larger. */
assert(strcmp("abc", "aac") > 0);
/*
Different lengths. '\0' is smaller than all.
https://stackoverflow.com/questions/36518931/what-does-strcmp-return-if-two-similar-strings-are-of-different-lengths/47366149#47366149*/
assert(strcmp("a", "abc") < 0);
}
/*
# strncmp
Like strcmp, but only check at most n bytes.
*/
{
assert(strncmp("abc", "abd", 2) == 0);
/*
Different lengths: I think this is guaranteed as C99 says:
> characters that follow a null character are not compared
*/
assert(strncmp("a", "abc", 5) < 1);
}
/*
# strcat
Concatenate two strings.
*/
{
char s1[5];
strcpy(s1, "ab");
char s2[] = "cd";
strcat(s1, s2);
assert(strcmp(s1, "abcd") == 0);
assert(strcmp(s2, "cd" ) == 0);
}
/*
# memchr
mem version of strchr.
*/
/*
# strchr
Search for char in string.
Return pointer to that char if found.
Return NULL if not found.
*/
{
{
char cs[] = "abcb";
assert(strchr(cs, 'b') == cs + 1);
assert(strchr(cs, 'd') == NULL);
}
/*
Find all occurences of c in cs:
there is no direct libc function for this.
*/
{
char cs[] = "abcb";
char *cp;
char c = 'b';
int is[] = { 1, 3 };
int i = 0;
cp = strchr(cs, c);
while (cp != NULL) {
assert(cp - cs == is[i]);
cp = strchr(cp + 1, c);
++i;
}
}
}
/*
# strrchr
Find last match of character in string.
*/
{
char cs[] = "abcb";
assert(strrchr(cs, 'b') == cs + 3);
assert(strrchr(cs, 'd') == NULL);
}
/*
# strstr
Find first match of string in string.
glibc has `strcasestr` which ignores the case.
There seems to be no general array analogue: glibc has a `memmem` extension.
*/
{
char cs[] = "abcabcd";
assert(strstr(cs, "bc") == cs + 1);
assert(strstr(cs, "bd") == NULL);
}
/*
# strspn
Return the length of initial string that only contains bytes in the second argument.
Mnemonic: SPaN of character set.
# strcspn
Complement (negation) of strspn: find bytes not there.
*/
{
/* '0' is not in the accept set, so the length is 5 for "abcba". */
assert(strspn("abcba0abc", "abc") == 5);
}
/*
# split
See strtok
# strtok
Split string at a given character sequence.
http://en.cppreference.com/w/c/string/byte/strtok
*/
/*
# strerror
Returns a readonly pointer to the description of the error with the given number:
char * strerror(int errnum);
Also consider perror if you want to print those error messages to stderr.
*/
{
printf("strerror(EDOM) = \"%s\"\n", strerror(EDOM));
}
/*
# strcspn
How many characters in s1 are there before the first character present in s2.
*/
{
assert(strcspn("ab01", "10") == 2);
assert(strcspn("a0b1", "10") == 1);
}
/*
# strpbrk
Point to the first character in s1 that is in s2.
*/
{
char *s1 = "ab01";
assert(strpbrk(s1, "10") - s1 == 2);
}
/*
# memset
Set memory block to a single value.
Like memcpy, potentially more efficient than a for loop.
*/
{
char cs[] = "abcdef";
memset(cs + 2, '0', 3);
assert(strcmp(cs, "ab000f") == 0);
}
/*
# strcoll
String compare using locale.
TODO understand.
*/
{
/* TODO example */
}
return EXIT_SUCCESS;
}