Skip to content

Commit b213b51

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 f9fff0a commit b213b51

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
@@ -998,6 +998,15 @@ def __init__(self, message: str, suggestions: list[str] | None = None) -> None:
998998
self.suggestions = suggestions
999999

10001000

1001+
SPACE_CHARS = {
1002+
' ', # space
1003+
'\xa0', # no-break space
1004+
'\u202f', # narrow no-break space
1005+
}
1006+
1007+
SPACE_CHARS_RE = re.compile('|'.join(SPACE_CHARS))
1008+
1009+
10011010
def parse_number(
10021011
string: str,
10031012
locale: Locale | str | None = LC_NUMERIC,
@@ -1029,12 +1038,12 @@ def parse_number(
10291038
group_symbol = get_group_symbol(locale, numbering_system=numbering_system)
10301039

10311040
if (
1032-
re.match(r'\s', group_symbol) and # if the grouping symbol is a kind of space,
1041+
group_symbol in SPACE_CHARS and # if the grouping symbol is a kind of space,
10331042
group_symbol not in string and # and the string to be parsed does not contain it,
1034-
re.search(r'\s', string) # but it does contain any other kind of space instead,
1043+
SPACE_CHARS_RE.search(string) # but it does contain any other kind of space instead,
10351044
):
10361045
# ... it's reasonable to assume it is taking the place of the grouping symbol.
1037-
string = re.sub(r'\s', group_symbol, string)
1046+
string = SPACE_CHARS_RE.sub(group_symbol, string)
10381047

10391048
try:
10401049
return int(string.replace(group_symbol, ''))
@@ -1095,12 +1104,12 @@ def parse_decimal(
10951104
decimal_symbol = get_decimal_symbol(locale, numbering_system=numbering_system)
10961105

10971106
if not strict and (
1098-
re.match(r'\s', group_symbol) and # if the grouping symbol is a kind of space,
1107+
group_symbol in SPACE_CHARS and # if the grouping symbol is a kind of space,
10991108
group_symbol not in string and # and the string to be parsed does not contain it,
1100-
re.search(r'\s', string) # but it does contain any other kind of space instead,
1109+
SPACE_CHARS_RE.search(string) # but it does contain any other kind of space instead,
11011110
):
11021111
# ... it's reasonable to assume it is taking the place of the grouping symbol.
1103-
string = re.sub(r'\s', group_symbol, string)
1112+
string = SPACE_CHARS_RE.sub(group_symbol, string)
11041113

11051114
try:
11061115
parsed = decimal.Decimal(string.replace(group_symbol, '')

0 commit comments

Comments
 (0)