Debian Codegraph @

pypy/module/cpyext/src/unicodeobject.c

raw symbols SCIP
1 #include "Python.h"
2
3 #if defined(Py_ISDIGIT) || defined(Py_ISALPHA)
4 #error remove these definitions
5 #endif
6 #define Py_ISDIGIT isdigit
7 #define Py_ISALPHA isalpha
8 _Py_COMP_DIAG_IGNORE_DEPR_DECLS
9
10 #include "unicodetype_db.h"
11
12 static void
13 makefmt(char *fmt, int longflag, int longlongflag, int size_tflag,
14 int zeropad, int width, int precision, char c)
15 {
16 *fmt++ = '%';
17 if (width) {
18 if (zeropad)
19 *fmt++ = '0';
20 fmt += sprintf(fmt, "%d", width);
21 }
22 if (precision)
23 fmt += sprintf(fmt, ".%d", precision);
24 if (longflag)
25 *fmt++ = 'l';
26 else if (longlongflag) {
27 /* longlongflag should only ever be nonzero on machines with
28 HAVE_LONG_LONG defined */
29 #ifdef HAVE_LONG_LONG
30 char *f = PY_FORMAT_LONG_LONG;
31 while (*f)
32 *fmt++ = *f++;
33 #else
34 /* we shouldn't ever get here */
35 assert(0);
36 *fmt++ = 'l';
37 #endif
38 }
39 else if (size_tflag) {
40 char *f = PY_FORMAT_SIZE_T;
41 while (*f)
42 *fmt++ = *f++;
43 }
44 *fmt++ = c;
45 *fmt = '\0';
46 }
47
48 /* Fast detection of the most frequent whitespace characters */
49 const unsigned char _Py_ascii_whitespace[] = {
50 0, 0, 0, 0, 0, 0, 0, 0,
51 /* case 0x0009: * CHARACTER TABULATION */
52 /* case 0x000A: * LINE FEED */
53 /* case 0x000B: * LINE TABULATION */
54 /* case 0x000C: * FORM FEED */
55 /* case 0x000D: * CARRIAGE RETURN */
56 0, 1, 1, 1, 1, 1, 0, 0,
57 0, 0, 0, 0, 0, 0, 0, 0,
58 /* case 0x001C: * FILE SEPARATOR */
59 /* case 0x001D: * GROUP SEPARATOR */
60 /* case 0x001E: * RECORD SEPARATOR */
61 /* case 0x001F: * UNIT SEPARATOR */
62 0, 0, 0, 0, 1, 1, 1, 1,
63 /* case 0x0020: * SPACE */
64 1, 0, 0, 0, 0, 0, 0, 0,
65 0, 0, 0, 0, 0, 0, 0, 0,
66 0, 0, 0, 0, 0, 0, 0, 0,
67 0, 0, 0, 0, 0, 0, 0, 0,
68
69 0, 0, 0, 0, 0, 0, 0, 0,
70 0, 0, 0, 0, 0, 0, 0, 0,
71 0, 0, 0, 0, 0, 0, 0, 0,
72 0, 0, 0, 0, 0, 0, 0, 0,
73 0, 0, 0, 0, 0, 0, 0, 0,
74 0, 0, 0, 0, 0, 0, 0, 0,
75 0, 0, 0, 0, 0, 0, 0, 0,
76 0, 0, 0, 0, 0, 0, 0, 0
77 };
78
79
80 #define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;}
81
82 /* size of fixed-size buffer for formatting single arguments */
83 #define ITEM_BUFFER_LEN 21
84 /* maximum number of characters required for output of %ld. 21 characters
85 allows for 64-bit integers (in decimal) and an optional sign. */
86 #define MAX_LONG_CHARS 21
87 /* maximum number of characters required for output of %lld.
88 We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits,
89 plus 1 for the sign. 53/22 is an upper bound for log10(256). */
90 #define MAX_LONG_LONG_CHARS (2 + (SIZEOF_LONG_LONG*53-1) / 22)
91
92 #ifdef HAVE_WCHAR_H
93
94 PyObject *
95 PyUnicode_FromWideChar(const wchar_t *w, Py_ssize_t size)
96 {
97 /*
98 if (w == NULL) {
99 if (size == 0)
100 _Py_RETURN_UNICODE_EMPTY();
101 PyErr_BadInternalCall();
102 return NULL;
103 }
104 */
105
106 if (size == -1) {
107 size = wcslen(w);
108 }
109
110 return PyUnicode_FromUnicode(w, size);
111 }
112
113 #endif /* HAVE_WCHAR_H */
114
115 PyObject *
116 PyUnicode_FromFormatV(const char *format, va_list vargs)
117 {
118 va_list count;
119 Py_ssize_t callcount = 0;
120 PyObject **callresults = NULL;
121 PyObject **callresult = NULL;
122 Py_ssize_t n = 0;
123 int zeropad;
124 const char* f;
125 Py_UNICODE *s;
126 PyObject *string;
127 /* used by sprintf */
128 char buffer[ITEM_BUFFER_LEN+1];
129 /* use abuffer instead of buffer, if we need more space
130 * (which can happen if there's a format specifier with width). */
131 char *abuffer = NULL;
132 char *realbuffer;
133 Py_ssize_t abuffersize = 0;
134 char fmt[61]; /* should be enough for %0width.precisionlld */
135 const char *copy;
136
137 Py_VA_COPY(count, vargs);
138 /* step 1: count the number of %S/%R/%A/%s format specifications
139 * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/
140 * PyUnicode_DecodeUTF8() for these objects once during step 3 and put the
141 * result in an array) */
142 for (f = format; *f; f++) {
143 if (*f == '%') {
144 f++;
145 if (f[0]=='%') {
146 continue;
147 }
148 while (Py_ISDIGIT((unsigned)f[0]) || f[0] == '.') f++;
149 if (f[0] == 'S' || f[0] == 'R' || f[0] == 'A' || f[0] == 'V' || f[0] == 's') {
150 callcount++;
151 }
152 }
153 else if (128 <= (unsigned char)*f) {
154 PyErr_Format(PyExc_ValueError,
155 "PyUnicode_FromFormatV() expects an ASCII-encoded format "
156 "string, got a non-ASCII byte: 0x%02x",
157 (unsigned char)*f);
158 return NULL;
159 }
160 }
161 /* step 2: allocate memory for the results of
162 * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */
163 if (callcount) {
164 callresults = PyObject_Malloc(sizeof(PyObject *)*callcount);
165 if (!callresults) {
166 PyErr_NoMemory();
167 return NULL;
168 }
169 callresult = callresults;
170 }
171 /* step 3: figure out how large a buffer we need */
172 for (f = format; *f; f++) {
173 if (*f == '%') {
174 #ifdef HAVE_LONG_LONG
175 int longlongflag = 0;
176 #endif
177 int width = 0;
178 const char* p = f;
179 while (Py_ISDIGIT((unsigned)*f))
180 width = (width*10) + *f++ - '0';
181 while (*++f && *f != '%' && !Py_ISALPHA((unsigned)*f))
182 ;
183
184 /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since
185 * they don't affect the amount of space we reserve.
186 */
187 if (*f == 'l') {
188 if (f[1] == 'd' || f[1] == 'i' || f[1] == 'u') {
189 ++f;
190 }
191 #ifdef HAVE_LONG_LONG
192 else if (f[1] == 'l' &&
193 (f[2] == 'd' || f[2] == 'i' || f[2] == 'u')) {
194 longlongflag = 1;
195 f += 2;
196 }
197 #endif
198 }
199 else if (*f == 'z' &&
200 (f[1] == 'd' || f[1] == 'i' || f[1] == 'u')) {
201 ++f;
202 }
203
204 switch (*f) {
205 case 'c':
206 {
207 #ifndef Py_UNICODE_WIDE
208 int ordinal = va_arg(count, int);
209 if (ordinal > 0xffff)
210 n += 2;
211 else
212 n++;
213 #else
214 (void)va_arg(count, int);
215 n++;
216 #endif
217 break;
218 }
219 case '%':
220 n++;
221 break;
222 case 'd': case 'u': case 'i': case 'x':
223 #ifdef HAVE_LONG_LONG
224 if (longlongflag)
225 (void) va_arg(count, long long);
226 else
227 #endif
228 (void) va_arg(count, int);
229 #ifdef HAVE_LONG_LONG
230 if (longlongflag) {
231 if (width < MAX_LONG_LONG_CHARS)
232 width = MAX_LONG_LONG_CHARS;
233 }
234 else
235 #endif
236 /* MAX_LONG_CHARS is enough to hold a 64-bit integer,
237 including sign. Decimal takes the most space. This
238 isn't enough for octal. If a width is specified we
239 need more (which we allocate later). */
240 if (width < MAX_LONG_CHARS)
241 width = MAX_LONG_CHARS;
242 n += width;
243 /* XXX should allow for large precision here too. */
244 if (abuffersize < width)
245 abuffersize = width;
246 break;
247 case 's':
248 {
249 /* UTF-8 */
250 const char *s = va_arg(count, const char*);
251 PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace");
252 if (!str)
253 goto fail;
254 n += PyUnicode_GET_SIZE(str);
255 /* Remember the str and switch to the next slot */
256 *callresult++ = str;
257 break;
258 }
259 case 'U':
260 {
261 PyObject *obj = va_arg(count, PyObject *);
262 assert(obj && PyUnicode_Check(obj));
263 n += PyUnicode_GET_SIZE(obj);
264 break;
265 }
266 case 'V':
267 {
268 PyObject *obj = va_arg(count, PyObject *);
269 const char *str = va_arg(count, const char *);
270 PyObject *str_obj;
271 assert(obj || str);
272 assert(!obj || PyUnicode_Check(obj));
273 if (obj) {
274 n += PyUnicode_GET_SIZE(obj);
275 *callresult++ = NULL;
276 }
277 else {
278 str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace");
279 if (!str_obj)
280 goto fail;
281 n += PyUnicode_GET_SIZE(str_obj);
282 *callresult++ = str_obj;
283 }
284 break;
285 }
286 case 'S':
287 {
288 PyObject *obj = va_arg(count, PyObject *);
289 PyObject *str;
290 assert(obj);
291 str = PyObject_Str(obj);
292 if (!str)
293 goto fail;
294 n += PyUnicode_GET_SIZE(str);
295 /* Remember the str and switch to the next slot */
296 *callresult++ = str;
297 break;
298 }
299 case 'R':
300 {
301 PyObject *obj = va_arg(count, PyObject *);
302 PyObject *repr;
303 assert(obj);
304 repr = PyObject_Repr(obj);
305 if (!repr)
306 goto fail;
307 n += PyUnicode_GET_SIZE(repr);
308 /* Remember the repr and switch to the next slot */
309 *callresult++ = repr;
310 break;
311 }
312 case 'A':
313 {
314 PyObject *obj = va_arg(count, PyObject *);
315 PyObject *ascii;
316 assert(obj);
317 ascii = PyObject_ASCII(obj);
318 if (!ascii)
319 goto fail;
320 n += PyUnicode_GET_SIZE(ascii);
321 /* Remember the repr and switch to the next slot */
322 *callresult++ = ascii;
323 break;
324 }
325 case 'p':
326 (void) va_arg(count, int);
327 /* maximum 64-bit pointer representation:
328 * 0xffffffffffffffff
329 * so 19 characters is enough.
330 * XXX I count 18 -- what's the extra for?
331 */
332 n += 19;
333 break;
334 default:
335 /* if we stumble upon an unknown
336 formatting code, copy the rest of
337 the format string to the output
338 string. (we cannot just skip the
339 code, since there's no way to know
340 what's in the argument list) */
341 n += strlen(p);
342 goto expand;
343 }
344 } else
345 n++;
346 }
347 expand:
348 if (abuffersize > ITEM_BUFFER_LEN) {
349 /* add 1 for sprintf's trailing null byte */
350 abuffer = PyObject_Malloc(abuffersize + 1);
351 if (!abuffer) {
352 PyErr_NoMemory();
353 goto fail;
354 }
355 realbuffer = abuffer;
356 }
357 else
358 realbuffer = buffer;
359 /* step 4: fill the buffer */
360 /* Since we've analyzed how much space we need for the worst case,
361 we don't have to resize the string.
362 There can be no errors beyond this point. */
363 string = PyUnicode_FromUnicode(NULL, n);
364 if (!string)
365 goto fail;
366
367 s = PyUnicode_AS_UNICODE(string);
368 callresult = callresults;
369
370 for (f = format; *f; f++) {
371 if (*f == '%') {
372 const char* p = f++;
373 int longflag = 0;
374 int longlongflag = 0;
375 int size_tflag = 0;
376 int width = 0;
377 int precision = 0;
378 zeropad = (*f == '0');
379 /* parse the width.precision part */
380 while (Py_ISDIGIT((unsigned)*f))
381 width = (width*10) + *f++ - '0';
382 if (*f == '.') {
383 f++;
384 while (Py_ISDIGIT((unsigned)*f))
385 precision = (precision*10) + *f++ - '0';
386 }
387 /* Handle %ld, %lu, %lld and %llu. */
388 if (*f == 'l') {
389 if (f[1] == 'd' || f[1] == 'i' || f[1] == 'u') {
390 longflag = 1;
391 ++f;
392 }
393 #ifdef HAVE_LONG_LONG
394 else if (f[1] == 'l' &&
395 (f[2] == 'd' || f[2] == 'i' || f[2] == 'u')) {
396 longlongflag = 1;
397 f += 2;
398 }
399 #endif
400 }
401 /* handle the size_t flag. */
402 if (*f == 'z' &&
403 (f[1] == 'd' || f[1] == 'i' || f[1] == 'u')) {
404 size_tflag = 1;
405 ++f;
406 }
407
408 switch (*f) {
409 case 'c':
410 {
411 int ordinal = va_arg(vargs, int);
412 #ifndef Py_UNICODE_WIDE
413 if (ordinal > 0xffff) {
414 ordinal -= 0x10000;
415 *s++ = 0xD800 | (ordinal >> 10);
416 *s++ = 0xDC00 | (ordinal & 0x3FF);
417 } else
418 #endif
419 *s++ = ordinal;
420 break;
421 }
422 case 'd':
423 case 'i':
424 makefmt(fmt, longflag, longlongflag, size_tflag, zeropad,
425 width, precision, *f);
426 if (longflag)
427 sprintf(realbuffer, fmt, va_arg(vargs, long));
428 #ifdef HAVE_LONG_LONG
429 else if (longlongflag)
430 sprintf(realbuffer, fmt, va_arg(vargs, PY_LONG_LONG));
431 #endif
432 else if (size_tflag)
433 sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t));
434 else
435 sprintf(realbuffer, fmt, va_arg(vargs, int));
436 appendstring(realbuffer);
437 break;
438 case 'u':
439 makefmt(fmt, longflag, longlongflag, size_tflag, zeropad,
440 width, precision, 'u');
441 if (longflag)
442 sprintf(realbuffer, fmt, va_arg(vargs, unsigned long));
443 #ifdef HAVE_LONG_LONG
444 else if (longlongflag)
445 sprintf(realbuffer, fmt, va_arg(vargs,
446 unsigned PY_LONG_LONG));
447 #endif
448 else if (size_tflag)
449 sprintf(realbuffer, fmt, va_arg(vargs, size_t));
450 else
451 sprintf(realbuffer, fmt, va_arg(vargs, unsigned int));
452 appendstring(realbuffer);
453 break;
454 case 'x':
455 makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x');
456 sprintf(realbuffer, fmt, va_arg(vargs, int));
457 appendstring(realbuffer);
458 break;
459 case 's':
460 {
461 /* unused, since we already have the result */
462 (void) va_arg(vargs, char *);
463 Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult),
464 PyUnicode_GET_SIZE(*callresult));
465 s += PyUnicode_GET_SIZE(*callresult);
466 /* We're done with the unicode()/repr() => forget it */
467 Py_DECREF(*callresult);
468 /* switch to next unicode()/repr() result */
469 ++callresult;
470 break;
471 }
472 case 'U':
473 {
474 PyObject *obj = va_arg(vargs, PyObject *);
475 Py_ssize_t size = PyUnicode_GET_SIZE(obj);
476 Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size);
477 s += size;
478 break;
479 }
480 case 'V':
481 {
482 PyObject *obj = va_arg(vargs, PyObject *);
483 va_arg(vargs, const char *);
484 if (obj) {
485 Py_ssize_t size = PyUnicode_GET_SIZE(obj);
486 Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size);
487 s += size;
488 } else {
489 Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult),
490 PyUnicode_GET_SIZE(*callresult));
491 s += PyUnicode_GET_SIZE(*callresult);
492 Py_DECREF(*callresult);
493 }
494 ++callresult;
495 break;
496 }
497 case 'S':
498 case 'R':
499 case 'A':
500 {
501 Py_UNICODE *ucopy;
502 Py_ssize_t usize;
503 Py_ssize_t upos;
504 /* unused, since we already have the result */
505 (void) va_arg(vargs, PyObject *);
506 ucopy = PyUnicode_AS_UNICODE(*callresult);
507 usize = PyUnicode_GET_SIZE(*callresult);
508 for (upos = 0; upos<usize;)
509 *s++ = ucopy[upos++];
510 /* We're done with the unicode()/repr() => forget it */
511 Py_DECREF(*callresult);
512 /* switch to next unicode()/repr() result */
513 ++callresult;
514 break;
515 }
516 case 'p':
517 sprintf(buffer, "%p", va_arg(vargs, void*));
518 /* %p is ill-defined: ensure leading 0x. */
519 if (buffer[1] == 'X')
520 buffer[1] = 'x';
521 else if (buffer[1] != 'x') {
522 memmove(buffer+2, buffer, strlen(buffer)+1);
523 buffer[0] = '0';
524 buffer[1] = 'x';
525 }
526 appendstring(buffer);
527 break;
528 case '%':
529 *s++ = '%';
530 break;
531 default:
532 appendstring(p);
533 goto end;
534 }
535 }
536 else
537 *s++ = *f;
538 }
539
540 end:
541 if (callresults)
542 PyObject_Free(callresults);
543 if (abuffer)
544 PyObject_Free(abuffer);
545 PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string));
546 return string;
547 fail:
548 if (callresults) {
549 PyObject **callresult2 = callresults;
550 while (callresult2 < callresult) {
551 Py_XDECREF(*callresult2);
552 ++callresult2;
553 }
554 PyObject_Free(callresults);
555 }
556 if (abuffer)
557 PyObject_Free(abuffer);
558 return NULL;
559 }
560
561 #undef appendstring
562
563 PyObject *
564 PyUnicode_FromFormat(const char *format, ...)
565 {
566 PyObject* ret;
567 va_list vargs;
568
569 #ifdef HAVE_STDARG_PROTOTYPES
570 va_start(vargs, format);
571 #else
572 va_start(vargs);
573 #endif
574 ret = PyUnicode_FromFormatV(format, vargs);
575 va_end(vargs);
576 return ret;
577 }
578
579 wchar_t*
580 PyUnicode_AsWideCharString(PyObject *unicode,
581 Py_ssize_t *size)
582 {
583 wchar_t* buffer;
584 Py_ssize_t buflen;
585
586 if (unicode == NULL) {
587 PyErr_BadInternalCall();
588 return NULL;
589 }
590
591 buflen = PyUnicode_GET_SIZE(unicode) + 1;
592 if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) {
593 PyErr_NoMemory();
594 return NULL;
595 }
596
597 /* PyPy shortcut: Unicode is already an array of wchar_t */
598 buffer = PyMem_MALLOC(buflen * sizeof(wchar_t));
599 if (buffer == NULL) {
600 PyErr_NoMemory();
601 return NULL;
602 }
603
604 if (PyUnicode_AsWideChar(unicode, buffer, buflen) < 0)
605 return NULL;
606 if (size != NULL)
607 *size = buflen - 1;
608 return buffer;
609 }
610
611 Py_ssize_t
612 PyUnicode_GetSize(PyObject *unicode)
613 {
614 if (!PyUnicode_Check(unicode)) {
615 PyErr_BadArgument();
616 goto onError;
617 }
618 return PyUnicode_GET_SIZE(unicode);
619
620 onError:
621 return -1;
622 }
623
624 Py_ssize_t
625 PyUnicode_GetLength(PyObject *unicode)
626 {
627 if (!PyUnicode_Check(unicode)) {
628 PyErr_BadArgument();
629 return -1;
630 }
631 if (PyUnicode_READY(unicode) == -1)
632 return -1;
633 return PyUnicode_GET_LENGTH(unicode);
634 }
635
636 void
637 PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right)
638 {
639 PyUnicode_Append(pleft, right);
640 Py_XDECREF(right);
641 }
642
643 Py_UNICODE * PyUnicode_AsUnicode(PyObject* unicode) {
644 return PyUnicode_AsUnicodeAndSize(unicode, NULL);
645 }
646
647