Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Simplify unicode regex call sites
Agent-Logs-Url: https://github.com/RustPython/RustPython/sessions/b894057a-9bed-4f35-8400-a5731c63602d

Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
  • Loading branch information
Copilot and youknowone authored Apr 6, 2026
commit 4efa5da5f68851b7b5c28fc53355590266d51db9
1 change: 0 additions & 1 deletion Cargo.lock

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

12 changes: 0 additions & 12 deletions crates/literal/src/char.rs

This file was deleted.

6 changes: 4 additions & 2 deletions crates/literal/src/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl UnicodeEscape<'_> {
'\\' | '\t' | '\r' | '\n' => 2,
ch if ch < ' ' || ch as u32 == 0x7f => 4, // \xHH
ch if ch.is_ascii() => 1,
ch if crate::char::is_printable(ch) => {
ch if rustpython_unicode::classify::is_repr_printable(ch as u32) => {
// max = std::cmp::max(ch, max);
ch.len_utf8()
}
Expand Down Expand Up @@ -238,7 +238,9 @@ impl UnicodeEscape<'_> {
ch if ch.is_ascii() => {
write!(formatter, "\\x{:02x}", ch as u8)
}
ch if crate::char::is_printable(ch) => formatter.write_char(ch),
ch if rustpython_unicode::classify::is_repr_printable(ch as u32) => {
formatter.write_char(ch)
}
'\0'..='\u{ff}' => {
write!(formatter, "\\x{:02x}", ch as u32)
}
Expand Down
1 change: 0 additions & 1 deletion crates/literal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

extern crate alloc;

pub mod char;
pub mod complex;
pub mod escape;
pub mod float;
Expand Down
108 changes: 60 additions & 48 deletions crates/sre_engine/src/engine.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
// good luck to those that follow; here be dragons

use crate::string::{
is_digit, is_linebreak, is_loc_word, is_space, is_uni_digit, is_uni_linebreak, is_uni_space,
is_uni_word, is_word, lower_ascii, lower_locate, lower_unicode, upper_locate, upper_unicode,
};

use super::{MAXREPEAT, SreAtCode, SreCatCode, SreInfo, SreOpcode, StrDrive, StringCursor};
use alloc::{vec, vec::Vec};
use core::{convert::TryFrom, ptr::null};
use optional::Optioned;
use rustpython_unicode::regex as unicode_regex;

#[derive(Debug, Clone, Copy)]
pub struct Request<'a, S> {
Expand Down Expand Up @@ -659,10 +655,10 @@ fn _match<S: StrDrive>(req: &Request<'_, S>, state: &mut State, mut ctx: MatchCo
}
SreOpcode::IN => general_op_in!(charset),
SreOpcode::IN_IGNORE => {
general_op_in!(|set, c| charset(set, lower_ascii(c)))
general_op_in!(|set, c| charset(set, unicode_regex::lower_ascii(c)))
}
SreOpcode::IN_UNI_IGNORE => {
general_op_in!(|set, c| charset(set, lower_unicode(c)))
general_op_in!(|set, c| charset(set, unicode_regex::lower_unicode(c)))
}
SreOpcode::IN_LOC_IGNORE => general_op_in!(charset_loc_ignore),
SreOpcode::MARK => {
Expand Down Expand Up @@ -803,25 +799,31 @@ fn _match<S: StrDrive>(req: &Request<'_, S>, state: &mut State, mut ctx: MatchCo
SreOpcode::LITERAL => general_op_literal!(|code, c| code == c),
SreOpcode::NOT_LITERAL => general_op_literal!(|code, c| code != c),
SreOpcode::LITERAL_IGNORE => {
general_op_literal!(|code, c| code == lower_ascii(c))
general_op_literal!(|code, c| code == unicode_regex::lower_ascii(c))
}
SreOpcode::NOT_LITERAL_IGNORE => {
general_op_literal!(|code, c| code != lower_ascii(c))
general_op_literal!(|code, c| code != unicode_regex::lower_ascii(c))
}
SreOpcode::LITERAL_UNI_IGNORE => {
general_op_literal!(|code, c| code == lower_unicode(c))
general_op_literal!(|code, c| code == unicode_regex::lower_unicode(c))
}
SreOpcode::NOT_LITERAL_UNI_IGNORE => {
general_op_literal!(|code, c| code != lower_unicode(c))
general_op_literal!(|code, c| code != unicode_regex::lower_unicode(c))
}
SreOpcode::LITERAL_LOC_IGNORE => general_op_literal!(char_loc_ignore),
SreOpcode::NOT_LITERAL_LOC_IGNORE => {
general_op_literal!(|code, c| !char_loc_ignore(code, c))
}
SreOpcode::GROUPREF => general_op_groupref!(|x| x),
SreOpcode::GROUPREF_IGNORE => general_op_groupref!(lower_ascii),
SreOpcode::GROUPREF_LOC_IGNORE => general_op_groupref!(lower_locate),
SreOpcode::GROUPREF_UNI_IGNORE => general_op_groupref!(lower_unicode),
SreOpcode::GROUPREF_IGNORE => {
general_op_groupref!(unicode_regex::lower_ascii)
}
SreOpcode::GROUPREF_LOC_IGNORE => {
general_op_groupref!(unicode_regex::lower_locale)
}
SreOpcode::GROUPREF_UNI_IGNORE => {
general_op_groupref!(unicode_regex::lower_unicode)
}
SreOpcode::GROUPREF_EXISTS => {
let (group_start, group_end) =
state.marks.get(ctx.peek_code(req, 1) as usize);
Expand Down Expand Up @@ -1125,7 +1127,7 @@ impl MatchContext {
}

fn at_linebreak<S: StrDrive>(&self, req: &Request<'_, S>) -> bool {
!self.at_end(req) && is_linebreak(self.peek_char::<S>())
!self.at_end(req) && unicode_regex::is_linebreak(self.peek_char::<S>())
}

fn at_boundary<S: StrDrive, F: FnMut(u32) -> bool>(
Expand Down Expand Up @@ -1192,54 +1194,56 @@ impl MatchContext {
fn at<S: StrDrive>(req: &Request<'_, S>, ctx: &MatchContext, at_code: SreAtCode) -> bool {
match at_code {
SreAtCode::BEGINNING | SreAtCode::BEGINNING_STRING => ctx.at_beginning(),
SreAtCode::BEGINNING_LINE => ctx.at_beginning() || is_linebreak(ctx.back_peek_char::<S>()),
SreAtCode::BOUNDARY => ctx.at_boundary(req, is_word),
SreAtCode::NON_BOUNDARY => ctx.at_non_boundary(req, is_word),
SreAtCode::BEGINNING_LINE => {
ctx.at_beginning() || unicode_regex::is_linebreak(ctx.back_peek_char::<S>())
}
SreAtCode::BOUNDARY => ctx.at_boundary(req, unicode_regex::is_word),
SreAtCode::NON_BOUNDARY => ctx.at_non_boundary(req, unicode_regex::is_word),
SreAtCode::END => {
(ctx.remaining_chars(req) == 1 && ctx.at_linebreak(req)) || ctx.at_end(req)
}
SreAtCode::END_LINE => ctx.at_linebreak(req) || ctx.at_end(req),
SreAtCode::END_STRING => ctx.at_end(req),
SreAtCode::LOC_BOUNDARY => ctx.at_boundary(req, is_loc_word),
SreAtCode::LOC_NON_BOUNDARY => ctx.at_non_boundary(req, is_loc_word),
SreAtCode::UNI_BOUNDARY => ctx.at_boundary(req, is_uni_word),
SreAtCode::UNI_NON_BOUNDARY => ctx.at_non_boundary(req, is_uni_word),
SreAtCode::LOC_BOUNDARY => ctx.at_boundary(req, unicode_regex::is_locale_word),
SreAtCode::LOC_NON_BOUNDARY => ctx.at_non_boundary(req, unicode_regex::is_locale_word),
SreAtCode::UNI_BOUNDARY => ctx.at_boundary(req, unicode_regex::is_unicode_word),
SreAtCode::UNI_NON_BOUNDARY => ctx.at_non_boundary(req, unicode_regex::is_unicode_word),
}
}

fn char_loc_ignore(code: u32, c: u32) -> bool {
code == c || code == lower_locate(c) || code == upper_locate(c)
code == c || code == unicode_regex::lower_locale(c) || code == unicode_regex::upper_locale(c)
}

fn charset_loc_ignore(set: &[u32], c: u32) -> bool {
let lo = lower_locate(c);
let lo = unicode_regex::lower_locale(c);
if charset(set, c) {
return true;
}
let up = upper_locate(c);
let up = unicode_regex::upper_locale(c);
up != lo && charset(set, up)
}

fn category(cat_code: SreCatCode, c: u32) -> bool {
match cat_code {
SreCatCode::DIGIT => is_digit(c),
SreCatCode::NOT_DIGIT => !is_digit(c),
SreCatCode::SPACE => is_space(c),
SreCatCode::NOT_SPACE => !is_space(c),
SreCatCode::WORD => is_word(c),
SreCatCode::NOT_WORD => !is_word(c),
SreCatCode::LINEBREAK => is_linebreak(c),
SreCatCode::NOT_LINEBREAK => !is_linebreak(c),
SreCatCode::LOC_WORD => is_loc_word(c),
SreCatCode::LOC_NOT_WORD => !is_loc_word(c),
SreCatCode::UNI_DIGIT => is_uni_digit(c),
SreCatCode::UNI_NOT_DIGIT => !is_uni_digit(c),
SreCatCode::UNI_SPACE => is_uni_space(c),
SreCatCode::UNI_NOT_SPACE => !is_uni_space(c),
SreCatCode::UNI_WORD => is_uni_word(c),
SreCatCode::UNI_NOT_WORD => !is_uni_word(c),
SreCatCode::UNI_LINEBREAK => is_uni_linebreak(c),
SreCatCode::UNI_NOT_LINEBREAK => !is_uni_linebreak(c),
SreCatCode::DIGIT => unicode_regex::is_digit(c),
SreCatCode::NOT_DIGIT => !unicode_regex::is_digit(c),
SreCatCode::SPACE => unicode_regex::is_space(c),
SreCatCode::NOT_SPACE => !unicode_regex::is_space(c),
SreCatCode::WORD => unicode_regex::is_word(c),
SreCatCode::NOT_WORD => !unicode_regex::is_word(c),
SreCatCode::LINEBREAK => unicode_regex::is_linebreak(c),
SreCatCode::NOT_LINEBREAK => !unicode_regex::is_linebreak(c),
SreCatCode::LOC_WORD => unicode_regex::is_locale_word(c),
SreCatCode::LOC_NOT_WORD => !unicode_regex::is_locale_word(c),
SreCatCode::UNI_DIGIT => unicode_regex::is_unicode_digit(c),
SreCatCode::UNI_NOT_DIGIT => !unicode_regex::is_unicode_digit(c),
SreCatCode::UNI_SPACE => unicode_regex::is_unicode_space(c),
SreCatCode::UNI_NOT_SPACE => !unicode_regex::is_unicode_space(c),
SreCatCode::UNI_WORD => unicode_regex::is_unicode_word(c),
SreCatCode::UNI_NOT_WORD => !unicode_regex::is_unicode_word(c),
SreCatCode::UNI_LINEBREAK => unicode_regex::is_unicode_linebreak(c),
SreCatCode::UNI_NOT_LINEBREAK => !unicode_regex::is_unicode_linebreak(c),
}
}

Expand Down Expand Up @@ -1320,7 +1324,7 @@ fn charset(set: &[u32], ch: u32) -> bool {
if set[i + 1] <= ch && ch <= set[i + 2] {
return ok;
}
let ch = upper_unicode(ch);
let ch = unicode_regex::upper_unicode(ch);
if set[i + 1] <= ch && ch <= set[i + 2] {
return ok;
}
Expand Down Expand Up @@ -1368,10 +1372,14 @@ fn _count<S: StrDrive>(
general_count_literal(req, ctx, end, |code, c| code != c);
}
SreOpcode::LITERAL_IGNORE => {
general_count_literal(req, ctx, end, |code, c| code == lower_ascii(c));
general_count_literal(req, ctx, end, |code, c| {
code == unicode_regex::lower_ascii(c)
});
}
SreOpcode::NOT_LITERAL_IGNORE => {
general_count_literal(req, ctx, end, |code, c| code != lower_ascii(c));
general_count_literal(req, ctx, end, |code, c| {
code != unicode_regex::lower_ascii(c)
});
}
SreOpcode::LITERAL_LOC_IGNORE => {
general_count_literal(req, ctx, end, char_loc_ignore);
Expand All @@ -1380,10 +1388,14 @@ fn _count<S: StrDrive>(
general_count_literal(req, ctx, end, |code, c| !char_loc_ignore(code, c));
}
SreOpcode::LITERAL_UNI_IGNORE => {
general_count_literal(req, ctx, end, |code, c| code == lower_unicode(c));
general_count_literal(req, ctx, end, |code, c| {
code == unicode_regex::lower_unicode(c)
});
}
SreOpcode::NOT_LITERAL_UNI_IGNORE => {
general_count_literal(req, ctx, end, |code, c| code != lower_unicode(c));
general_count_literal(req, ctx, end, |code, c| {
code != unicode_regex::lower_unicode(c)
});
}
_ => {
/* General case */
Expand Down
59 changes: 0 additions & 59 deletions crates/sre_engine/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,62 +331,3 @@ const fn utf8_is_cont_byte(byte: u8) -> bool {

/// Mask of the value bits of a continuation byte.
const CONT_MASK: u8 = 0b0011_1111;

#[inline]
pub(crate) fn is_word(ch: u32) -> bool {
rustpython_unicode::regex::is_word(ch)
}
#[inline]
pub(crate) fn is_space(ch: u32) -> bool {
rustpython_unicode::regex::is_space(ch)
}
#[inline]
pub(crate) fn is_digit(ch: u32) -> bool {
rustpython_unicode::regex::is_digit(ch)
}
#[inline]
pub(crate) fn is_loc_word(ch: u32) -> bool {
rustpython_unicode::regex::is_locale_word(ch)
}
#[inline]
pub(crate) const fn is_linebreak(ch: u32) -> bool {
rustpython_unicode::regex::is_linebreak(ch)
}
#[inline]
pub fn lower_ascii(ch: u32) -> u32 {
rustpython_unicode::regex::lower_ascii(ch)
}
#[inline]
pub(crate) fn lower_locate(ch: u32) -> u32 {
// FIXME: Ignore the locales
rustpython_unicode::regex::lower_locale(ch)
}
#[inline]
pub(crate) fn upper_locate(ch: u32) -> u32 {
// FIXME: Ignore the locales
rustpython_unicode::regex::upper_locale(ch)
}
#[inline]
pub(crate) fn is_uni_digit(ch: u32) -> bool {
rustpython_unicode::regex::is_unicode_digit(ch)
}
#[inline]
pub(crate) fn is_uni_space(ch: u32) -> bool {
rustpython_unicode::regex::is_unicode_space(ch)
}
#[inline]
pub(crate) const fn is_uni_linebreak(ch: u32) -> bool {
rustpython_unicode::regex::is_unicode_linebreak(ch)
}
#[inline]
pub(crate) fn is_uni_word(ch: u32) -> bool {
rustpython_unicode::regex::is_unicode_word(ch)
}
#[inline]
pub fn lower_unicode(ch: u32) -> u32 {
rustpython_unicode::regex::lower_unicode(ch)
}
#[inline]
pub fn upper_unicode(ch: u32) -> u32 {
rustpython_unicode::regex::upper_unicode(ch)
}
1 change: 0 additions & 1 deletion crates/vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ strum_macros = { workspace = true }
thiserror = { workspace = true }
memchr = { workspace = true }

caseless = "0.2.2"
flamer = { version = "0.5", optional = true }
half = "2"
psm = "0.1"
Expand Down
12 changes: 5 additions & 7 deletions crates/vm/src/stdlib/_sre.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ mod _sre {
use crossbeam_utils::atomic::AtomicCell;
use itertools::Itertools;
use num_traits::ToPrimitive;
use rustpython_sre_engine::{
Request, SearchIter, SreFlag, State, StrDrive,
string::{lower_ascii, lower_unicode, upper_unicode},
};
use rustpython_sre_engine::{Request, SearchIter, SreFlag, State, StrDrive};
use rustpython_unicode::regex as unicode_regex;

#[pyattr]
pub use rustpython_sre_engine::{CODESIZE, MAXGROUPS, MAXREPEAT, SRE_MAGIC as MAGIC};
Expand All @@ -42,17 +40,17 @@ mod _sre {
#[pyfunction]
fn unicode_iscased(ch: i32) -> bool {
let ch = ch as u32;
ch != lower_unicode(ch) || ch != upper_unicode(ch)
ch != unicode_regex::lower_unicode(ch) || ch != unicode_regex::upper_unicode(ch)
}

#[pyfunction]
fn ascii_tolower(ch: i32) -> i32 {
lower_ascii(ch as u32) as i32
unicode_regex::lower_ascii(ch as u32) as i32
}

#[pyfunction]
fn unicode_tolower(ch: i32) -> i32 {
lower_unicode(ch as u32) as i32
unicode_regex::lower_unicode(ch as u32) as i32
}

trait SreStr: StrDrive {
Expand Down
Loading