Skip to content

Commit 7fc29f6

Browse files
committed
Be more specific about which alternative space characters should work as group symbols
The `\s` character class (or the `string.isspace()` method) could match characters like new lines that we probably don’t want to consider as potential group symbols in numbers.
1 parent 74d07a1 commit 7fc29f6

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

babel/numbers.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,15 @@ def __init__(self, message: str, suggestions: list[str] | None = None) -> None:
862862
self.suggestions = suggestions
863863

864864

865+
SPACE_CHARS = {
866+
' ', # space
867+
'\xa0', # no-break space
868+
'\u202f', # narrow no-break space
869+
}
870+
871+
SPACE_CHARS_RE = re.compile('|'.join(SPACE_CHARS))
872+
873+
865874
def parse_number(string: str, locale: Locale | str | None = LC_NUMERIC) -> int:
866875
"""Parse localized number string into an integer.
867876
@@ -885,12 +894,12 @@ def parse_number(string: str, locale: Locale | str | None = LC_NUMERIC) -> int:
885894
group_symbol = get_group_symbol(locale)
886895

887896
if (
888-
re.match(r'\s', group_symbol) and # if the grouping symbol is a kind of space,
897+
group_symbol in SPACE_CHARS and # if the grouping symbol is a kind of space,
889898
group_symbol not in string and # and the string to be parsed does not contain it,
890-
re.search(r'\s', string) # but it does contain any other kind of space instead,
899+
SPACE_CHARS_RE.search(string) # but it does contain any other kind of space instead,
891900
):
892901
# ... it's reasonable to assume it is taking the place of the grouping symbol.
893-
string = re.sub(r'\s', group_symbol, string)
902+
string = SPACE_CHARS_RE.sub(group_symbol, string)
894903

895904
try:
896905
return int(string.replace(group_symbol, ''))
@@ -940,12 +949,12 @@ def parse_decimal(string: str, locale: Locale | str | None = LC_NUMERIC, strict:
940949
decimal_symbol = get_decimal_symbol(locale)
941950

942951
if not strict and (
943-
re.match(r'\s', group_symbol) and # if the grouping symbol is a kind of space,
952+
group_symbol in SPACE_CHARS and # if the grouping symbol is a kind of space,
944953
group_symbol not in string and # and the string to be parsed does not contain it,
945-
re.search(r'\s', string) # but it does contain any other kind of space instead,
954+
SPACE_CHARS_RE.search(string) # but it does contain any other kind of space instead,
946955
):
947956
# ... it's reasonable to assume it is taking the place of the grouping symbol.
948-
string = re.sub(r'\s', group_symbol, string)
957+
string = SPACE_CHARS_RE.sub(group_symbol, string)
949958

950959
try:
951960
parsed = decimal.Decimal(string.replace(group_symbol, '')

0 commit comments

Comments
 (0)