mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Use memcpy when possible in icu_calibre_utils
This commit is contained in:
parent
a3d6204304
commit
5333986819
@ -122,6 +122,7 @@ static UChar* python_to_icu(PyObject *obj, int32_t *osz) {
|
|||||||
UChar *ans = NULL;
|
UChar *ans = NULL;
|
||||||
Py_ssize_t sz = 0;
|
Py_ssize_t sz = 0;
|
||||||
UErrorCode status = U_ZERO_ERROR;
|
UErrorCode status = U_ZERO_ERROR;
|
||||||
|
Py_UCS2 *data;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (!PyUnicode_CheckExact(obj)) {
|
if (!PyUnicode_CheckExact(obj)) {
|
||||||
@ -150,10 +151,14 @@ static UChar* python_to_icu(PyObject *obj, int32_t *osz) {
|
|||||||
break;
|
break;
|
||||||
case PyUnicode_2BYTE_KIND:
|
case PyUnicode_2BYTE_KIND:
|
||||||
ans = (UChar*) malloc((sz+1) * sizeof(UChar));
|
ans = (UChar*) malloc((sz+1) * sizeof(UChar));
|
||||||
// UChar might be more than 2 bytes, so we need to copy manually.
|
data = PyUnicode_2BYTE_DATA(obj);
|
||||||
// Hopefully this will be optimized as memcpy where possible.
|
// if UChar is more than 2 bytes, we need to copy manually.
|
||||||
for(i = 0; i < sz; i++) {
|
if(sizeof(UChar) != sizeof(Py_UCS2)) {
|
||||||
ans[i] = PyUnicode_2BYTE_DATA(obj)[i];
|
for(i = 0; i < sz; i++) {
|
||||||
|
ans[i] = data[i];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
memcpy(ans, data, sz);
|
||||||
}
|
}
|
||||||
// add null terminator
|
// add null terminator
|
||||||
ans[sz] = 0;
|
ans[sz] = 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user