File tree 2 files changed +17
-12
lines changed
2 files changed +17
-12
lines changed Original file line number Diff line number Diff line change @@ -1736,7 +1736,7 @@ def test_basic(self):
1736
1736
writer .write_char ('=' )
1737
1737
1738
1738
# test PyUnicodeWriter_WriteSubstring()
1739
- writer .write_substring ("[long]" , 1 , 5 );
1739
+ writer .write_substring ("[long]" , 1 , 5 )
1740
1740
1741
1741
# test PyUnicodeWriter_WriteStr()
1742
1742
writer .write_str (" value " )
@@ -1862,6 +1862,10 @@ def test_ucs4(self):
1862
1862
with self .assertRaises (ValueError ):
1863
1863
writer .write_ucs4 ("text" , - 1 )
1864
1864
1865
+ def test_substring_empty (self ):
1866
+ writer = self .create_writer (0 )
1867
+ writer .write_substring ("abc" , 1 , 1 )
1868
+ self .assertEqual (writer .finish (), '' )
1865
1869
1866
1870
1867
1871
@unittest .skipIf (ctypes is None , 'need ctypes' )
Original file line number Diff line number Diff line change @@ -13637,27 +13637,28 @@ int
13637
13637
_PyUnicodeWriter_WriteSubstring (_PyUnicodeWriter * writer , PyObject * str ,
13638
13638
Py_ssize_t start , Py_ssize_t end )
13639
13639
{
13640
- Py_UCS4 maxchar ;
13641
- Py_ssize_t len ;
13642
-
13643
13640
assert (0 <= start );
13644
13641
assert (end <= PyUnicode_GET_LENGTH (str ));
13645
13642
assert (start <= end );
13646
13643
13647
- if (end == 0 )
13648
- return 0 ;
13649
-
13650
13644
if (start == 0 && end == PyUnicode_GET_LENGTH (str ))
13651
13645
return _PyUnicodeWriter_WriteStr (writer , str );
13652
13646
13653
- if (PyUnicode_MAX_CHAR_VALUE (str ) > writer -> maxchar )
13647
+ Py_ssize_t len = end - start ;
13648
+ if (len == 0 ) {
13649
+ return 0 ;
13650
+ }
13651
+
13652
+ Py_UCS4 maxchar ;
13653
+ if (PyUnicode_MAX_CHAR_VALUE (str ) > writer -> maxchar ) {
13654
13654
maxchar = _PyUnicode_FindMaxChar (str , start , end );
13655
- else
13655
+ }
13656
+ else {
13656
13657
maxchar = writer -> maxchar ;
13657
- len = end - start ;
13658
-
13659
- if (_PyUnicodeWriter_Prepare (writer , len , maxchar ) < 0 )
13658
+ }
13659
+ if (_PyUnicodeWriter_Prepare (writer , len , maxchar ) < 0 ) {
13660
13660
return -1 ;
13661
+ }
13661
13662
13662
13663
_PyUnicode_FastCopyCharacters (writer -> buffer , writer -> pos ,
13663
13664
str , start , len );
You can’t perform that action at this time.
0 commit comments