Skip to content

Commit 628a505

Browse files
committed
strlen fixes for unicode
1 parent daf0611 commit 628a505

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

quaddtype/numpy_quaddtype/src/casts.cpp

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,8 @@ quad_to_string_adaptive(Sleef_quad *sleef_val, npy_intp unicode_size_chars)
342342
return NULL;
343343
}
344344

345-
npy_intp pos_len = strlen(pos_str);
345+
// no need to scan full, only checking if its longer
346+
npy_intp pos_len = strnlen(pos_str, unicode_size_chars + 1);
346347

347348
// If positional format fits, use it; otherwise use scientific notation
348349
if (pos_len <= unicode_size_chars) {
@@ -357,22 +358,6 @@ quad_to_string_adaptive(Sleef_quad *sleef_val, npy_intp unicode_size_chars)
357358
}
358359
}
359360

360-
// Helper function: Copy string to UCS4 output buffer
361-
static inline void
362-
copy_string_to_ucs4(const char *str, Py_UCS4 *out_ucs4, npy_intp unicode_size_chars)
363-
{
364-
npy_intp str_len = strlen(str);
365-
366-
for (npy_intp i = 0; i < unicode_size_chars; i++) {
367-
if (i < str_len) {
368-
out_ucs4[i] = (Py_UCS4)str[i];
369-
}
370-
else {
371-
out_ucs4[i] = 0;
372-
}
373-
}
374-
}
375-
376361
template <bool Aligned>
377362
static int
378363
quad_to_unicode_loop(PyArrayMethod_Context *context, char *const data[],
@@ -411,7 +396,13 @@ quad_to_unicode_loop(PyArrayMethod_Context *context, char *const data[],
411396

412397
// Convert char string to UCS4 and store in output
413398
Py_UCS4 *out_ucs4 = (Py_UCS4 *)out_ptr;
414-
copy_string_to_ucs4(temp_str, out_ucs4, unicode_size_chars);
399+
npy_intp str_len = strnlen(temp_str, unicode_size_chars);
400+
for (npy_intp i = 0; i < str_len; i++) {
401+
out_ucs4[i] = (Py_UCS4)temp_str[i];
402+
}
403+
for (npy_intp i = str_len; i < unicode_size_chars; i++) {
404+
out_ucs4[i] = 0;
405+
}
415406

416407
Py_DECREF(py_str);
417408

0 commit comments

Comments
 (0)