From 821e65b687ca47be566810c3be22631bb67c3b89 Mon Sep 17 00:00:00 2001 From: Alexandre van Beurden <1949482+alexvbrdn@users.noreply.github.com> Date: Thu, 19 Nov 2026 19:49:27 +0100 Subject: [PATCH] Use regex-charclass 1.3's Char::index and Char::from_index regex-charclass 1.3.0 exposes the char <-> contiguous index mapping that SURROGATES, scalar and from_scalar duplicated, so drop the local copy. Requires regex-charclass 1.3.0 on crates.io; Cargo.lock still pins 1.2.0 and must be refreshed (cargo update -p regex-charclass) once it is published. --- CHANGELOG.md | 3 +++ Cargo.lock | 4 ++-- Cargo.toml | 2 +- src/fast_automaton/generate.rs | 20 +++++++++--------- src/fast_automaton/mod.rs | 28 -------------------------- src/fast_automaton/spanning_set/mod.rs | 24 ++++++++++++---------- 6 files changed, 29 insertions(+), 52 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 924bb09..fc8924f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), ### Added - `Term::to_pattern_with`, the pattern of `Term::to_regex_with`. +### Changed +- Updated `regex-charclass` to 1.3, which makes `Char::index` and `Char::from_index` public: the mapping of a `char` to its position among all `char` values, the surrogate block skipped. They replace the crate's own copy of that mapping, and since `regex_charclass` is re-exported they are usable through it. + ## [1.2.0] - 2026-11-14 ### Added diff --git a/Cargo.lock b/Cargo.lock index ab84e16..7377437 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3208,9 +3208,9 @@ dependencies = [ [[package]] name = "regex-charclass" -version = "1.2.0" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "115f966197e7327844076cfeab1959ddd43c16fd3d98cab101efd940ec595280" +checksum = "a19c4eaa4769e5f515ed426be99acda01d229a213bebe35cf334c8849937aa78" dependencies = [ "irange", ] diff --git a/Cargo.toml b/Cargo.toml index 134b724..72893ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,7 @@ include = [ tracing = "0.1" regex-syntax = "0.8.11" # The feature flag (ucd-*) has to match the Unicode version `regex-syntax` uses. -regex-charclass = { version = "1.2.0", features = ["ucd-16"] } +regex-charclass = { version = "1.3.0", features = ["ucd-16"] } bit-set = "0.11.1" indexmap = "2.13.0" # `neural-synthesis`: learned DFA -> regex models, run with candle. diff --git a/src/fast_automaton/generate.rs b/src/fast_automaton/generate.rs index 5c242eb..ea9101c 100644 --- a/src/fast_automaton/generate.rs +++ b/src/fast_automaton/generate.rs @@ -1067,8 +1067,8 @@ struct RangeCursor<'a> { bounds: &'a [Char], /// Index of the current pair's low bound; past `bounds` once exhausted. pair: usize, - /// [`scalar`] of the next character to hand out. - next_scalar: u32, + /// [`Char::index`] of the next character to hand out. + next_index: u32, } impl<'a> RangeCursor<'a> { @@ -1077,13 +1077,13 @@ impl<'a> RangeCursor<'a> { fn new(range: &'a CharRange, mut ordinal: u32) -> Self { let bounds = range.0.as_slice(); let mut pair = 0; - let mut next_scalar = 0; + let mut next_index = 0; while pair < bounds.len() { - let (low, high) = (scalar(bounds[pair]), scalar(bounds[pair + 1])); + let (low, high) = (bounds[pair].index(), bounds[pair + 1].index()); let length = high - low + 1; if ordinal < length { - next_scalar = low + ordinal; + next_index = low + ordinal; break; } ordinal -= length; @@ -1093,7 +1093,7 @@ impl<'a> RangeCursor<'a> { RangeCursor { bounds, pair, - next_scalar, + next_index, } } @@ -1102,15 +1102,15 @@ impl<'a> RangeCursor<'a> { return None; } - let ch = from_scalar(self.next_scalar); - if self.next_scalar == scalar(self.bounds[self.pair + 1]) { + let ch = Char::from_index(self.next_index); + if self.next_index == self.bounds[self.pair + 1].index() { // Past the current interval: on to the next one. self.pair += 2; if self.pair < self.bounds.len() { - self.next_scalar = scalar(self.bounds[self.pair]); + self.next_index = self.bounds[self.pair].index(); } } else { - self.next_scalar += 1; + self.next_index += 1; } ch diff --git a/src/fast_automaton/mod.rs b/src/fast_automaton/mod.rs index 6290da3..0596e63 100644 --- a/src/fast_automaton/mod.rs +++ b/src/fast_automaton/mod.rs @@ -29,34 +29,6 @@ pub mod spanning_set; pub use convert::to_regex::{RegexSynthesizer, StateElimination}; pub use generate::{CharacterOrder, GenerationOptions, PathOrder}; -/// The block of code points `char` cannot hold: [`regex_charclass::char::Char`] -/// values skip it, so scalar values have to be shifted down past it to be -/// counted. -const SURROGATES: std::ops::Range = 0xD800..0xE000; - -/// The index of `ch` among all the characters, the surrogate block excluded. -/// Consecutive scalars are consecutive `Char`s, so `+ 1` arithmetic in scalar -/// space cannot land inside the surrogate hole. -#[inline] -fn scalar(ch: regex_charclass::char::Char) -> u32 { - let code = ch.to_u32(); - if code >= SURROGATES.end { - code - (SURROGATES.end - SURROGATES.start) - } else { - code - } -} - -/// The inverse of [`scalar`]. -#[inline] -fn from_scalar(index: u32) -> Option { - regex_charclass::char::Char::from_u32(if index >= SURROGATES.start { - index + (SURROGATES.end - SURROGATES.start) - } else { - index - }) -} - /// Represents a finite-state automaton. #[derive(Clone, Debug, PartialEq, Eq)] #[must_use = "non-`_mut` operations return a new automaton"] diff --git a/src/fast_automaton/spanning_set/mod.rs b/src/fast_automaton/spanning_set/mod.rs index 2580177..5f4a839 100644 --- a/src/fast_automaton/spanning_set/mod.rs +++ b/src/fast_automaton/spanning_set/mod.rs @@ -1,20 +1,20 @@ use std::slice::Iter; use ahash::AHashMap; +use regex_charclass::char::Char; use regex_charclass::irange::RangeSet; -use super::{from_scalar, scalar}; use crate::CharRange; -/// Converts merged, ascending scalar segments (see [`scalar`](super::scalar)) -/// back into a [`CharRange`]. The segments are disjoint and non-adjacent, so +/// Converts merged, ascending index segments (see [`Char::index`]) back +/// into a [`CharRange`]. The segments are disjoint and non-adjacent, so /// the flat bound list is already the canonical representation the set /// operations produce. fn segments_to_range(segments: &[(u32, u32)]) -> CharRange { let mut bounds = Vec::with_capacity(segments.len() * 2); for &(start, end) in segments { - bounds.push(from_scalar(start).expect("segment bounds are valid scalars")); - bounds.push(from_scalar(end).expect("segment bounds are valid scalars")); + bounds.push(Char::from_index(start).expect("segment bounds are valid indices")); + bounds.push(Char::from_index(end).expect("segment bounds are valid indices")); } RangeSet(bounds) } @@ -103,7 +103,7 @@ impl SpanningSet { /// /// The bases are the *atoms* of the inputs: the classes of characters /// covered by exactly the same subset of input ranges. They are computed - /// with one endpoint sweep over the surrogate-free scalar space, + /// with one endpoint sweep over the surrogate-free index space, /// O(E log E) in the total number of input intervals. pub fn compute_spanning_set(ranges: &[CharRange]) -> Self { let mut inputs: Vec<&CharRange> = ranges.iter().filter(|r| !r.is_empty()).collect(); @@ -118,18 +118,18 @@ impl SpanningSet { _ => {} } - // Interval [lo, hi] becomes the half-open [scalar(lo), scalar(hi)+1). + // Interval [lo, hi] becomes the half-open [lo.index(), hi.index()+1). let interval_count: usize = inputs.iter().map(|input| input.0.len() / 2).sum(); let mut events: Vec = Vec::with_capacity(interval_count * 2); for (input_index, input) in inputs.iter().enumerate() { for pair in input.0.as_chunks::<2>().0 { events.push(Event { - position: scalar(pair[0]), + position: pair[0].index(), input_index: input_index as u32, is_start: true, }); events.push(Event { - position: scalar(pair[1]) + 1, + position: pair[1].index() + 1, input_index: input_index as u32, is_start: false, }); @@ -314,7 +314,7 @@ mod tests { for (a, b) in pairs { let (low, high) = if a <= b { (a, b) } else { (b, a) }; let interval = CharRange::new_from_range( - from_scalar(low).unwrap()..=from_scalar(high).unwrap(), + Char::from_index(low).unwrap()..=Char::from_index(high).unwrap(), ); range = range.union(&interval); } @@ -340,7 +340,9 @@ mod tests { #[test] fn sweep_wide_matches_the_reference() { let single = |low: u32, high: u32| { - CharRange::new_from_range(from_scalar(low).unwrap()..=from_scalar(high).unwrap()) + CharRange::new_from_range( + Char::from_index(low).unwrap()..=Char::from_index(high).unwrap(), + ) }; let mut inputs = Vec::new();