Skip to content

Commit ae812b6

Browse files
committed
Address review comments
1 parent 0e7bce1 commit ae812b6

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

Objects/unicodeobject.c

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -684,25 +684,32 @@ backslashreplace(_PyBytesWriter *writer, char *str,
684684
}
685685

686686
static Py_ssize_t
687-
xmlcharrefreplace_get_incr(Py_UCS4 ch)
687+
get_xmlcharref_length(Py_UCS4 ch)
688688
{
689689
Py_ssize_t incr;
690690

691-
if (ch < 10)
692-
incr = 2+1+1;
693-
else if (ch < 100)
694-
incr = 2+2+1;
695-
else if (ch < 1000)
696-
incr = 2+3+1;
697-
else if (ch < 10000)
698-
incr = 2+4+1;
699-
else if (ch < 100000)
700-
incr = 2+5+1;
701-
else if (ch < 1000000)
702-
incr = 2+6+1;
691+
/* `2 + 1` part is `&#` + `;` */
692+
if (ch < 10) {
693+
incr = 2 + 1 + 1;
694+
}
695+
else if (ch < 100) {
696+
incr = 2 + 2 + 1;
697+
}
698+
else if (ch < 1000) {
699+
incr = 2 + 3 + 1;
700+
}
701+
else if (ch < 10000) {
702+
incr = 2 + 4 + 1;
703+
}
704+
else if (ch < 100000) {
705+
incr = 2 + 5 + 1;
706+
}
707+
else if (ch < 1000000) {
708+
incr = 2 + 6 + 1;
709+
}
703710
else {
704711
assert(ch <= MAX_UNICODE);
705-
incr = 2+7+1;
712+
incr = 2 + 7 + 1;
706713
}
707714

708715
return incr;
@@ -726,7 +733,7 @@ xmlcharrefreplace(_PyBytesWriter *writer, char *str,
726733
/* determine replacement size */
727734
for (i = collstart; i < collend; ++i) {
728735
ch = PyUnicode_READ(kind, data, i);
729-
incr = xmlcharrefreplace_get_incr(ch);
736+
incr = get_xmlcharref_length(ch);
730737

731738
if (size > PY_SSIZE_T_MAX - incr) {
732739
PyErr_SetString(PyExc_OverflowError,
@@ -743,7 +750,7 @@ xmlcharrefreplace(_PyBytesWriter *writer, char *str,
743750
/* generate replacement */
744751
for (i = collstart; i < collend; ++i) {
745752
ch = PyUnicode_READ(kind, data, i);
746-
incr = xmlcharrefreplace_get_incr(ch);
753+
incr = get_xmlcharref_length(ch);
747754
size = PyOS_snprintf(str, incr + 1, "&#%d;", ch);
748755
if (size < 0) {
749756
return NULL;

0 commit comments

Comments
 (0)