summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth M Morton <seth.m.morton@gmail.com>2018-04-10 22:33:29 -0700
committerSeth M Morton <seth.m.morton@gmail.com>2018-04-10 22:33:29 -0700
commitd001af85e65a7071acb627a360751096824e3f77 (patch)
tree33c74ffd0da14e870eecb52be2b2118493b37b19
parentd495a090f07942f0eacbd1830abb150138f404a2 (diff)
downloadnatsort-d001af85e65a7071acb627a360751096824e3f77.tar.gz
fake_fastnumbers now supports long unicode decimal strings.
The behavior of fake_fastnumbers was not matching that of fastnumbers, which was to allow conversion of strings such as "۱۲" to 12. This has been corrected.
-rw-r--r--natsort/compat/fake_fastnumbers.py12
1 files changed, 8 insertions, 4 deletions
diff --git a/natsort/compat/fake_fastnumbers.py b/natsort/compat/fake_fastnumbers.py
index 0af60b5..6eee532 100644
--- a/natsort/compat/fake_fastnumbers.py
+++ b/natsort/compat/fake_fastnumbers.py
@@ -13,6 +13,7 @@ from __future__ import (
# Std. lib imports.
import unicodedata
+from natsort.unicode_numbers import decimal_chars
from natsort.compat.py23 import PY_VERSION
if PY_VERSION >= 3:
long = int
@@ -22,16 +23,18 @@ NAN_INF = ['INF', 'INf', 'Inf', 'inF', 'iNF', 'InF', 'inf', 'iNf',
'NAN', 'nan', 'NaN', 'nAn', 'naN', 'NAn', 'nAN', 'Nan']
NAN_INF.extend(['+'+x[:2] for x in NAN_INF] + ['-'+x[:2] for x in NAN_INF])
NAN_INF = frozenset(NAN_INF)
+ASCII_NUMS = '0123456789+-'
def fast_float(x, key=lambda x: x, nan=None,
- uni=unicodedata.numeric, nan_inf=NAN_INF):
+ uni=unicodedata.numeric, nan_inf=NAN_INF,
+ _first_char=frozenset(decimal_chars + list(ASCII_NUMS + '.'))):
"""\
Convert a string to a float quickly, return input as-is if not possible.
We don't need to accept all input that the real fast_int accepts because
the input will be controlled by the splitting algorithm.
"""
- if x[0] in '0123456789+-.' or x.lstrip()[:3] in nan_inf:
+ if x[0] in _first_char or x.lstrip()[:3] in nan_inf:
try:
x = float(x)
return nan if nan is not None and x != x else x
@@ -47,13 +50,14 @@ def fast_float(x, key=lambda x: x, nan=None,
return key(x)
-def fast_int(x, key=lambda x: x, nan=None, uni=unicodedata.digit):
+def fast_int(x, key=lambda x: x, nan=None, uni=unicodedata.digit,
+ _first_char=frozenset(decimal_chars + list(ASCII_NUMS))):
"""\
Convert a string to a int quickly, return input as-is if not possible.
We don't need to accept all input that the real fast_int accepts because
the input will be controlled by the splitting algorithm.
"""
- if x[0] in '0123456789+-':
+ if x[0] in _first_char:
try:
return long(x)
except ValueError: