Skip to content

Commit 017179c

Browse files
committed
Use bytes not strings in substr matcher; fixes #100
1 parent e610ab9 commit 017179c

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

controllers/aici_abi/src/substring.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt::Display;
22

33
use crate::{
4-
bytes::limit_str,
4+
bytes::limit_bytes,
55
recognizer::{FunctionalRecognizer, StackRecognizer},
66
toktree::SpecialToken,
77
};
@@ -14,7 +14,7 @@ enum Node {
1414

1515
pub struct SubStrMatcher {
1616
end_str: String,
17-
source: String,
17+
source: Vec<u8>,
1818
nodes: Vec<Node>,
1919
}
2020

@@ -52,7 +52,7 @@ impl SubStrMatcher {
5252
serde_json::Value::Object(children_json)
5353
}
5454
Node::Leaf { source_offset } => {
55-
json!(limit_str(&self.source[*source_offset..], 20))
55+
json!(limit_bytes(&self.source[*source_offset..], 20))
5656
}
5757
}
5858
}
@@ -77,7 +77,7 @@ impl SubStrMatcher {
7777
"{:indent$}{}: {:?}",
7878
"",
7979
*source_offset,
80-
limit_str(&self.source[*source_offset..], 20),
80+
limit_bytes(&self.source[*source_offset..], 20),
8181
)?;
8282
}
8383
}
@@ -86,13 +86,13 @@ impl SubStrMatcher {
8686

8787
pub fn new(source: &str, end_str: &str) -> Self {
8888
let mut tmp = Self {
89-
source: source.to_string() + " ",
89+
source: (source.to_string() + " ").as_bytes().to_vec(),
9090
end_str: end_str.to_string(),
9191
nodes: vec![Node::Inner { children: vec![] }],
9292
};
9393
tmp.add(0);
9494
for i in 0..tmp.source.len() {
95-
if tmp.source.as_bytes()[i] == b' ' {
95+
if tmp.source[i] == b' ' {
9696
tmp.add(i + 1);
9797
}
9898
}
@@ -101,15 +101,15 @@ impl SubStrMatcher {
101101
tmp
102102
}
103103

104-
fn find(&self, s: &str) -> (usize, usize) {
104+
fn find(&self, s: &[u8]) -> (usize, usize) {
105105
let mut node_idx = 0;
106-
for (i, b) in s.bytes().enumerate() {
106+
for (i, b) in s.iter().enumerate() {
107107
let node = &self.nodes[node_idx];
108108
match node {
109109
Node::Inner { children } => {
110110
let mut found = false;
111111
for (c, idx) in children.iter() {
112-
if *c == b {
112+
if *c == *b {
113113
node_idx = *idx;
114114
found = true;
115115
break;
@@ -137,7 +137,7 @@ impl SubStrMatcher {
137137
let num_nodes = self.nodes.len();
138138
match &mut self.nodes[node_idx] {
139139
Node::Inner { children } => {
140-
children.push((s1.as_bytes()[0], num_nodes));
140+
children.push((s1[0], num_nodes));
141141
let n = add_node(
142142
&mut self.nodes,
143143
Node::Leaf {
@@ -160,8 +160,8 @@ impl SubStrMatcher {
160160
}
161161

162162
for i in 0..s1.len() {
163-
let b1 = s1.as_bytes()[i];
164-
let b2 = s2.as_bytes()[i];
163+
let b1 = s1[i];
164+
let b2 = s2[i];
165165
if b1 != b2 {
166166
let n1 = add_node(
167167
&mut self.nodes,
@@ -196,7 +196,7 @@ impl SubStrMatcher {
196196
}
197197

198198
fn append_to_src_off(&self, off: usize, byte: u8) -> SubStrState {
199-
if off < self.source.len() && self.source.as_bytes()[off] == byte {
199+
if off < self.source.len() && self.source[off] == byte {
200200
SubStrState::SourceOffset(off + 1)
201201
} else {
202202
SubStrState::Dead

0 commit comments

Comments
 (0)