Skip to content

Commit f79d6df

Browse files
fix: Ignore combining characters in SRE
Closes: #7518
1 parent 647aa8e commit f79d6df

6 files changed

Lines changed: 14 additions & 11 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/sre_engine/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ rustpython-wtf8 = { workspace = true }
1919
num_enum = { workspace = true }
2020
bitflags = { workspace = true }
2121
optional = { workspace = true }
22+
icu_properties = { workspace = true }
2223

2324
[dev-dependencies]
2425
criterion = { workspace = true }

‎crates/sre_engine/src/string.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use icu_properties::props::{CanonicalCombiningClass, EnumeratedProperty};
12
use rustpython_wtf8::Wtf8;
23

34
#[derive(Debug, Clone, Copy)]
@@ -443,7 +444,10 @@ pub(crate) const fn is_uni_linebreak(ch: u32) -> bool {
443444
pub(crate) fn is_uni_alnum(ch: u32) -> bool {
444445
// TODO: check with cpython
445446
char::try_from(ch)
446-
.map(|x| x.is_alphanumeric())
447+
.map(|x| {
448+
x.is_alphanumeric()
449+
&& CanonicalCombiningClass::for_char(x) == CanonicalCombiningClass::NotReordered
450+
})
447451
.unwrap_or(false)
448452
}
449453
#[inline]

‎crates/vm/src/builtins/str.rs‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,9 @@ use rustpython_common::{
4444
wtf8::{CodePoint, Wtf8, Wtf8Buf, Wtf8Chunk, Wtf8Concat},
4545
};
4646

47-
use icu_properties::{
48-
CodePointMapData,
49-
props::{
50-
BidiClass, BinaryProperty, CanonicalCombiningClass, EnumeratedProperty, GeneralCategory,
51-
XidContinue, XidStart,
52-
},
47+
use icu_properties::props::{
48+
BidiClass, BinaryProperty, CanonicalCombiningClass, EnumeratedProperty, GeneralCategory,
49+
XidContinue, XidStart,
5350
};
5451
use unicode_casing::CharExt;
5552

@@ -950,10 +947,10 @@ impl PyStr {
950947

951948
#[pymethod]
952949
fn isalnum(&self) -> bool {
953-
let map = CodePointMapData::<CanonicalCombiningClass>::new();
954950
!self.data.is_empty()
955951
&& self.char_all(|c| {
956-
c.is_alphanumeric() && map.get(c) == CanonicalCombiningClass::NotReordered
952+
c.is_alphanumeric()
953+
&& CanonicalCombiningClass::for_char(c) == CanonicalCombiningClass::NotReordered
957954
})
958955
}
959956

‎extra_tests/snippets/builtin_str.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
assert not "\u006e\u0303".isalnum()
8080
assert "\u00f1".isalnum()
8181
assert not "\u0345".isalnum()
82-
for raw in range(0x0363, 0x036f):
82+
for raw in range(0x0363, 0x036F):
8383
assert not chr(raw).isalnum()
8484

8585
s = "1 2 3"

‎extra_tests/snippets/stdlib_re.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,4 @@
8181
assert re.fullmatch(r"([0-9]++(?:\.[0-9]+)*+)", "1.25.38").group(0) == "1.25.38"
8282

8383
# Combining characters; issue #7518
84-
# assert not re.match(r"\w", "\u0345"), r"\w should not match U+0345 (category Mn)"
84+
assert not re.match(r"\w", "\u0345"), r"\w should not match U+0345 (category Mn)"

0 commit comments

Comments
 (0)