Skip to content

Commit e1704e2

Browse files
committed
refactor(regex): remove symbol map lower bound
1 parent 0e0e7c6 commit e1704e2

8 files changed

Lines changed: 20 additions & 26 deletions

File tree

bytes/internal/regex_engine/compile.mbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn compile(profile~ : Profile, ast : Pattern) -> Regex {
2727
}
2828
let symbol_map = @symbol_map.new()
2929
symbolize(profile~, symbol_map~, ast)
30-
let (symbol_table, symbol_repr) = symbol_map.finalize(profile.lb, profile.ub)
30+
let (symbol_table, symbol_repr) = symbol_map.finalize(profile.ub)
3131
let ctx = @automata.Context::new()
3232
let tc = TranslateContext::new(ctx, symbol_table)
3333
let (expr, pref) = translate(tc, ast)

bytes/internal/regex_engine/symbol_map/dense_table.mbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
// limitations under the License.
1414

1515
///|
16-
priv struct DenseTable(@shared_types.Rechar, FixedArray[@shared_types.Rechar])
16+
priv struct DenseTable(FixedArray[@shared_types.Rechar])
1717

1818
///|
1919
impl Table for DenseTable with map(self, c) {
20-
self.1.unsafe_get(c - self.0)
20+
self.0.unsafe_get(c)
2121
}

bytes/internal/regex_engine/symbol_map/pkg.generated.mbti

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub fn new() -> SymbolMap
1212

1313
// Types and methods
1414
type SymbolMap
15-
pub fn SymbolMap::finalize(Self, Int, Int) -> (&Table, ReadOnlyArray[Int])
15+
pub fn SymbolMap::finalize(Self, Int) -> (&Table, ReadOnlyArray[Int])
1616
pub fn SymbolMap::split(Self, @rechar_set.RecharSet) -> Unit
1717

1818
// Type aliases

bytes/internal/regex_engine/symbol_map/symbol_map.mbt

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,45 +40,42 @@ pub fn SymbolMap::split(
4040
/// Function `finalize`.
4141
pub fn SymbolMap::finalize(
4242
self : SymbolMap,
43-
lb : @shared_types.Rechar,
4443
ub : @shared_types.Rechar,
4544
) -> (&Table, ReadOnlyArray[@shared_types.Rechar]) {
46-
if ub - lb < 1024 {
47-
let (table, repr) = self.finalize_dense(lb, ub)
45+
if ub < 1024 {
46+
let (table, repr) = self.finalize_dense(ub)
4847
(table as &Table, repr)
4948
} else {
50-
let (table, repr) = self.finalize_sparse(lb, ub)
49+
let (table, repr) = self.finalize_sparse(ub)
5150
(table as &Table, repr)
5251
}
5352
}
5453

5554
///|
5655
fn SymbolMap::finalize_dense(
5756
self : SymbolMap,
58-
lb : @shared_types.Rechar,
5957
ub : @shared_types.Rechar,
6058
) -> (DenseTable, ReadOnlyArray[@shared_types.Rechar]) {
6159
let repr = []
62-
let table = FixedArray::make(ub - lb + 1, 0)
63-
self.each_intervals(lb, ub, (lo, hi) => {
60+
let table = FixedArray::make(ub + 1, 0)
61+
self.each_intervals(0, ub, (lo, hi) => {
6462
let symbol = repr.length()
6563
repr.push(lo)
6664
for c in lo..<=hi {
67-
table[c - lb] = symbol
65+
table[c] = symbol
6866
}
6967
})
70-
(DenseTable(lb, table), ReadOnlyArray::from_array(repr))
68+
(DenseTable(table), ReadOnlyArray::from_array(repr))
7169
}
7270

7371
///|
7472
fn SymbolMap::finalize_sparse(
7573
self : SymbolMap,
76-
lb : @shared_types.Rechar,
7774
ub : @shared_types.Rechar,
7875
) -> (SparseTable, ReadOnlyArray[@shared_types.Rechar]) {
7976
let repr = []
8077
let entries = []
81-
self.each_intervals(lb, ub, (lo, hi) => {
78+
self.each_intervals(0, ub, (lo, hi) => {
8279
let symbol = repr.length()
8380
repr.push(lo)
8481
entries.push({ lo, hi, symbol })

bytes/internal/regex_engine/symbol_map/symbol_map_test.mbt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
// limitations under the License.
1414

1515
///|
16-
test "dense table respects non-zero lower bound" {
16+
test "dense table uses absolute indexes" {
1717
let symbol_map = @symbol_map.new()
1818
symbol_map.split(@shared_types.RecharSet::char(11))
19-
let (table, repr) = symbol_map.finalize(10, 12)
19+
let (table, repr) = symbol_map.finalize(12)
2020
assert_eq(repr.length(), 3)
21-
assert_eq(repr[0], 10)
21+
assert_eq(repr[0], 0)
2222
assert_eq(repr[1], 11)
2323
assert_eq(repr[2], 12)
2424
assert_eq(table.map(10), 0)

string/internal/regex_engine/compile.mbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn compile(profile~ : Profile, ast : Pattern) -> Regex {
2727
}
2828
let symbol_map = @symbol_map.new()
2929
symbolize(profile~, symbol_map~, ast)
30-
let (symbol_table, symbol_repr) = symbol_map.finalize(profile.lb, profile.ub)
30+
let (symbol_table, symbol_repr) = symbol_map.finalize(profile.ub)
3131
let ctx = @automata.Context::new()
3232
let tc = TranslateContext::new(ctx, symbol_table)
3333
let (expr, pref) = translate(tc, ast)

string/internal/regex_engine/symbol_map/pkg.generated.mbti

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub fn new() -> SymbolMap
1212

1313
// Types and methods
1414
type SymbolMap
15-
pub fn SymbolMap::finalize(Self, Int, Int) -> (Table, ReadOnlyArray[Int])
15+
pub fn SymbolMap::finalize(Self, Int) -> (Table, ReadOnlyArray[Int])
1616
pub fn SymbolMap::split(Self, @rechar_set.RecharSet) -> Unit
1717

1818
type Table

string/internal/regex_engine/symbol_map/symbol_map.mbt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,29 @@ pub fn SymbolMap::split(
4040
/// Function `finalize`.
4141
pub fn SymbolMap::finalize(
4242
self : SymbolMap,
43-
lb : @shared_types.Rechar,
4443
ub : @shared_types.Rechar,
4544
) -> (Table, ReadOnlyArray[@shared_types.Rechar]) {
46-
// Most cases lb is 0
47-
guard lb >= 0 else { panic() }
4845
let repr = []
4946
let table = Array::make(128, -1)
5047
if ub < 128 {
5148
self.each_intervals(0, ub, (lo, hi) => {
5249
let symbol = repr.length()
53-
repr.push(Int::max(lo, lb))
50+
repr.push(lo)
5451
for c in lo..<=hi {
5552
table[c] = symbol
5653
}
5754
})
5855
} else {
5956
self.each_intervals(0, 127, (lo, hi) => {
6057
let symbol = repr.length()
61-
repr.push(Int::max(lo, lb))
58+
repr.push(lo)
6259
for c in lo..<=hi {
6360
table[c] = symbol
6461
}
6562
})
6663
self.each_intervals(128, ub, (lo, hi) => {
6764
let symbol = repr.length()
68-
repr.push(Int::max(lo, lb))
65+
repr.push(lo)
6966
// (lo, hi, symbol)
7067
table.push(lo)
7168
table.push(hi)

0 commit comments

Comments
 (0)