@@ -1292,31 +1292,35 @@ AK_CPL_Free(AK_CodePointLine* cpl)
12921292
12931293// Returns 0 on success, -1 on failure.
12941294static inline int
1295- AK_CPL_resize_buffer (AK_CodePointLine * cpl , Py_ssize_t increment )
1296- {
1297- // TODO: this does ensure that there is increment sized data! must loop
1298- if ( AK_UNLIKELY (( cpl -> buffer_count + increment ) >= cpl -> buffer_capacity ) ) {
1299- // realloc
1300- cpl -> buffer_capacity *= 2 ; // needs to be max of this or element_length
1295+ AK_CPL_resize_buffer (AK_CodePointLine * cpl , Py_ssize_t increment ) {
1296+ Py_ssize_t target = cpl -> buffer_count + increment ;
1297+ if ( AK_UNLIKELY ( target >= cpl -> buffer_capacity )) {
1298+ while ( cpl -> buffer_capacity < target ) {
1299+ cpl -> buffer_capacity <<= 1 ;
1300+ }
13011301 cpl -> buffer = PyMem_Realloc (cpl -> buffer ,
13021302 sizeof (Py_UCS4 ) * cpl -> buffer_capacity );
1303- if (cpl -> buffer == NULL ) return -1 ;
1304-
1303+ if (cpl -> buffer == NULL ) {
1304+ return -1 ;
1305+ }
13051306 cpl -> buffer_current_ptr = cpl -> buffer + cpl -> buffer_count ;
13061307 }
13071308 return 0 ;
13081309}
13091310
1311+
1312+ // NOTE: we only add one offset at time, so this does not need to take an increment argument.
13101313static inline int
1311- AK_CPL_resize_offsets (AK_CodePointLine * cpl )
1312- {
1314+ AK_CPL_resize_offsets (AK_CodePointLine * cpl ) {
13131315 // increment by at most one, so only need to check if equal
13141316 if (AK_UNLIKELY (cpl -> offsets_count == cpl -> offsets_capacity )) {
13151317 // realloc
1316- cpl -> offsets_capacity *= 2 ;
1318+ cpl -> offsets_capacity <<= 1 ;
13171319 cpl -> offsets = PyMem_Realloc (cpl -> offsets ,
13181320 sizeof (Py_ssize_t ) * cpl -> offsets_capacity );
1319- if (cpl -> offsets == NULL ) return -1 ;
1321+ if (cpl -> offsets == NULL ) {
1322+ return -1 ;
1323+ }
13201324 }
13211325 return 0 ;
13221326}
@@ -1332,7 +1336,9 @@ AK_CPL_AppendField(AK_CodePointLine* cpl, PyObject* field)
13321336 Py_ssize_t element_length = PyUnicode_GET_LENGTH (field );
13331337
13341338 // if we cannot fit field length, resize
1335- if (AK_CPL_resize_buffer (cpl , element_length )) return -1 ;
1339+ if (AK_CPL_resize_buffer (cpl , element_length )) {
1340+ return -1 ;
1341+ }
13361342
13371343 // we write the field directly into the CPL buffer
13381344 if (PyUnicode_AsUCS4 (field ,
0 commit comments