mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 10:44:09 -04:00
Tiny performance improvement
This commit is contained in:
parent
0930fef90a
commit
e05c4e669b
@ -713,7 +713,7 @@ icu_character_name(PyObject *self, PyObject *args) {
|
|||||||
int32_t sz, alias = 0;
|
int32_t sz, alias = 0;
|
||||||
UChar *buf;
|
UChar *buf;
|
||||||
UErrorCode status = U_ZERO_ERROR;
|
UErrorCode status = U_ZERO_ERROR;
|
||||||
PyObject *palias = NULL;
|
PyObject *palias = NULL, *result = NULL;
|
||||||
UChar32 code = 0;
|
UChar32 code = 0;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "es|O", "UTF-8", &input, &palias)) return NULL;
|
if (!PyArg_ParseTuple(args, "es|O", "UTF-8", &input, &palias)) return NULL;
|
||||||
@ -732,20 +732,21 @@ icu_character_name(PyObject *self, PyObject *args) {
|
|||||||
sz = u_charName(code, U_UNICODE_CHAR_NAME, name, 511, &status);
|
sz = u_charName(code, U_UNICODE_CHAR_NAME, name, 511, &status);
|
||||||
}
|
}
|
||||||
if (U_FAILURE(status)) { PyErr_SetString(PyExc_ValueError, "Failed to get name for code"); goto end; }
|
if (U_FAILURE(status)) { PyErr_SetString(PyExc_ValueError, "Failed to get name for code"); goto end; }
|
||||||
|
result = PyUnicode_DecodeUTF8(name, sz, "strict");
|
||||||
end:
|
end:
|
||||||
if (buf != NULL) free(buf);
|
if (buf != NULL) free(buf);
|
||||||
PyMem_Free(input);
|
PyMem_Free(input);
|
||||||
|
|
||||||
return (PyErr_Occurred()) ? NULL : Py_BuildValue("s#", name, sz);
|
return result;
|
||||||
} // }}}
|
} // }}}
|
||||||
|
|
||||||
// character_name {{{
|
// character_name_from_code {{{
|
||||||
static PyObject *
|
static PyObject *
|
||||||
icu_character_name_from_code(PyObject *self, PyObject *args) {
|
icu_character_name_from_code(PyObject *self, PyObject *args) {
|
||||||
char name[512] = {0};
|
char name[512] = {0};
|
||||||
int32_t sz, alias = 0;
|
int32_t sz, alias = 0;
|
||||||
UErrorCode status = U_ZERO_ERROR;
|
UErrorCode status = U_ZERO_ERROR;
|
||||||
PyObject *palias = NULL;
|
PyObject *palias = NULL, *result = NULL;
|
||||||
UChar32 code = 0;
|
UChar32 code = 0;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "I|O", &code, &palias)) return NULL;
|
if (!PyArg_ParseTuple(args, "I|O", &code, &palias)) return NULL;
|
||||||
@ -758,8 +759,9 @@ icu_character_name_from_code(PyObject *self, PyObject *args) {
|
|||||||
sz = u_charName(code, U_UNICODE_CHAR_NAME, name, 511, &status);
|
sz = u_charName(code, U_UNICODE_CHAR_NAME, name, 511, &status);
|
||||||
}
|
}
|
||||||
if (U_FAILURE(status)) { PyErr_SetString(PyExc_ValueError, "Failed to get name for code"); goto end; }
|
if (U_FAILURE(status)) { PyErr_SetString(PyExc_ValueError, "Failed to get name for code"); goto end; }
|
||||||
|
result = PyUnicode_DecodeUTF8(name, sz, "strict");
|
||||||
end:
|
end:
|
||||||
return (PyErr_Occurred()) ? NULL : Py_BuildValue("s#", name, sz);
|
return result;
|
||||||
} // }}}
|
} // }}}
|
||||||
|
|
||||||
// chr {{{
|
// chr {{{
|
||||||
@ -770,6 +772,7 @@ icu_chr(PyObject *self, PyObject *args) {
|
|||||||
UChar buf[5] = {0};
|
UChar buf[5] = {0};
|
||||||
int32_t sz = 0;
|
int32_t sz = 0;
|
||||||
char utf8[21];
|
char utf8[21];
|
||||||
|
PyObject *result = NULL;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "I", &code)) return NULL;
|
if (!PyArg_ParseTuple(args, "I", &code)) return NULL;
|
||||||
|
|
||||||
@ -777,8 +780,9 @@ icu_chr(PyObject *self, PyObject *args) {
|
|||||||
if (U_FAILURE(status)) { PyErr_SetString(PyExc_ValueError, "arg not in range(0x110000)"); goto end; }
|
if (U_FAILURE(status)) { PyErr_SetString(PyExc_ValueError, "arg not in range(0x110000)"); goto end; }
|
||||||
u_strToUTF8(utf8, 20, &sz, buf, sz, &status);
|
u_strToUTF8(utf8, 20, &sz, buf, sz, &status);
|
||||||
if (U_FAILURE(status)) { PyErr_SetString(PyExc_ValueError, "arg not in range(0x110000)"); goto end; }
|
if (U_FAILURE(status)) { PyErr_SetString(PyExc_ValueError, "arg not in range(0x110000)"); goto end; }
|
||||||
|
result = PyUnicode_DecodeUTF8(utf8, sz, "strict");
|
||||||
end:
|
end:
|
||||||
return (PyErr_Occurred()) ? NULL : Py_BuildValue("s#", utf8, sz);
|
return result;
|
||||||
} // }}}
|
} // }}}
|
||||||
|
|
||||||
// normalize {{{
|
// normalize {{{
|
||||||
|
@ -133,7 +133,7 @@ def py_find(pattern, source):
|
|||||||
def character_name(string):
|
def character_name(string):
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
return _icu.character_name(unicode(string)).decode('utf-8') or None
|
return _icu.character_name(unicode(string)) or None
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
import unicodedata
|
import unicodedata
|
||||||
return unicodedata.name(unicode(string)[0], None)
|
return unicodedata.name(unicode(string)[0], None)
|
||||||
@ -143,7 +143,7 @@ def character_name(string):
|
|||||||
def character_name_from_code(code):
|
def character_name_from_code(code):
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
return _icu.character_name_from_code(code).decode('utf-8') or ''
|
return _icu.character_name_from_code(code) or ''
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
import unicodedata
|
import unicodedata
|
||||||
return unicodedata.name(py_safe_chr(code), '')
|
return unicodedata.name(py_safe_chr(code), '')
|
||||||
@ -164,7 +164,7 @@ else:
|
|||||||
|
|
||||||
def safe_chr(code):
|
def safe_chr(code):
|
||||||
try:
|
try:
|
||||||
return _icu.chr(code).decode('utf-8')
|
return _icu.chr(code)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return py_safe_chr(code)
|
return py_safe_chr(code)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user