forked from cirosantilli/cpp-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.c
299 lines (230 loc) · 8.21 KB
/
string.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
/*
# string
By convention, *C strings* are simply char arrays
terminated by the null character.
This convention is used throughout libc string functions,
such as `printf`, `strcmp` and others
so that you don't have to pass an additional size parameter to them
(those functions stop operating when they see the first null char).
Nothing prevents you from making a "string" that contains a null char,
except that you will break a very well stabilised convention,
and libc functions will not work properly with it...
If you absolutely need a "string" with a null char, just use regular
array functions to manipulate it, and pass string lengths around.
*/
#include "common.h"
int main(void) {
/* Basic example. */
{
char cs[] = "abc";
/* SAME: */
/*char cs[] = { 'a', 'b', 'c', '\0' }*/
assert(cs[0] == 'a' );
assert(cs[1] == 'b' );
assert(cs[2] == 'c' );
assert(cs[3] == '\0');
cs[0] = 'A';
assert(strcmp(cs, "Abc") == 0);
/* ERROR: you cannot assign a string to memory like this, */
/* except at initialization */
/*cs = "Abc";*/
/* You probably want `strcpy`. */
}
/*
# Iterate string
*/
{
/* Pointer version. */
{
char s[] = "abc";
char s2[] = "ABC";
char *cPtr;
for (cPtr = s; *cPtr != '\0'; cPtr++)
*cPtr = toupper(*cPtr);
assert(strcmp(s, s2) == 0);
}
}
/* String initialization */
{
/*
# char * vs char[] initialization
http://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s-in-c
When `""` appears on an array initialization, it is magic:
char c[] = "abc";
`"abc"` initializes `c` and end of story.
Everywhere else, the string literal is an:
- unamed
- array of char TODO const or not? http://stackoverflow.com/questions/2245664/what-is-the-type-of-string-literals-in-c-c
- that gives UB if modified
E.g.:
char *c = "abc";
Here `"abc"` is of type `char[4]`. Array types can be converted to pointers, so `c` points to it.
Therefore with `*,` it cannot be modified later, if we modify the contents of `c`, UB.
In Linux, this is either `.rodata` or directly on the `.text` segment.
*/
{
/* Array initialization magic. */
{
char s[] = "abc";
assert(s[0] == 'a');
/* OK */
s[0] = '0';
}
/* Everywhere else case. */
{
char *s = "abc";
assert(s[0] == 'a');
/* Unefined behaviour. On Linux, segfault. */
/*cs[0] = '0';*/
}
/*
Arrays and structs have the analogous compound literals, except that those:
- are modifiable, not UB const
- have block scope, not static
*/
{
int *is = (int[]){1, 3, 2};
}
}
/*
Parenthesis. Legal but ugly. GCC 4.8 gives an error with `-pedantic`.
*/
{
/*char s[] = ("abc");*/
}
}
/*
# String literals
http://en.cppreference.com/w/c/language/string_literal
*/
{
/* Escape chars in string conts */
{
/*
Octal byte string literals
`\` followed by any digit.
Remember that 3 octal digits can encode a byte,
but that 400 is out of byte range.
1 or 2 digits are also OK, although it is less readable and more error prone.
*/
{
assert(!strcmp("\1", "\x1"));
assert(!strcmp("\11", "\x9"));
assert(!strcmp("\141", "a"));
assert(!strcmp("\200", "\x80"));
/* Warning: out of range. TODO legal? */
/*assert(!strcmp("\400", "\x80"));*/
}
/* Hexadecimal bytes. */
{
/* TODO: guaranteed? */
assert(!strcmp("\x61", "a"));
/*
WARNING: Hex escape out of range.
Happens because f can be part of a hex literal,
and C tries to put it together.
TODO what happens exactly according to ANSI C?
Undefined behaviour?
*/
/*{ "\xfff"; }*/
/*
The solution is to either concatenate strings, or use another escape:
*/
assert(strcmp("\xff""f", "\xff\x66") == 0);
/* Chinese UTF-8. */
puts(">>>\xe4\xb8\xad<<< zhong1, chinese for \"middle\" in utf8");
}
/*
\0 is the NUL char, but you can't insert is directly on the literal,
or else the string is interpreted to end there since C strigs are NUL terminated.
*/
printf(">>>%c<<< NUL char\n", '\0');
/* Double quotes. */
assert(strcmp("\"", "\x22") == 0);
/* Single quote. Likely exists for symmetry with character literals. */
assert(strcmp("\'", "\x27") == 0);
/* Backslash. */
assert(strcmp("\\", "\x5c") == 0);
/*
WARNING: Unknown escape sequence. TODO what ANSI C says can happen?
*/
/*assert(strcmp("\c", "\x0b") == 0);*/
/* Alert. Your terminal may interpret this as a sound beep or not. */
assert(strcmp("\a", "\x07") == 0);
/* Backspace. */
assert(strcmp("\b", "\x08") == 0);
/* Feed */
assert(strcmp("\f", "\x0c") == 0);
/* New line */
assert(strcmp("\n", "\x0a") == 0);
/* Carriage return */
assert(strcmp("\r", "\x0d") == 0);
/* Tab */
assert(strcmp("\t", "\x09") == 0);
/* Vertical tab */
assert(strcmp("\v", "\x0b") == 0);
/* Notable extensions: \e GNU for ESC */
/* Percent % is not magic for string literals, only for printf. */
assert(strlen("%%") == 2);
}
/*
# What are string literals
Arrays that give UB if you modify them.
*/
{
assert("abc"[0] == 'a');
/* Must be an array: a pointer could not have size 3. */
assert(sizeof("ab") == 3);
/* Array to pointer decays like for any array. */
{
char *c = "abc";
c = "def";
assert(c[0] == 'd');
}
/* lvalue */
{
char (*c)[] = &"abc";
assert((*c)[0] == 'a');
}
}
/*
# Concatenate strings at compile time
Adjacent string literals are concatenated at compile time.
No spaces are implied.
Runtime concatenation can be achieved with `strcat`.
*/
{
assert(strcmp("ab" "cd", "abcd") == 0);
/*
This cannot be done with variables,
but can be useful if you have a string that is defined in a macro:
*/
{
char var[] = "ab";
/* ERROR */
/*assert(strcmp(cs var, "abcd") == 0);*/
/* OK. */
#define STRING_AB "ab"
assert(strcmp(STRING_AB "cd", "abcd") == 0);
}
/*
Another application is to break a long string literal over severl lines.
No newline is implied.
*/
{
char cs[] = "ab"
"cd";
assert(strcmp(cs, "abcd") == 0);
}
/* It is not possible to break a string literal over on multiple lines. */
/*
{
char cs[] = "ab
cd";
assert(strcmp(cs, "abcd") == 0);
}
*/
}
}
return EXIT_SUCCESS;
}