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.color3 import parse_color_string, hsl_to_rgb
|
||||||
from tinycss.tests import BaseTest
|
from tinycss.tests import BaseTest
|
||||||
|
|
||||||
|
|
||||||
class TestColor3(BaseTest):
|
class TestColor3(BaseTest):
|
||||||
|
|
||||||
def test_color_parsing(self):
|
def test_color_parsing(self):
|
||||||
|
@ -203,41 +203,24 @@ tokenize_init(PyObject *self, PyObject *args) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define END_ITER_CODE_PTS }}
|
#define END_ITER_CODE_PTS }}
|
||||||
|
static PyObject *dot = NULL;
|
||||||
|
|
||||||
static PyObject *unicode_to_number(PyObject *src) {
|
static PyObject *unicode_to_number(PyObject *src) {
|
||||||
const char *utf8_data;
|
|
||||||
#if PY_MAJOR_VERSION >= 3
|
#if PY_MAJOR_VERSION >= 3
|
||||||
utf8_data = PyUnicode_AsUTF8(src);
|
PyObject* ans = PyFloat_FromString(src);
|
||||||
if (!utf8_data) return NULL;
|
|
||||||
#else
|
#else
|
||||||
PyObject *utf_8_temp = PyUnicode_AsUTF8String(src);
|
PyObject* ans = PyFloat_FromString(src, NULL);
|
||||||
if (utf_8_temp == NULL) return NULL;
|
|
||||||
utf8_data = PyString_AS_STRING(utf_8_temp);
|
|
||||||
#endif
|
#endif
|
||||||
PyObject *ans = NULL;
|
double val = PyFloat_AsDouble(ans);
|
||||||
char *end = NULL;
|
long lval = (long)val;
|
||||||
if (strchr(utf8_data, '.')) {
|
if (val - lval != 0) return ans;
|
||||||
double d = 0;
|
Py_DECREF(ans);
|
||||||
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");
|
|
||||||
#if PY_MAJOR_VERSION >= 3
|
#if PY_MAJOR_VERSION >= 3
|
||||||
else ans = PyLong_FromLong(d);
|
return PyLong_FromLong(lval);
|
||||||
#else
|
#else
|
||||||
else ans = PyInt_FromLong(d);
|
return PyInt_FromLong(lval);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
#if PY_MAJOR_VERSION < 3
|
|
||||||
Py_DECREF(utf_8_temp);
|
|
||||||
#endif
|
|
||||||
return ans;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void lowercase(PyObject *x) {
|
static void lowercase(PyObject *x) {
|
||||||
@ -516,6 +499,8 @@ CALIBRE_MODINIT_FUNC inittokenizer(void) {
|
|||||||
Py_INCREF(&tokenizer_TokenType);
|
Py_INCREF(&tokenizer_TokenType);
|
||||||
PyModule_AddObject(mod, "Token", (PyObject *) &tokenizer_TokenType);
|
PyModule_AddObject(mod, "Token", (PyObject *) &tokenizer_TokenType);
|
||||||
|
|
||||||
|
dot = PyUnicode_FromString(".");
|
||||||
|
if (dot == NULL) INITERROR;
|
||||||
|
|
||||||
#if PY_MAJOR_VERSION >= 3
|
#if PY_MAJOR_VERSION >= 3
|
||||||
return mod;
|
return mod;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user