Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8a1a6cf

Browse files
committed
Add _PyUnicodeWriter_WriteCharInline()
1 parent d949126 commit 8a1a6cf

1 file changed

Lines changed: 35 additions & 71 deletions

File tree

Objects/unicodeobject.c

Lines changed: 35 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ static PyObject *unicode_empty = NULL;
196196
return unicode_empty; \
197197
} while (0)
198198

199+
/* Forward declaration */
200+
Py_LOCAL_INLINE(int)
201+
_PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch);
202+
199203
/* List of static strings. */
200204
static _Py_Identifier *static_strings = NULL;
201205

@@ -2432,10 +2436,8 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer,
24322436
"character argument not in range(0x110000)");
24332437
return NULL;
24342438
}
2435-
if (_PyUnicodeWriter_Prepare(writer, 1, ordinal) == -1)
2439+
if (_PyUnicodeWriter_WriteCharInline(writer, ordinal) < 0)
24362440
return NULL;
2437-
PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ordinal);
2438-
writer->pos++;
24392441
break;
24402442
}
24412443

@@ -2636,10 +2638,8 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer,
26362638
}
26372639

26382640
case '%':
2639-
if (_PyUnicodeWriter_Prepare(writer, 1, '%') == 1)
2641+
if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0)
26402642
return NULL;
2641-
PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '%');
2642-
writer->pos++;
26432643
break;
26442644

26452645
default:
@@ -4282,18 +4282,14 @@ PyUnicode_DecodeUTF7Stateful(const char *s,
42824282
/* expecting a second surrogate */
42834283
if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) {
42844284
Py_UCS4 ch2 = Py_UNICODE_JOIN_SURROGATES(surrogate, outCh);
4285-
if (_PyUnicodeWriter_Prepare(&writer, 1, ch2) == -1)
4285+
if (_PyUnicodeWriter_WriteCharInline(&writer, ch2) < 0)
42864286
goto onError;
4287-
PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch2);
4288-
writer.pos++;
42894287
surrogate = 0;
42904288
continue;
42914289
}
42924290
else {
4293-
if (_PyUnicodeWriter_Prepare(&writer, 1, surrogate) == -1)
4291+
if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0)
42944292
goto onError;
4295-
PyUnicode_WRITE(writer.kind, writer.data, writer.pos, surrogate);
4296-
writer.pos++;
42974293
surrogate = 0;
42984294
}
42994295
}
@@ -4302,21 +4298,17 @@ PyUnicode_DecodeUTF7Stateful(const char *s,
43024298
surrogate = outCh;
43034299
}
43044300
else {
4305-
if (_PyUnicodeWriter_Prepare(&writer, 1, outCh) == -1)
4301+
if (_PyUnicodeWriter_WriteCharInline(&writer, outCh) < 0)
43064302
goto onError;
4307-
PyUnicode_WRITE(writer.kind, writer.data, writer.pos, outCh);
4308-
writer.pos++;
43094303
}
43104304
}
43114305
}
43124306
else { /* now leaving a base-64 section */
43134307
inShift = 0;
43144308
s++;
43154309
if (surrogate) {
4316-
if (_PyUnicodeWriter_Prepare(&writer, 1, surrogate) == -1)
4310+
if (_PyUnicodeWriter_WriteCharInline(&writer, surrogate) < 0)
43174311
goto onError;
4318-
PyUnicode_WRITE(writer.kind, writer.data, writer.pos, surrogate);
4319-
writer.pos++;
43204312
surrogate = 0;
43214313
}
43224314
if (base64bits > 0) { /* left-over bits */
@@ -4336,10 +4328,8 @@ PyUnicode_DecodeUTF7Stateful(const char *s,
43364328
if (ch != '-') {
43374329
/* '-' is absorbed; other terminating
43384330
characters are preserved */
4339-
if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1)
4331+
if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0)
43404332
goto onError;
4341-
PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch);
4342-
writer.pos++;
43434333
}
43444334
}
43454335
}
@@ -4348,10 +4338,8 @@ PyUnicode_DecodeUTF7Stateful(const char *s,
43484338
s++; /* consume '+' */
43494339
if (s < e && *s == '-') { /* '+-' encodes '+' */
43504340
s++;
4351-
if (_PyUnicodeWriter_Prepare(&writer, 1, '+') == -1)
4341+
if (_PyUnicodeWriter_WriteCharInline(&writer, '+') < 0)
43524342
goto onError;
4353-
PyUnicode_WRITE(writer.kind, writer.data, writer.pos, '+');
4354-
writer.pos++;
43554343
}
43564344
else { /* begin base64-encoded section */
43574345
inShift = 1;
@@ -4361,10 +4349,8 @@ PyUnicode_DecodeUTF7Stateful(const char *s,
43614349
}
43624350
else if (DECODE_DIRECT(ch)) { /* character decodes as itself */
43634351
s++;
4364-
if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1)
4352+
if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0)
43654353
goto onError;
4366-
PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch);
4367-
writer.pos++;
43684354
}
43694355
else {
43704356
startinpos = s-starts;
@@ -4711,10 +4697,8 @@ PyUnicode_DecodeUTF8Stateful(const char *s,
47114697
endinpos = startinpos + ch - 1;
47124698
break;
47134699
default:
4714-
if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1)
4700+
if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0)
47154701
goto onError;
4716-
PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch);
4717-
writer.pos++;
47184702
continue;
47194703
}
47204704

