FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python/Objects/stringobject.c at master · cooljt/python · GitHub
cooljt
/
python
Public
forked from
glix/python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
python
/
Objects
/
stringobject.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
5239 lines (4691 loc) · 127 KB
Breadcrumbs
python
/
Objects
/
stringobject.c
Copy path
File metadata and controls
5239 lines (4691 loc) · 127 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* String (str/bytes) object implementation */
#define
PY_SSIZE_T_CLEAN
#include
"Python.h"
#include
<ctype.h>
#include
<stddef.h>
#ifdef
COUNT_ALLOCS
Py_ssize_t
null_strings
,
one_strings
;
#endif
static
PyStringObject
*
characters
[
UCHAR_MAX
+
1
];
static
PyStringObject
*
nullstring
;
/* This dictionary holds all interned strings. Note that references to
strings in this dictionary are *not* counted in the string's ob_refcnt.
When the interned string reaches a refcnt of 0 the string deallocation
function will delete the reference from this dictionary.
Another way to look at this is that to say that the actual reference
count of a string is: s->ob_refcnt + (s->ob_sstate?2:0)
*/
static
PyObject
*
interned
;
/* PyStringObject_SIZE gives the basic size of a string; any memory allocation
for a string of length n should request PyStringObject_SIZE + n bytes.
Using PyStringObject_SIZE instead of sizeof(PyStringObject) saves
3 bytes per string allocation on a typical system.
*/
#define
PyStringObject_SIZE
(offsetof(PyStringObject, ob_sval) + 1)
/*
For both PyString_FromString() and PyString_FromStringAndSize(), the
parameter `size' denotes number of characters to allocate, not counting any
null terminating character.
For PyString_FromString(), the parameter `str' points to a null-terminated
string containing exactly `size' bytes.
For PyString_FromStringAndSize(), the parameter the parameter `str' is
either NULL or else points to a string containing at least `size' bytes.
For PyString_FromStringAndSize(), the string in the `str' parameter does
not have to be null-terminated. (Therefore it is safe to construct a
substring by calling `PyString_FromStringAndSize(origstring, substrlen)'.)
If `str' is NULL then PyString_FromStringAndSize() will allocate `size+1'
bytes (setting the last byte to the null terminating character) and you can
fill in the data yourself. If `str' is non-NULL then the resulting
PyString object must be treated as immutable and you must not fill in nor
alter the data yourself, since the strings may be shared.
The PyObject member `op->ob_size', which denotes the number of "extra
items" in a variable-size object, will contain the number of bytes
allocated for string data, not counting the null terminating character. It
is therefore equal to the equal to the `size' parameter (for
PyString_FromStringAndSize()) or the length of the string in the `str'
parameter (for PyString_FromString()).
*/
PyObject
*
PyString_FromStringAndSize
(
const
char
*
str
,
Py_ssize_t
size
)
{
register
PyStringObject
*
op
;
if
(
size
<
0
) {
PyErr_SetString
(
PyExc_SystemError
,
"Negative size passed to PyString_FromStringAndSize"
);
return
NULL
;
}
if
(
size
==
0
&&
(
op
=
nullstring
)
!=
NULL
) {
#ifdef
COUNT_ALLOCS
null_strings
++
;
#endif
Py_INCREF
(
op
);
return
(
PyObject
*
)
op
;
}
if
(
size
==
1
&&
str
!=
NULL
&&
(
op
=
characters
[
*
str
&
UCHAR_MAX
])
!=
NULL
)
{
#ifdef
COUNT_ALLOCS
one_strings
++
;
#endif
Py_INCREF
(
op
);
return
(
PyObject
*
)
op
;
}
if
(
size
>
PY_SSIZE_T_MAX
-
PyStringObject_SIZE
) {
PyErr_SetString
(
PyExc_OverflowError
,
"string is too large"
);
return
NULL
;
}
/* Inline PyObject_NewVar */
op
=
(
PyStringObject
*
)
PyObject_MALLOC
(
PyStringObject_SIZE
+
size
);
if
(
op
==
NULL
)
return
PyErr_NoMemory
();
PyObject_INIT_VAR
(
op
,
&
PyString_Type
,
size
);
op
->
ob_shash
=
-1
;
op
->
ob_sstate
=
SSTATE_NOT_INTERNED
;
if
(
str
!=
NULL
)
Py_MEMCPY
(
op
->
ob_sval
,
str
,
size
);
op
->
ob_sval
[
size
]
=
'\0'
;
/* share short strings */
if
(
size
==
0
) {
PyObject
*
t
=
(
PyObject
*
)
op
;
PyString_InternInPlace
(
&
t
);
op
=
(
PyStringObject
*
)
t
;
nullstring
=
op
;
Py_INCREF
(
op
);
}
else
if
(
size
==
1
&&
str
!=
NULL
) {
PyObject
*
t
=
(
PyObject
*
)
op
;
PyString_InternInPlace
(
&
t
);
op
=
(
PyStringObject
*
)
t
;
characters
[
*
str
&
UCHAR_MAX
]
=
op
;
Py_INCREF
(
op
);
}
return
(
PyObject
*
)
op
;
}
PyObject
*
PyString_FromString
(
const
char
*
str
)
{
register
size_t
size
;
register
PyStringObject
*
op
;
assert
(
str
!=
NULL
);
size
=
strlen
(
str
);
if
(
size
>
PY_SSIZE_T_MAX
-
PyStringObject_SIZE
) {
PyErr_SetString
(
PyExc_OverflowError
,
"string is too long for a Python string"
);
return
NULL
;
}
if
(
size
==
0
&&
(
op
=
nullstring
)
!=
NULL
) {
#ifdef
COUNT_ALLOCS
null_strings
++
;
#endif
Py_INCREF
(
op
);
return
(
PyObject
*
)
op
;
}
if
(
size
==
1
&&
(
op
=
characters
[
*
str
&
UCHAR_MAX
])
!=
NULL
) {
#ifdef
COUNT_ALLOCS
one_strings
++
;
#endif
Py_INCREF
(
op
);
return
(
PyObject
*
)
op
;
}
/* Inline PyObject_NewVar */
op
=
(
PyStringObject
*
)
PyObject_MALLOC
(
PyStringObject_SIZE
+
size
);
if
(
op
==
NULL
)
return
PyErr_NoMemory
();
PyObject_INIT_VAR
(
op
,
&
PyString_Type
,
size
);
op
->
ob_shash
=
-1
;
op
->
ob_sstate
=
SSTATE_NOT_INTERNED
;
Py_MEMCPY
(
op
->
ob_sval
,
str
,
size
+
1
);
/* share short strings */
if
(
size
==
0
) {
PyObject
*
t
=
(
PyObject
*
)
op
;
PyString_InternInPlace
(
&
t
);
op
=
(
PyStringObject
*
)
t
;
nullstring
=
op
;
Py_INCREF
(
op
);
}
else
if
(
size
==
1
) {
PyObject
*
t
=
(
PyObject
*
)
op
;
PyString_InternInPlace
(
&
t
);
op
=
(
PyStringObject
*
)
t
;
characters
[
*
str
&
UCHAR_MAX
]
=
op
;
Py_INCREF
(
op
);
}
return
(
PyObject
*
)
op
;
}
PyObject
*
PyString_FromFormatV
(
const
char
*
format
,
va_list
vargs
)
{
va_list
count
;
Py_ssize_t
n
=
0
;
const
char
*
f
;
char
*
s
;
PyObject
*
string
;
#ifdef
VA_LIST_IS_ARRAY
Py_MEMCPY
(
count
,
vargs
,
sizeof
(
va_list
));
#else
#ifdef
__va_copy
__va_copy
(
count
,
vargs
);
#else
count
=
vargs
;
#endif
#endif
/* step 1: figure out how large a buffer we need */
for
(
f
=
format
;
*
f
;
f
++
) {
if
(
*
f
==
'%'
) {
const
char
*
p
=
f
;
while
(
*
++
f
&&
*
f
!=
'%'
&&
!
isalpha
(
Py_CHARMASK
(
*
f
)))
;
/* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since
* they don't affect the amount of space we reserve.
*/
if
((
*
f
==
'l'
||
*
f
==
'z'
)
&&
(
f
[
1
]
==
'd'
||
f
[
1
]
==
'u'
))
++
f
;
switch
(
*
f
) {
case
'c'
:
(
void
)
va_arg
(
count
,
int
);
/* fall through... */
case
'%'
:
n
++
;
break
;
case
'd'
:
case
'u'
:
case
'i'
:
case
'x'
:
(
void
)
va_arg
(
count
,
int
);
/* 20 bytes is enough to hold a 64-bit
integer. Decimal takes the most space.
This isn't enough for octal. */
n
+=
20
;
break
;
case
's'
:
s
=
va_arg
(
count
,
char
*
);
n
+=
strlen
(
s
);
break
;
case
'p'
:
(
void
)
va_arg
(
count
,
int
);
/* maximum 64-bit pointer representation:
* 0xffffffffffffffff
* so 19 characters is enough.
* XXX I count 18 -- what's the extra for?
*/
n
+=
19
;
break
;
default
:
/* if we stumble upon an unknown
formatting code, copy the rest of
the format string to the output
string. (we cannot just skip the
code, since there's no way to know
what's in the argument list) */
n
+=
strlen
(
p
);
goto
expand
;
}
}
else
n
++
;
}
expand
:
/* step 2: fill the buffer */
/* Since we've analyzed how much space we need for the worst case,
use sprintf directly instead of the slower PyOS_snprintf. */
string
=
PyString_FromStringAndSize
(
NULL
,
n
);
if
(!
string
)
return
NULL
;
s
=
PyString_AsString
(
string
);
for
(
f
=
format
;
*
f
;
f
++
) {
if
(
*
f
==
'%'
) {
const
char
*
p
=
f
++
;
Py_ssize_t
i
;
int
longflag
=
0
;
int
size_tflag
=
0
;
/* parse the width.precision part (we're only
interested in the precision value, if any) */
n
=
0
;
while
(
isdigit
(
Py_CHARMASK
(
*
f
)))
n
=
(
n
*
10
)
+
*
f
++
-
'0'
;
if
(
*
f
==
'.'
) {
f
++
;
n
=
0
;
while
(
isdigit
(
Py_CHARMASK
(
*
f
)))
n
=
(
n
*
10
)
+
*
f
++
-
'0'
;
}
while
(
*
f
&&
*
f
!=
'%'
&&
!
isalpha
(
Py_CHARMASK
(
*
f
)))
f
++
;
/* handle the long flag, but only for %ld and %lu.
others can be added when necessary. */
if
(
*
f
==
'l'
&&
(
f
[
1
]
==
'd'
||
f
[
1
]
==
'u'
)) {
longflag
=
1
;
++
f
;
}
/* handle the size_t flag. */
if
(
*
f
==
'z'
&&
(
f
[
1
]
==
'd'
||
f
[
1
]
==
'u'
)) {
size_tflag
=
1
;
++
f
;
}
switch
(
*
f
) {
case
'c'
:
*
s
++
=
va_arg
(
vargs
,
int
);
break
;
case
'd'
:
if
(
longflag
)
sprintf
(
s
,
"%ld"
,
va_arg
(
vargs
,
long
));
else
if
(
size_tflag
)
sprintf
(
s
,
"%"
PY_FORMAT_SIZE_T
"d"
,
va_arg
(
vargs
,
Py_ssize_t
));
else
sprintf
(
s
,
"%d"
,
va_arg
(
vargs
,
int
));
s
+=
strlen
(
s
);
break
;
case
'u'
:
if
(
longflag
)
sprintf
(
s
,
"%lu"
,
va_arg
(
vargs
,
unsigned
long
));
else
if
(
size_tflag
)
sprintf
(
s
,
"%"
PY_FORMAT_SIZE_T
"u"
,
va_arg
(
vargs
,
size_t
));
else
sprintf
(
s
,
"%u"
,
va_arg
(
vargs
,
unsigned
int
));
s
+=
strlen
(
s
);
break
;
case
'i'
:
sprintf
(
s
,
"%i"
,
va_arg
(
vargs
,
int
));
s
+=
strlen
(
s
);
break
;
case
'x'
:
sprintf
(
s
,
"%x"
,
va_arg
(
vargs
,
int
));
s
+=
strlen
(
s
);
break
;
case
's'
:
p
=
va_arg
(
vargs
,
char
*
);
i
=
strlen
(
p
);
if
(
n
>
0
&&
i
>
n
)
i
=
n
;
Py_MEMCPY
(
s
,
p
,
i
);
s
+=
i
;
break
;
case
'p'
:
sprintf
(
s
,
"%p"
,
va_arg
(
vargs
,
void
*
));
/* %p is ill-defined: ensure leading 0x. */
if
(
s
[
1
]
==
'X'
)
s
[
1
]
=
'x'
;
else
if
(
s
[
1
]
!=
'x'
) {
memmove
(
s
+
2
,
s
,
strlen
(
s
)
+
1
);
s
[
0
]
=
'0'
;
s
[
1
]
=
'x'
;
}
s
+=
strlen
(
s
);
break
;
case
'%'
:
*
s
++
=
'%'
;
break
;
default
:
strcpy
(
s
,
p
);
s
+=
strlen
(
s
);
goto
end
;
}
}
else
*
s
++
=
*
f
;
}
end
:
_PyString_Resize
(
&
string
,
s
-
PyString_AS_STRING
(
string
));
return
string
;
}
PyObject
*
PyString_FromFormat
(
const
char
*
format
, ...)
{
PyObject
*
ret
;
va_list
vargs
;
#ifdef
HAVE_STDARG_PROTOTYPES
va_start
(
vargs
,
format
);
#else
va_start
(
vargs
);
#endif
ret
=
PyString_FromFormatV
(
format
,
vargs
);
va_end
(
vargs
);
return
ret
;
}
PyObject
*
PyString_Decode
(
const
char
*
s
,
Py_ssize_t
size
,
const
char
*
encoding
,
const
char
*
errors
)
{
PyObject
*
v
,
*
str
;
str
=
PyString_FromStringAndSize
(
s
,
size
);
if
(
str
==
NULL
)
return
NULL
;
v
=
PyString_AsDecodedString
(
str
,
encoding
,
errors
);
Py_DECREF
(
str
);
return
v
;
}
PyObject
*
PyString_AsDecodedObject
(
PyObject
*
str
,
const
char
*
encoding
,
const
char
*
errors
)
{
PyObject
*
v
;
if
(!
PyString_Check
(
str
)) {
PyErr_BadArgument
();
goto
onError
;
}
if
(
encoding
==
NULL
) {
#ifdef
Py_USING_UNICODE
encoding
=
PyUnicode_GetDefaultEncoding
();
#else
PyErr_SetString
(
PyExc_ValueError
,
"no encoding specified"
);
goto
onError
;
#endif
}
/* Decode via the codec registry */
v
=
PyCodec_Decode
(
str
,
encoding
,
errors
);
if
(
v
==
NULL
)
goto
onError
;
return
v
;
onError
:
return
NULL
;
}
PyObject
*
PyString_AsDecodedString
(
PyObject
*
str
,
const
char
*
encoding
,
const
char
*
errors
)
{
PyObject
*
v
;
v
=
PyString_AsDecodedObject
(
str
,
encoding
,
errors
);
if
(
v
==
NULL
)
goto
onError
;
#ifdef
Py_USING_UNICODE
/* Convert Unicode to a string using the default encoding */
if
(
PyUnicode_Check
(
v
)) {
PyObject
*
temp
=
v
;
v
=
PyUnicode_AsEncodedString
(
v
,
NULL
,
NULL
);
Py_DECREF
(
temp
);
if
(
v
==
NULL
)
goto
onError
;
}
#endif
if
(!
PyString_Check
(
v
)) {
PyErr_Format
(
PyExc_TypeError
,
"decoder did not return a string object (type=%.400s)"
,
Py_TYPE
(
v
)
->
tp_name
);
Py_DECREF
(
v
);
goto
onError
;
}
return
v
;
onError
:
return
NULL
;
}
PyObject
*
PyString_Encode
(
const
char
*
s
,
Py_ssize_t
size
,
const
char
*
encoding
,
const
char
*
errors
)
{
PyObject
*
v
,
*
str
;
str
=
PyString_FromStringAndSize
(
s
,
size
);
if
(
str
==
NULL
)
return
NULL
;
v
=
PyString_AsEncodedString
(
str
,
encoding
,
errors
);
Py_DECREF
(
str
);
return
v
;
}
PyObject
*
PyString_AsEncodedObject
(
PyObject
*
str
,
const
char
*
encoding
,
const
char
*
errors
)
{
PyObject
*
v
;
if
(!
PyString_Check
(
str
)) {
PyErr_BadArgument
();
goto
onError
;
}
if
(
encoding
==
NULL
) {
#ifdef
Py_USING_UNICODE
encoding
=
PyUnicode_GetDefaultEncoding
();
#else
PyErr_SetString
(
PyExc_ValueError
,
"no encoding specified"
);
goto
onError
;
#endif
}
/* Encode via the codec registry */
v
=
PyCodec_Encode
(
str
,
encoding
,
errors
);
if
(
v
==
NULL
)
goto
onError
;
return
v
;
onError
:
return
NULL
;
}
PyObject
*
PyString_AsEncodedString
(
PyObject
*
str
,
const
char
*
encoding
,
const
char
*
errors
)
{
PyObject
*
v
;
v
=
PyString_AsEncodedObject
(
str
,
encoding
,
errors
);
if
(
v
==
NULL
)
goto
onError
;
#ifdef
Py_USING_UNICODE
/* Convert Unicode to a string using the default encoding */
if
(
PyUnicode_Check
(
v
)) {
PyObject
*
temp
=
v
;
v
=
PyUnicode_AsEncodedString
(
v
,
NULL
,
NULL
);
Py_DECREF
(
temp
);
if
(
v
==
NULL
)
goto
onError
;
}
#endif
if
(!
PyString_Check
(
v
)) {
PyErr_Format
(
PyExc_TypeError
,
"encoder did not return a string object (type=%.400s)"
,
Py_TYPE
(
v
)
->
tp_name
);
Py_DECREF
(
v
);
goto
onError
;
}
return
v
;
onError
:
return
NULL
;
}
static
void
string_dealloc
(
PyObject
*
op
)
{
switch
(
PyString_CHECK_INTERNED
(
op
)) {
case
SSTATE_NOT_INTERNED
:
break
;
case
SSTATE_INTERNED_MORTAL
:
/* revive dead object temporarily for DelItem */
Py_REFCNT
(
op
)
=
3
;
if
(
PyDict_DelItem
(
interned
,
op
)
!=
0
)
Py_FatalError
(
"deletion of interned string failed"
);
break
;
case
SSTATE_INTERNED_IMMORTAL
:
Py_FatalError
(
"Immortal interned string died."
);
default
:
Py_FatalError
(
"Inconsistent interned string state."
);
}
Py_TYPE
(
op
)
->
tp_free
(
op
);
}
/* Unescape a backslash-escaped string. If unicode is non-zero,
the string is a u-literal. If recode_encoding is non-zero,
the string is UTF-8 encoded and should be re-encoded in the
specified encoding. */
PyObject
*
PyString_DecodeEscape
(
const
char
*
s
,
Py_ssize_t
len
,
const
char
*
errors
,
Py_ssize_t
unicode
,
const
char
*
recode_encoding
)
{
int
c
;
char
*
p
,
*
buf
;
const
char
*
end
;
PyObject
*
v
;
Py_ssize_t
newlen
=
recode_encoding
?
4
*
len
:
len
;
v
=
PyString_FromStringAndSize
((
char
*
)
NULL
,
newlen
);
if
(
v
==
NULL
)
return
NULL
;
p
=
buf
=
PyString_AsString
(
v
);
end
=
s
+
len
;
while
(
s
<
end
) {
if
(
*
s
!=
'\\'
) {
non_esc
:
#ifdef
Py_USING_UNICODE
if
(
recode_encoding
&&
(
*
s
&
0x80
)) {
PyObject
*
u
,
*
w
;
char
*
r
;
const
char
*
t
;
Py_ssize_t
rn
;
t
=
s
;
/* Decode non-ASCII bytes as UTF-8. */
while
(
t
<
end
&&
(
*
t
&
0x80
))
t
++
;
u
=
PyUnicode_DecodeUTF8
(
s
,
t
-
s
,
errors
);
if
(!
u
) goto
failed
;
/* Recode them in target encoding. */
w
=
PyUnicode_AsEncodedString
(
u
,
recode_encoding
,
errors
);
Py_DECREF
(
u
);
if
(!
w
) goto
failed
;
/* Append bytes to output buffer. */
assert
(
PyString_Check
(
w
));
r
=
PyString_AS_STRING
(
w
);
rn
=
PyString_GET_SIZE
(
w
);
Py_MEMCPY
(
p
,
r
,
rn
);
p
+=
rn
;
Py_DECREF
(
w
);
s
=
t
;
}
else
{
*
p
++
=
*
s
++
;
}
#else
*
p
++
=
*
s
++
;
#endif
continue
;
}
s
++
;
if
(
s
==
end
) {
PyErr_SetString
(
PyExc_ValueError
,
"Trailing \\ in string"
);
goto
failed
;
}
switch
(
*
s
++
) {
/* XXX This assumes ASCII! */
case
'\n'
:
break
;
case
'\\'
:
*
p
++
=
'\\'
;
break
;
case
'\''
:
*
p
++
=
'\''
;
break
;
case
'\"'
:
*
p
++
=
'\"'
;
break
;
case
'b'
:
*
p
++
=
'\b'
;
break
;
case
'f'
:
*
p
++
=
'\014'
;
break
;
/* FF */
case
't'
:
*
p
++
=
'\t'
;
break
;
case
'n'
:
*
p
++
=
'\n'
;
break
;
case
'r'
:
*
p
++
=
'\r'
;
break
;
case
'v'
:
*
p
++
=
'\013'
;
break
;
/* VT */
case
'a'
:
*
p
++
=
'\007'
;
break
;
/* BEL, not classic C */
case
'0'
:
case
'1'
:
case
'2'
:
case
'3'
:
case
'4'
:
case
'5'
:
case
'6'
:
case
'7'
:
c
=
s
[
-1
]
-
'0'
;
if
(
s
<
end
&&
'0'
<=
*
s
&&
*
s
<=
'7'
) {
c
=
(
c
<<
3
)
+
*
s
++
-
'0'
;
if
(
s
<
end
&&
'0'
<=
*
s
&&
*
s
<=
'7'
)
c
=
(
c
<<
3
)
+
*
s
++
-
'0'
;
}
*
p
++
=
c
;
break
;
case
'x'
:
if
(
s
+
1
<
end
&&
isxdigit
(
Py_CHARMASK
(
s
[
0
]))
&&
isxdigit
(
Py_CHARMASK
(
s
[
1
])))
{
unsigned
int
x
=
0
;
c
=
Py_CHARMASK
(
*
s
);
s
++
;
if
(
isdigit
(
c
))
x
=
c
-
'0'
;
else
if
(
islower
(
c
))
x
=
10
+
c
-
'a'
;
else
x
=
10
+
c
-
'A'
;
x
=
x
<<
4
;
c
=
Py_CHARMASK
(
*
s
);
s
++
;
if
(
isdigit
(
c
))
x
+=
c
-
'0'
;
else
if
(
islower
(
c
))
x
+=
10
+
c
-
'a'
;
else
x
+=
10
+
c
-
'A'
;
*
p
++
=
x
;
break
;
}
if
(!
errors
||
strcmp
(
errors
,
"strict"
)
==
0
) {
PyErr_SetString
(
PyExc_ValueError
,
"invalid \\x escape"
);
goto
failed
;
}
if
(
strcmp
(
errors
,
"replace"
)
==
0
) {
*
p
++
=
'?'
;
}
else
if
(
strcmp
(
errors
,
"ignore"
)
==
0
)
/* do nothing */
;
else
{
PyErr_Format
(
PyExc_ValueError
,
"decoding error; "
"unknown error handling code: %.400s"
,
errors
);
goto
failed
;
}
#ifndef
Py_USING_UNICODE
case
'u'
:
case
'U'
:
case
'N'
:
if
(
unicode
) {
PyErr_SetString
(
PyExc_ValueError
,
"Unicode escapes not legal "
"when Unicode disabled"
);
goto
failed
;
}
#endif
default
:
*
p
++
=
'\\'
;
s
--
;
goto
non_esc
;
/* an arbitry number of unescaped
UTF-8 bytes may follow. */
}
}
if
(
p
-
buf
<
newlen
)
_PyString_Resize
(
&
v
,
p
-
buf
);
return
v
;
failed
:
Py_DECREF
(
v
);
return
NULL
;
}
/* -------------------------------------------------------------------- */
/* object api */
static
Py_ssize_t
string_getsize
(register
PyObject
*
op
)
{
char
*
s
;
Py_ssize_t
len
;
if
(
PyString_AsStringAndSize
(
op
,
&
s
,
&
len
))
return
-1
;
return
len
;
}
static
/*const*/
char
*
string_getbuffer
(register
PyObject
*
op
)
{
char
*
s
;
Py_ssize_t
len
;
if
(
PyString_AsStringAndSize
(
op
,
&
s
,
&
len
))
return
NULL
;
return
s
;
}
Py_ssize_t
PyString_Size
(register
PyObject
*
op
)
{
if
(!
PyString_Check
(
op
))
return
string_getsize
(
op
);
return
Py_SIZE
(
op
);
}
/*const*/
char
*
PyString_AsString
(register
PyObject
*
op
)
{
if
(!
PyString_Check
(
op
))
return
string_getbuffer
(
op
);
return
((
PyStringObject
*
)
op
)
->
ob_sval
;
}
int
PyString_AsStringAndSize
(register
PyObject
*
obj
,
register
char
*
*
s
,
register
Py_ssize_t
*
len
)
{
if
(
s
==
NULL
) {
PyErr_BadInternalCall
();
return
-1
;
}
if
(!
PyString_Check
(
obj
)) {
#ifdef
Py_USING_UNICODE
if
(
PyUnicode_Check
(
obj
)) {
obj
=
_PyUnicode_AsDefaultEncodedString
(
obj
,
NULL
);
if
(
obj
==
NULL
)
return
-1
;
}
else
#endif
{
PyErr_Format
(
PyExc_TypeError
,
"expected string or Unicode object, "
"%.200s found"
,
Py_TYPE
(
obj
)
->
tp_name
);
return
-1
;
}
}
*
s
=
PyString_AS_STRING
(
obj
);
if
(
len
!=
NULL
)
*
len
=
PyString_GET_SIZE
(
obj
);
else
if
(
strlen
(
*
s
)
!=
(
size_t
)
PyString_GET_SIZE
(
obj
)) {
PyErr_SetString
(
PyExc_TypeError
,
"expected string without null bytes"
);
return
-1
;
}
return
0
;
}
/* -------------------------------------------------------------------- */
/* Methods */
#include
"stringlib/stringdefs.h"
#include
"stringlib/fastsearch.h"
#include
"stringlib/count.h"
#include
"stringlib/find.h"
#include
"stringlib/partition.h"
#define
_Py_InsertThousandsGrouping
_PyString_InsertThousandsGrouping
#include
"stringlib/localeutil.h"
static
int
string_print
(
PyStringObject
*
op
,
FILE
*
fp
,
int
flags
)
{
Py_ssize_t
i
,
str_len
;
char
c
;
int
quote
;
/* XXX Ought to check for interrupts when writing long strings */
if
(!
PyString_CheckExact
(
op
)) {
int
ret
;
/* A str subclass may have its own __str__ method. */
op
=
(
PyStringObject
*
)
PyObject_Str
((
PyObject
*
)
op
);
if
(
op
==
NULL
)
return
-1
;
ret
=
string_print
(
op
,
fp
,
flags
);
Py_DECREF
(
op
);
return
ret
;
}
if
(
flags
&
Py_PRINT_RAW
) {
char
*
data
=
op
->
ob_sval
;
Py_ssize_t
size
=
Py_SIZE
(
op
);
Py_BEGIN_ALLOW_THREADS
while
(
size
>
INT_MAX
) {
/* Very long strings cannot be written atomically.
* But don't write exactly INT_MAX bytes at a time
* to avoid memory aligment issues.
*/
const
int
chunk_size
=
INT_MAX
&
~
0x3FFF
;
fwrite
(
data
,
1
,
chunk_size
,
fp
);
data
+=
chunk_size
;
size
-=
chunk_size
;
}
#ifdef
__VMS
if
(
size
)
fwrite
(
data
, (
int
)
size
,
1
,
fp
);
#else
fwrite
(
data
,
1
, (
int
)
size
,
fp
);
#endif
Py_END_ALLOW_THREADS
return
0
;
}
/* figure out which quote to use; single is preferred */
quote
=
'\''
;
if
(
memchr
(
op
->
ob_sval
,
'\''
,
Py_SIZE
(
op
))
&&
!
memchr
(
op
->
ob_sval
,
'"'
,
Py_SIZE
(
op
)))
quote
=
'"'
;
str_len
=
Py_SIZE
(
op
);
Py_BEGIN_ALLOW_THREADS
fputc
(
quote
,
fp
);
for
(
i
=
0
;
i
<
str_len
;
i
++
) {
/* Since strings are immutable and the caller should have a
reference, accessing the interal buffer should not be an issue
with the GIL released. */
c
=
op
->
ob_sval
[
i
];
if
(
c
==
quote
||
c
==
'\\'
)
fprintf
(
fp
,
"\\%c"
,
c
);
else
if
(
c
==
'\t'
)
fprintf
(
fp
,
"\\t"
);
else
if
(
c
==
'\n'
)
fprintf
(
fp
,
"\\n"
);
else
if
(
c
==
'\r'
)
fprintf
(
fp
,
"\\r"
);
else
if
(
c
<
' '
||
c
>=
0x7f
)
fprintf
(
fp
,
"\\x%02x"
,
c
&
0xff
);
else
fputc
(
c
,
fp
);
}
fputc
(
quote
,
fp
);
Py_END_ALLOW_THREADS
return
0
;
}
PyObject
*
PyString_Repr
(
PyObject
*
obj
,
int
smartquotes
)
{
register
PyStringObject
*
op
=
(
PyStringObject
*
)
obj
;
size_t
newsize
=
2
+
4
*
Py_SIZE
(
op
);
PyObject
*
v
;
if
(
newsize
>
PY_SSIZE_T_MAX
||
newsize
/
4
!=
Py_SIZE
(
op
)) {
PyErr_SetString
(
PyExc_OverflowError
,
"string is too large to make repr"
);
return
NULL
;
}
v
=
PyString_FromStringAndSize
((
char
*
)
NULL
,
newsize
);
if
(
v
==
NULL
) {
return
NULL
;
}
else
{
register
Py_ssize_t
i
;
register
char
c
;
register
char
*
p
;
int
quote
;
/* figure out which quote to use; single is preferred */
quote
=
'\''
;
if
(
smartquotes
&&
memchr
(
op
->
ob_sval
,
'\''
,
Py_SIZE
(
op
))
&&
!
memchr
(
op
->
ob_sval
,
'"'
,
Py_SIZE
(
op
)))
quote
=
'"'
;
p
=
PyString_AS_STRING
(
v
);
*
p
++
=
quote
;
for
(
i
=
0
;
i
<
Py_SIZE
(
op
);
i
++
) {
/* There's at least enough room for a hex escape
and a closing quote. */
assert
(
newsize
-
(
p
-
PyString_AS_STRING
(
v
)) >=
5
);
c
=
op
->
ob_sval
[
i
];
if
(
c
==
quote
||
c
==
'\\'
)
*
p
++
=
'\\'
,
*
p
++
=
c
;
else
if
(
c
==
'\t'
)
*
p
++
=
'\\'
,
*
p
++
=
't'
;
else
if
(
c
==
'\n'
)
*
p
++
=
'\\'
,
*
p
++
=
'n'
;
else
if
(
c
==
'\r'
)
*
p
++
=
'\\'
,
*
p
++
=
'r'
;
else
if
(
c
<
' '
||
c
>=
0x7f
) {
/* For performance, we don't want to call
PyOS_snprintf here (extra layers of
function call). */
sprintf
(
p
,
"\\x%02x"
,
c
&
0xff
);
p
+=
4
;
}
else
*
p
++
=
c
;
}
assert
(
newsize
-
(
p
-
PyString_AS_STRING
(
v
)) >=
1
);
*
p
++
=
quote
;
*
p
=
'\0'
;
_PyString_Resize
(
&
v
, (
p
-
PyString_AS_STRING
(
v
)));
return
v
;
}
}
static
PyObject
*
string_repr
(
PyObject
*
op
)
{
return
PyString_Repr
(
op
,
1
);
}
static
PyObject
*
string_str
(
PyObject
*
s
)
{
assert
(
PyString_Check
(
s
));
if
(
PyString_CheckExact
(
s
)) {
Py_INCREF
(
s
);
return
s
;
}
else
{
/* Subtype -- return genuine string with the same value. */
PyStringObject
*
t
=
(
PyStringObject
*
)
s
;
return
PyString_FromStringAndSize
(
t
->
ob_sval
,
Py_SIZE
(
t
));
}
}
static
Py_ssize_t
string_length
(
PyStringObject
*
a
)
{
return
Py_SIZE
(
a
);
}
static
PyObject
*
string_concat
(register
PyStringObject
*
a
, register
PyObject
*
bb
)
{
register
Py_ssize_t
size
;
register
PyStringObject
*
op
;
if
(!
PyString_Check
(
bb
)) {
#ifdef
Py_USING_UNICODE
if
(
PyUnicode_Check
(
bb
))
return
PyUnicode_Concat
((
PyObject
*
)
a
,
bb
);
#endif
if
(
PyByteArray_Check
(
bb
))
return
PyByteArray_Concat
((
PyObject
*
)
a
,
bb
);
PyErr_Format
(
PyExc_TypeError
,
"cannot concatenate 'str' and '%.200s' objects"
,
Py_TYPE
(
bb
)
->
tp_name
);
return
NULL
;
}
#define
b
((PyStringObject *)bb)
/* Optimize cases with empty left or right operand */
if
((
Py_SIZE
(
a
)
==
0
||
Py_SIZE
(
b
)
==
0
)
&&
PyString_CheckExact
(
a
)
&&
PyString_CheckExact
(
b
)) {
if
(
Py_SIZE
(
a
)
==
0
) {
Py_INCREF
(
bb
);
return
bb
;
}
Py_INCREF
(
a
);
return
(
PyObject
*
)
a
;
}
size
=
Py_SIZE
(
a
)
+
Py_SIZE
(
b
);
/* Check that string sizes are not negative, to prevent an
overflow in cases where we are passed incorrectly-created
strings with negative lengths (due to a bug in other code).
*/
if
(
Py_SIZE
(
a
)
<
0
||
Py_SIZE
(
b
)
<
0
||
Py_SIZE
(
a
)
>
PY_SSIZE_T_MAX
-
Py_SIZE
(
b
)) {
PyErr_SetString
(
PyExc_OverflowError
,
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL