Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/sre_engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ harness = false

[dependencies]
rustpython-wtf8 = { workspace = true }
unic-ucd-category = { workspace = true }
num_enum = { workspace = true }
bitflags = { workspace = true }
optional = { workspace = true }
Expand Down
15 changes: 13 additions & 2 deletions crates/sre_engine/src/string.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use rustpython_wtf8::Wtf8;
use unic_ucd_category::GeneralCategory;

#[derive(Debug, Clone, Copy)]
pub struct StringCursor {
Expand Down Expand Up @@ -441,9 +442,19 @@ pub(crate) const fn is_uni_linebreak(ch: u32) -> bool {
}
#[inline]
pub(crate) fn is_uni_alnum(ch: u32) -> bool {
// TODO: check with cpython
char::try_from(ch)
.map(|x| x.is_alphanumeric())
.map(|c| match GeneralCategory::of(c) {
GeneralCategory::UppercaseLetter
| GeneralCategory::LowercaseLetter
| GeneralCategory::TitlecaseLetter
| GeneralCategory::ModifierLetter
| GeneralCategory::OtherLetter
| GeneralCategory::DecimalNumber
| GeneralCategory::LetterNumber
| GeneralCategory::OtherNumber => true,
GeneralCategory::Unassigned => c.is_alphanumeric(),
_ => false,
})
.unwrap_or(false)
}
#[inline]
Expand Down
25 changes: 23 additions & 2 deletions crates/vm/src/builtins/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,19 @@ impl PyStr {

#[pymethod]
fn isalnum(&self) -> bool {
!self.data.is_empty() && self.char_all(char::is_alphanumeric)
!self.data.is_empty()
&& self.char_all(|c| match GeneralCategory::of(c) {
GeneralCategory::UppercaseLetter
| GeneralCategory::LowercaseLetter
| GeneralCategory::TitlecaseLetter
| GeneralCategory::ModifierLetter
| GeneralCategory::OtherLetter
| GeneralCategory::DecimalNumber
| GeneralCategory::LetterNumber
| GeneralCategory::OtherNumber => true,
GeneralCategory::Unassigned => c.is_alphanumeric(),
_ => false,
})
}

#[pymethod]
Expand Down Expand Up @@ -1056,7 +1068,16 @@ impl PyStr {

#[pymethod]
fn isalpha(&self) -> bool {
!self.data.is_empty() && self.char_all(char::is_alphabetic)
!self.data.is_empty()
&& self.char_all(|c| match GeneralCategory::of(c) {
GeneralCategory::UppercaseLetter
| GeneralCategory::LowercaseLetter
| GeneralCategory::TitlecaseLetter
| GeneralCategory::ModifierLetter
| GeneralCategory::OtherLetter => true,
GeneralCategory::Unassigned => c.is_alphabetic(),
_ => false,
})
}

#[pymethod]
Expand Down
10 changes: 10 additions & 0 deletions extra_tests/snippets/builtin_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,3 +839,13 @@ class MyString(str):
assert id(b) != id(b * 1)
assert id(b) != id(1 * b)
assert id(b) != id(b * 2)


# Regression tests for isalpha/isalnum Unicode General Category correctness.
# These characters are in letter categories (Ll/Lo) and should return True,
# but were missed in older Unicode tables used by unic-ucd-category.
# See: https://github.com/RustPython/RustPython/pull/7520#issuecomment-4148322294
for _cp in [1376, 1416, 1519, 2160, 2161, 2162, 2163, 2164, 2165, 2166]:
_c = chr(_cp)
assert _c.isalpha(), f"U+{_cp:04X} should be isalpha"
assert _c.isalnum(), f"U+{_cp:04X} should be isalnum"
8 changes: 8 additions & 0 deletions extra_tests/snippets/builtin_str_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

assert c == "👋👋👋"

import re
import unicodedata

assert unicodedata.category("a") == "Ll"
Expand Down Expand Up @@ -38,3 +39,10 @@
# TODO: add east_asian_width and mirrored
# assert unicodedata.ucd_3_2_0.east_asian_width('\u231a') == 'N'
# assert not unicodedata.ucd_3_2_0.mirrored("\u0f3a")

# U+0345 COMBINING GREEK YPOGEGRAMMENI (category Mn) should not be alphanumeric.
# CPython's isalpha/isalnum use Unicode letter categories (Lu/Ll/Lt/Lm/Lo),
# not the broader Unicode Alphabetic derived property.
assert not "\u0345".isalpha(), "isalpha should not match Mn category characters"
assert not "\u0345".isalnum(), "isalnum should not match Mn category characters"
assert not re.match(r"\w", "\u0345"), r"\w should not match U+0345 (category Mn)"
Loading