@@ -4970,10 +4954,8 @@ PyUnicode_DecodeUTF32Stateful(const char *s,
49704954
}
49714955
else {
49724956
if (ch < 0x110000) {
4973-
if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1)
4957+
if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0)
49744958
goto onError;
4975-
PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch);
4976-
writer.pos++;
49774959
q += 4;
49784960
continue;
49794961
}
@@ -5227,10 +5209,8 @@ PyUnicode_DecodeUTF16Stateful(const char *s,
52275209
endinpos = startinpos + 2;
52285210
break;
52295211
default:
5230-
if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1)
5212+
if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0)
52315213
goto onError;
5232-
PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch);
5233-
writer.pos++;
52345214
continue;
52355215
}
52365216

@@ -5469,10 +5449,8 @@ PyUnicode_DecodeUnicodeEscape(const char *s,
54695449
if (*s != '\\') {
54705450
x = (unsigned char)*s;
54715451
s++;
5472-
if (_PyUnicodeWriter_Prepare(&writer, 1, x) == -1)
5452+
if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0)
54735453
goto onError;
5474-
PyUnicode_WRITE(writer.kind, writer.data, writer.pos, x);
5475-
writer.pos++;
54765454
continue;
54775455
}
54785456

@@ -5492,10 +5470,8 @@ PyUnicode_DecodeUnicodeEscape(const char *s,
54925470
/* \x escapes */
54935471
#define WRITECHAR(ch) \
54945472
do { \
5495-
if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) \
5473+
if (_PyUnicodeWriter_WriteCharInline(&writer, (ch)) < 0) \
54965474
goto onError; \
5497-
PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); \
5498-
writer.pos++; \
54995475
} while(0)
55005476

55015477
case '\n': break;
@@ -5825,10 +5801,8 @@ PyUnicode_DecodeRawUnicodeEscape(const char *s,
58255801
/* Non-escape characters are interpreted as Unicode ordinals */
58265802
if (*s != '\\') {
58275803
x = (unsigned char)*s++;
5828-
if (_PyUnicodeWriter_Prepare(&writer, 1, x) == -1)
5804+
if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0)
58295805
goto onError;
5830-
PyUnicode_WRITE(writer.kind, writer.data, writer.pos, x);
5831-
writer.pos++;
58325806
continue;
58335807
}
58345808
startinpos = s-starts;
@@ -5840,10 +5814,8 @@ PyUnicode_DecodeRawUnicodeEscape(const char *s,
58405814
if (*s != '\\')
58415815
break;
58425816
x = (unsigned char)*s++;
5843-
if (_PyUnicodeWriter_Prepare(&writer, 1, x) == -1)
5817+
if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0)
58445818
goto onError;
5845-
PyUnicode_WRITE(writer.kind, writer.data, writer.pos, x);
5846-
writer.pos++;
58475819
}
58485820
if (((s - bs) & 1) == 0 ||
58495821
s >= end ||
@@ -5876,10 +5848,8 @@ PyUnicode_DecodeRawUnicodeEscape(const char *s,
58765848
x += 10 + c - 'A';
58775849
}
58785850
if (x <= MAX_UNICODE) {
5879-
if (_PyUnicodeWriter_Prepare(&writer, 1, x) == -1)
5851+
if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0)
58805852
goto onError;
5881-
PyUnicode_WRITE(writer.kind, writer.data, writer.pos, x);
5882-
writer.pos++;
58835853
}
58845854
else {
58855855
endinpos = s-starts;
@@ -6059,10 +6029,8 @@ _PyUnicode_DecodeUnicodeInternal(const char *s,
60596029
}
60606030
#endif
60616031

6062-
if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1)
6032+
if (_PyUnicodeWriter_WriteCharInline(&writer, ch) < 0)
60636033
goto onError;
6064-
PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch);
6065-
writer.pos++;
60666034
continue;
60676035

