@@ -684,25 +684,32 @@ backslashreplace(_PyBytesWriter *writer, char *str,
684
684
}
685
685
686
686
static Py_ssize_t
687
- xmlcharrefreplace_get_incr (Py_UCS4 ch )
687
+ get_xmlcharref_length (Py_UCS4 ch )
688
688
{
689
689
Py_ssize_t incr ;
690
690
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
+ }
703
710
else {
704
711
assert (ch <= MAX_UNICODE );
705
- incr = 2 + 7 + 1 ;
712
+ incr = 2 + 7 + 1 ;
706
713
}
707
714
708
715
return incr ;
@@ -726,7 +733,7 @@ xmlcharrefreplace(_PyBytesWriter *writer, char *str,
726
733
/* determine replacement size */
727
734
for (i = collstart ; i < collend ; ++ i ) {
728
735
ch = PyUnicode_READ (kind , data , i );
729
- incr = xmlcharrefreplace_get_incr (ch );
736
+ incr = get_xmlcharref_length (ch );
730
737
731
738
if (size > PY_SSIZE_T_MAX - incr ) {
732
739
PyErr_SetString (PyExc_OverflowError ,
@@ -743,7 +750,7 @@ xmlcharrefreplace(_PyBytesWriter *writer, char *str,
743
750
/* generate replacement */
744
751
for (i = collstart ; i < collend ; ++ i ) {
745
752
ch = PyUnicode_READ (kind , data , i );
746
- incr = xmlcharrefreplace_get_incr (ch );
753
+ incr = get_xmlcharref_length (ch );
747
754
size = PyOS_snprintf (str , incr + 1 , "&#%d;" , ch );
748
755
if (size < 0 ) {
749
756
return NULL ;
0 commit comments