/
primitives.c
394 lines (332 loc) · 10.2 KB
/
primitives.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
Copyright (C) 2006-2009, Parrot Foundation.
$Id$
=head1 NAME
src/string/string_primitives.c - String Primitives
=head1 DESCRIPTION
This file collects together all the functions that call into the ICU
API.
=head2 Functions
=over 4
=cut
*/
/* HEADERIZER HFILE: include/parrot/string_primitives.h */
#include "parrot/parrot.h"
#if PARROT_HAS_ICU
# include <unicode/ucnv.h>
# include <unicode/utypes.h>
# include <unicode/uchar.h>
# include <unicode/ustring.h>
#else
# include <ctype.h>
#endif
/*
=item C<void string_set_data_directory(PARROT_INTERP, const char *dir)>
Set the directory where ICU finds its data files (encodings, locales,
etc.).
=cut
*/
PARROT_EXPORT
void
string_set_data_directory(PARROT_INTERP, ARGIN(const char *dir))
{
ASSERT_ARGS(string_set_data_directory)
#if PARROT_HAS_ICU
u_setDataDirectory(dir);
/* Since u_setDataDirectory doesn't have a result code, we'll spot
check that everything is okay by making sure that '9' had decimal
value 9. Using 57 rather than '9' so that the encoding of this
source code file isn't an issue.... (Don't want to get bitten by
EBCDIC.) */
if (!u_isdigit(57) || (u_charDigitValue(57) != 9))
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_ICU_ERROR,
"string_set_data_directory: ICU data files not found"
"(apparently) for directory [%s]", dir);
#else
UNUSED(dir);
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_ICU_ERROR,
"string_set_data_directory: parrot compiled without ICU support");
#endif
}
/*
=item C<Parrot_UInt4 string_unescape_one(PARROT_INTERP, UINTVAL *offset, STRING
*string)>
Unescape a single character. We assume that we're at the start of a
sequence, right after the \.
=cut
*/
PARROT_EXPORT
Parrot_UInt4
string_unescape_one(PARROT_INTERP, ARGMOD(UINTVAL *offset),
ARGMOD(STRING *string))
{
ASSERT_ARGS(string_unescape_one)
UINTVAL workchar = 0;
UINTVAL charcount = 0;
const UINTVAL len = Parrot_str_byte_length(interp, string);
/* Well, not right now */
UINTVAL codepoint = CHARSET_GET_BYTE(interp, string, *offset);
++*offset;
switch (codepoint) {
case 'x':
codepoint = CHARSET_GET_BYTE(interp, string, *offset);
if (codepoint >= '0' && codepoint <= '9') {
workchar = codepoint - '0';
}
else if (codepoint >= 'a' && codepoint <= 'f') {
workchar = codepoint - 'a' + 10;
}
else if (codepoint >= 'A' && codepoint <= 'F') {
workchar = codepoint - 'A' + 10;
}
else if (codepoint == '{') {
int i;
++*offset;
workchar = 0;
for (i = 0; i < 8 && *offset < len; ++i, ++*offset) {
codepoint = CHARSET_GET_BYTE(interp, string, *offset);
if (codepoint == '}') {
++*offset;
return workchar;
}
workchar *= 16;
if (codepoint >= '0' && codepoint <= '9') {
workchar += codepoint - '0';
}
else if (codepoint >= 'a' && codepoint <= 'f') {
workchar += codepoint - 'a' + 10;
}
else if (codepoint >= 'A' && codepoint <= 'F') {
workchar += codepoint - 'A' + 10;
}
else {
Parrot_ex_throw_from_c_args(interp, NULL,
EXCEPTION_UNIMPLEMENTED,
"Illegal escape sequence inside {}");
}
}
if (*offset == len)
Parrot_ex_throw_from_c_args(interp, NULL,
EXCEPTION_UNIMPLEMENTED,
"Illegal escape sequence no '}'");
}
else {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_UNIMPLEMENTED,
"Illegal escape sequence in");
}
++*offset;
if (*offset < len) {
workchar *= 16;
codepoint = CHARSET_GET_BYTE(interp, string, *offset);
if (codepoint >= '0' && codepoint <= '9') {
workchar += codepoint - '0';
}
else if (codepoint >= 'a' && codepoint <= 'f') {
workchar += codepoint - 'a' + 10;
}
else if (codepoint >= 'A' && codepoint <= 'F') {
workchar += codepoint - 'A' + 10;
}
else {
return workchar;
}
}
else {
return workchar;
}
++*offset;
return workchar;
case 'c':
codepoint = CHARSET_GET_BYTE(interp, string, *offset);
if (codepoint >= 'A' && codepoint <= 'Z') {
workchar = codepoint - 'A' + 1;
}
else {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_UNIMPLEMENTED,
"Illegal escape sequence");
}
++*offset;
return workchar;
case 'u':
workchar = 0;
for (charcount = 0; charcount < 4; charcount++) {
if (*offset < len) {
workchar *= 16;
codepoint = CHARSET_GET_BYTE(interp, string, *offset);
if (codepoint >= '0' && codepoint <= '9') {
workchar += codepoint - '0';
}
else if (codepoint >= 'a' && codepoint <= 'f') {
workchar += codepoint - 'a' + 10;
}
else if (codepoint >= 'A' && codepoint <= 'F') {
workchar += codepoint - 'A' + 10;
}
else {
Parrot_ex_throw_from_c_args(interp, NULL,
EXCEPTION_UNIMPLEMENTED,
"Illegal escape sequence in uxxx escape");
}
}
else {
Parrot_ex_throw_from_c_args(interp, NULL,
EXCEPTION_UNIMPLEMENTED,
"Illegal escape sequence in uxxx escape - too short");
}
++*offset;
}
return workchar;
case 'U':
workchar = 0;
for (charcount = 0; charcount < 8; charcount++) {
if (*offset < len) {
workchar *= 16;
codepoint = CHARSET_GET_BYTE(interp, string, *offset);
if (codepoint >= '0' && codepoint <= '9') {
workchar += codepoint - '0';
}
else if (codepoint >= 'a' && codepoint <= 'f') {
workchar += codepoint - 'a' + 10;
}
else if (codepoint >= 'A' && codepoint <= 'F') {
workchar += codepoint - 'A' + 10;
}
else {
Parrot_ex_throw_from_c_args(interp, NULL,
EXCEPTION_UNIMPLEMENTED,
"Illegal escape sequence in Uxxx escape");
}
}
else {
Parrot_ex_throw_from_c_args(interp, NULL,
EXCEPTION_UNIMPLEMENTED,
"Illegal escape sequence in uxxx escape - too short");
}
++*offset;
}
return workchar;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
workchar = codepoint - '0';
if (*offset < len) {
workchar *= 8;
codepoint = CHARSET_GET_BYTE(interp, string, *offset);
if (codepoint >= '0' && codepoint <= '7') {
workchar += codepoint - '0';
}
else {
return workchar;
}
}
else {
return workchar;
}
++*offset;
if (*offset < len) {
workchar *= 8;
codepoint = CHARSET_GET_BYTE(interp, string, *offset);
if (codepoint >= '0' && codepoint <= '7') {
workchar += codepoint - '0';
}
else {
return workchar;
}
}
else {
return workchar;
}
++*offset;
return workchar;
case 'a':
return 7; /* bell */
case 'b':
return 8; /* bs */
case 't':
return 9;
case 'n':
return 10;
case 'v':
return 11;
case 'f':
return 12;
case 'r':
return 13;
case 'e':
return 27;
case 92: /* \ */
return 92;
case '"':
return '"';
default:
return codepoint; /* any not special return the char */
}
}
/*
=back
=head2 Character Property Functions
=over 4
=item C<INTVAL Parrot_char_digit_value(PARROT_INTERP, UINTVAL character)>
Returns the decimal digit value of the specified character if it is a decimal
digit character. If not, then -1 is returned.
Note that as currently written, C<Parrot_char_digit_value()> can
correctly return the decimal digit value of characters for which
C<Parrot_char_is_digit()> returns false.
=cut
*/
PARROT_EXPORT
PARROT_CONST_FUNCTION
INTVAL
Parrot_char_digit_value(SHIM_INTERP, UINTVAL character)
{
ASSERT_ARGS(Parrot_char_digit_value)
#if PARROT_HAS_ICU
return u_charDigitValue(character);
#else
if ((character >= 0x30) || (character <= 0x39))
return character - 0x30;
return -1;
#endif
}
/*
=item C<char * str_dup_remove_quotes(const char *old)>
Duplicates a C string (minus the wrapping quotes). Similar to strdup(),
except it dies if it runs out of memory.
=cut
*/
PARROT_EXPORT
PARROT_MALLOC
PARROT_CANNOT_RETURN_NULL
char *
str_dup_remove_quotes(ARGIN(const char *old))
{
ASSERT_ARGS(str_dup_remove_quotes)
const size_t oldlen = strlen(old) + 1;
/* 2 for the beginning and ending quote chars */
const size_t newlen = oldlen - 2;
char * const copy = (char *)mem_sys_allocate(newlen);
memcpy(copy, old + 1, newlen);
copy[newlen - 1] = 0;
return copy;
}
/*
=back
=head1 SEE ALSO
=over 4
=item F<include/parrot/string_primitives.h>
=item F<include/parrot/string.h>
=item F<src/string.c>
=back
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4:
*/