mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Greatly simplify unicode_to_number
This also has the virtue of making number parsing locale independent, as is required for parsing CSS literals
This commit is contained in:
parent
3de9c83787
commit
d324e776b5
@ -9,6 +9,7 @@ __copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||
from tinycss.color3 import parse_color_string, hsl_to_rgb
|
||||
from tinycss.tests import BaseTest
|
||||
|
||||
|
||||
class TestColor3(BaseTest):
|
||||
|
||||
def test_color_parsing(self):
|
||||
|
@ -203,40 +203,23 @@ tokenize_init(PyObject *self, PyObject *args) {
|
||||
#endif
|
||||
|
||||
#define END_ITER_CODE_PTS }}
|
||||
static PyObject *dot = NULL;
|
||||
|
||||
static PyObject *unicode_to_number(PyObject *src) {
|
||||
const char *utf8_data;
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
utf8_data = PyUnicode_AsUTF8(src);
|
||||
if (!utf8_data) return NULL;
|
||||
PyObject* ans = PyFloat_FromString(src);
|
||||
#else
|
||||
PyObject *utf_8_temp = PyUnicode_AsUTF8String(src);
|
||||
if (utf_8_temp == NULL) return NULL;
|
||||
utf8_data = PyString_AS_STRING(utf_8_temp);
|
||||
PyObject* ans = PyFloat_FromString(src, NULL);
|
||||
#endif
|
||||
PyObject *ans = NULL;
|
||||
char *end = NULL;
|
||||
if (strchr(utf8_data, '.')) {
|
||||
double d = 0;
|
||||
errno = 0;
|
||||
d = strtod(utf8_data, &end);
|
||||
if (errno != 0 || (end == utf8_data)) PyErr_SetString(PyExc_ValueError, "Not a valid float");
|
||||
else ans = PyFloat_FromDouble(d);
|
||||
} else {
|
||||
long d = 0;
|
||||
errno = 0;
|
||||
d = strtol(utf8_data, &end, 10);
|
||||
if (errno != 0 || (end == utf8_data)) PyErr_SetString(PyExc_ValueError, "Not a valid integer");
|
||||
double val = PyFloat_AsDouble(ans);
|
||||
long lval = (long)val;
|
||||
if (val - lval != 0) return ans;
|
||||
Py_DECREF(ans);
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
else ans = PyLong_FromLong(d);
|
||||
return PyLong_FromLong(lval);
|
||||
#else
|
||||
else ans = PyInt_FromLong(d);
|
||||
return PyInt_FromLong(lval);
|
||||
#endif
|
||||
}
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
Py_DECREF(utf_8_temp);
|
||||
#endif
|
||||
return ans;
|
||||
}
|
||||
|
||||
|
||||
@ -516,6 +499,8 @@ CALIBRE_MODINIT_FUNC inittokenizer(void) {
|
||||
Py_INCREF(&tokenizer_TokenType);
|
||||
PyModule_AddObject(mod, "Token", (PyObject *) &tokenizer_TokenType);
|
||||
|
||||
dot = PyUnicode_FromString(".");
|
||||
if (dot == NULL) INITERROR;
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
return mod;
|
||||
|
Loading…
x
Reference in New Issue
Block a user