@@ -1292,31 +1292,35 @@ AK_CPL_Free(AK_CodePointLine* cpl)
1292
1292
1293
1293
// Returns 0 on success, -1 on failure.
1294
1294
static 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
+ }
1301
1301
cpl -> buffer = PyMem_Realloc (cpl -> buffer ,
1302
1302
sizeof (Py_UCS4 ) * cpl -> buffer_capacity );
1303
- if (cpl -> buffer == NULL ) return -1 ;
1304
-
1303
+ if (cpl -> buffer == NULL ) {
1304
+ return -1 ;
1305
+ }
1305
1306
cpl -> buffer_current_ptr = cpl -> buffer + cpl -> buffer_count ;
1306
1307
}
1307
1308
return 0 ;
1308
1309
}
1309
1310
1311
+
1312
+ // NOTE: we only add one offset at time, so this does not need to take an increment argument.
1310
1313
static inline int
1311
- AK_CPL_resize_offsets (AK_CodePointLine * cpl )
1312
- {
1314
+ AK_CPL_resize_offsets (AK_CodePointLine * cpl ) {
1313
1315
// increment by at most one, so only need to check if equal
1314
1316
if (AK_UNLIKELY (cpl -> offsets_count == cpl -> offsets_capacity )) {
1315
1317
// realloc
1316
- cpl -> offsets_capacity *= 2 ;
1318
+ cpl -> offsets_capacity <<= 1 ;
1317
1319
cpl -> offsets = PyMem_Realloc (cpl -> offsets ,
1318
1320
sizeof (Py_ssize_t ) * cpl -> offsets_capacity );
1319
- if (cpl -> offsets == NULL ) return -1 ;
1321
+ if (cpl -> offsets == NULL ) {
1322
+ return -1 ;
1323
+ }
1320
1324
}
1321
1325
return 0 ;
1322
1326
}
@@ -1332,7 +1336,9 @@ AK_CPL_AppendField(AK_CodePointLine* cpl, PyObject* field)
1332
1336
Py_ssize_t element_length = PyUnicode_GET_LENGTH (field );
1333
1337
1334
1338
// 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
+ }
1336
1342
1337
1343
// we write the field directly into the CPL buffer
1338
1344
if (PyUnicode_AsUCS4 (field ,
0 commit comments