Skip to content

Commit 3707acc

Browse files
CopilotashyanSpada
andauthored
Consolidate Bolt PRs: optimize Value Display, Token comparisons, and tokenizer allocations
Agent-Logs-Url: https://github.com/ashyanSpada/expression_engine_rs/sessions/ca3024b8-72f8-4ddf-aa61-9c26416668e9 Co-authored-by: ashyanSpada <22587148+ashyanSpada@users.noreply.github.com>
1 parent f5e118f commit 3707acc

3 files changed

Lines changed: 28 additions & 35 deletions

File tree

src/token.rs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,22 @@ impl From<&str> for DelimTokenType {
5252
}
5353

5454
impl DelimTokenType {
55-
pub fn string(&self) -> String {
55+
pub fn as_str(&self) -> &'static str {
5656
use DelimTokenType::*;
5757
match self {
58-
OpenParen => "(".to_string(),
59-
CloseParen => ")".to_string(),
60-
OpenBracket => "[".to_string(),
61-
CloseBracket => "]".to_string(),
62-
OpenBrace => "{".to_string(),
63-
CloseBrace => "}".to_string(),
64-
Unknown => "??".to_string(),
58+
OpenParen => "(",
59+
CloseParen => ")",
60+
OpenBracket => "[",
61+
CloseBracket => "]",
62+
OpenBrace => "{",
63+
CloseBrace => "}",
64+
Unknown => "??",
6565
}
6666
}
67+
68+
pub fn string(&self) -> String {
69+
self.as_str().to_string()
70+
}
6771
}
6872

6973
#[derive(Clone, PartialEq, Debug, Copy)]
@@ -85,19 +89,10 @@ pub enum Token<'input> {
8589

8690
pub fn check_op(token: Token, expected: &str) -> bool {
8791
match token {
88-
Token::Delim(op, _) => {
89-
if op.string() == expected {
90-
return true;
91-
}
92-
}
93-
Token::Operator(op, _) => {
94-
if op == expected {
95-
return true;
96-
}
97-
}
98-
_ => return false,
92+
Token::Delim(op, _) => op.as_str() == expected,
93+
Token::Operator(op, _) => op == expected,
94+
_ => false,
9995
}
100-
return false;
10196
}
10297

10398
impl<'input> Token<'input> {
@@ -213,7 +208,7 @@ impl<'input> fmt::Display for Token<'input> {
213208
Function(val, span) => write!(f, "Function Token: {}, {}", val, span),
214209
String(val, span) => write!(f, "String Token: {}, {}", val, span),
215210
Semicolon(val, span) => write!(f, "Semicolon Token: {}, {}", val, span),
216-
Delim(ty, span) => write!(f, "Delim Token: {}, {}", ty.string(), span),
211+
Delim(ty, span) => write!(f, "Delim Token: {}, {}", ty.as_str(), span),
217212
EOF => write!(f, "EOF"),
218213
}
219214
}

src/tokenizer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl<'a> Tokenizer<'a> {
5858
loop {
5959
match self.peek_one() {
6060
Some((_, _ch)) => {
61-
if keyword::is_op(&(self.input[start..self.current() + 1].to_string())) {
61+
if keyword::is_op(&self.input[start..self.current() + 1]) {
6262
self.next_one();
6363
} else {
6464
break;
@@ -145,7 +145,7 @@ impl<'a> Tokenizer<'a> {
145145
self.next()?;
146146
match token {
147147
Token::Delim(bracket, _) => {
148-
if bracket.string() == op {
148+
if bracket.as_str() == op {
149149
return Ok(());
150150
}
151151
}

src/value.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,23 @@ pub enum Value {
1717
impl fmt::Display for Value {
1818
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1919
match self {
20-
Self::String(val) => write!(f, "value string: {}", val.clone()),
21-
Self::Number(val) => write!(f, "value number: {}", val.clone()),
22-
Self::Bool(val) => write!(f, "value bool: {}", val.clone()),
20+
Self::String(val) => write!(f, "value string: {}", val),
21+
Self::Number(val) => write!(f, "value number: {}", val),
22+
Self::Bool(val) => write!(f, "value bool: {}", val),
2323
Self::List(values) => {
24-
let mut s = String::from("[");
24+
write!(f, "value list: [")?;
2525
for value in values {
26-
s.push_str(format!("{},", value.clone()).as_str());
26+
write!(f, "{},", value)?;
2727
}
28-
s.push_str("]");
29-
write!(f, "value list: {}", s)
28+
write!(f, "]")
3029
}
3130
Self::Map(m) => {
32-
let mut s = String::from("{");
31+
write!(f, "value map: {{")?;
3332
for (k, v) in m {
34-
s.push_str(format!("key: {},", k.clone()).as_str());
35-
s.push_str(format!("value: {}; ", v.clone()).as_str());
33+
write!(f, "key: {},", k)?;
34+
write!(f, "value: {}; ", v)?;
3635
}
37-
s.push_str("}");
38-
write!(f, "value map: {}", s)
36+
write!(f, "}}")
3937
}
4038
Self::None => write!(f, "None"),
4139
}

0 commit comments

Comments
 (0)