Skip to content
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 10 additions & 10 deletions src/fast_automaton/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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;
Expand All @@ -1093,7 +1093,7 @@ impl<'a> RangeCursor<'a> {
RangeCursor {
bounds,
pair,
next_scalar,
next_index,
}
}

Expand All @@ -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
Expand Down
28 changes: 0 additions & 28 deletions src/fast_automaton/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32> = 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> {
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"]
Expand Down
24 changes: 13 additions & 11 deletions src/fast_automaton/spanning_set/mod.rs
Original file line number Diff line number Diff line change
@@ -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)
}
Expand Down Expand Up @@ -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();
Expand All @@ -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<Event> = 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,
});
Expand Down Expand Up @@ -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);
}
Expand All @@ -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();
Expand Down
Loading