60686036
error:
@@ -7409,10 +7377,8 @@ PyUnicode_DecodeCharmap(const char *s,
74097377
continue;
74107378
}
74117379

7412-
if (_PyUnicodeWriter_Prepare(&writer, 1, x) == -1)
7380+
if (_PyUnicodeWriter_WriteCharInline(&writer, x) < 0)
74137381
goto onError;
7414-
PyUnicode_WRITE(writer.kind, writer.data, writer.pos, x);
7415-
writer.pos++;
74167382
++s;
74177383
}
74187384
}
@@ -7451,12 +7417,10 @@ PyUnicode_DecodeCharmap(const char *s,
74517417
goto onError;
74527418
}
74537419

7454-
if (_PyUnicodeWriter_Prepare(&writer, 1, value) == -1) {
7420+
if (_PyUnicodeWriter_WriteCharInline(&writer, value) < 0) {
74557421
Py_DECREF(x);
74567422
goto onError;
74577423
}
7458-
PyUnicode_WRITE(writer.kind, writer.data, writer.pos, value);
7459-
writer.pos++;
74607424
}
74617425
else if (PyUnicode_Check(x)) {
74627426
if (PyUnicode_READY(x) == -1) {
@@ -7467,12 +7431,10 @@ PyUnicode_DecodeCharmap(const char *s,
74677431
Py_UCS4 value = PyUnicode_READ_CHAR(x, 0);
74687432
if (value == 0xFFFE)
74697433
goto Undefined;
7470-
if (_PyUnicodeWriter_Prepare(&writer, 1, value) == -1) {
7434+
if (_PyUnicodeWriter_WriteCharInline(&writer, value) < 0) {
74717435
Py_DECREF(x);
74727436
goto onError;
74737437
}
7474-
PyUnicode_WRITE(writer.kind, writer.data, writer.pos, value);
7475-
writer.pos++;
74767438
}
74777439
else {
74787440
writer.overallocate = 1;
@@ -12959,8 +12921,8 @@ _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer,
1295912921
return 0;
1296012922
}
1296112923

12962-
int
12963-
_PyUnicodeWriter_WriteChar(_PyUnicodeWriter *writer, Py_UCS4 ch)
12924+
Py_LOCAL_INLINE(int)
12925+
_PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch)
1296412926
{
1296512927
if (_PyUnicodeWriter_Prepare(writer, 1, ch) < 0)
1296612928
return -1;
@@ -12969,6 +12931,12 @@ _PyUnicodeWriter_WriteChar(_PyUnicodeWriter *writer, Py_UCS4 ch)
1296912931
return 0;
1297012932
}
1297112933

12934+
int
12935+
_PyUnicodeWriter_WriteChar(_PyUnicodeWriter *writer, Py_UCS4 ch)
12936+
{
12937+
return _PyUnicodeWriter_WriteCharInline(writer, ch);
12938+
}
12939+
1297212940
int
1297312941
_PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str)
1297412942
{
@@ -13873,10 +13841,8 @@ unicode_format_arg_format(struct unicode_formatter_t *ctx,
1387313841
ctx->writer.overallocate = 0;
1387413842

1387513843
if (arg->ch == '%') {
13876-
if (_PyUnicodeWriter_Prepare(writer, 1, '%') == -1)
13844+
if (_PyUnicodeWriter_WriteCharInline(writer, '%') < 0)
1387713845
return -1;
13878-
PyUnicode_WRITE(writer->kind, writer->data, writer->pos, '%');
13879-
writer->pos += 1;
1388013846
return 1;
1388113847
}
1388213848

@@ -13951,10 +13917,8 @@ unicode_format_arg_format(struct unicode_formatter_t *ctx,
1395113917
return -1;
1395213918
if (arg->width == -1 && arg->prec == -1) {
1395313919
/* Fast path */
13954-
if (_PyUnicodeWriter_Prepare(writer, 1, ch) == -1)
13920+
if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0)
1395513921
return -1;
13956-
PyUnicode_WRITE(writer->kind, writer->data, writer->pos, ch);
13957-
writer->pos += 1;
1395813922
return 1;
1395913923
}
1396013924
*p_str = PyUnicode_FromOrdinal(ch);

0 commit comments

Comments
 (0)