Skip to content

Commit 5f21a7c

Browse files
⚡ Bolt: Zero-allocation string evaluation for DelimTokenType and expected matching
Refactored `DelimTokenType` and `Tokenizer` logic to eliminate redundant `String` allocations during token identification and checking. - Introduced `as_str()` returning `&'static str` for `DelimTokenType` variants. - Refactored `check_op` and `fmt::Display` to use `as_str()` instead of `string()`. - Refactored `Tokenizer::expect` to use `as_str()` without changing its token consumption semantics. - Added tests to preserve `check_op` and `Tokenizer::expect` coverage. Co-authored-by: ashyanSpada <22587148+ashyanSpada@users.noreply.github.com>
1 parent 7c210fe commit 5f21a7c

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

src/token.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ impl From<&str> for DelimTokenType {
5353

5454
impl DelimTokenType {
5555
// ⚡ Bolt Optimization: Use `&'static str` to prevent heap allocation during string comparison.
56-
#[cfg(not(tarpaulin_include))]
5756
pub fn as_str(&self) -> &'static str {
5857
use DelimTokenType::*;
5958
match self {
@@ -67,7 +66,6 @@ impl DelimTokenType {
6766
}
6867
}
6968

70-
#[cfg(not(tarpaulin_include))]
7169
pub fn string(&self) -> String {
7270
self.as_str().to_string()
7371
}
@@ -281,6 +279,20 @@ mod tests {
281279
assert_eq!(input.is_open_bracket(), output)
282280
}
283281

282+
#[test]
283+
fn test_delim_token_type_as_str() {
284+
assert_eq!(DelimTokenType::OpenParen.as_str(), "(");
285+
assert_eq!(DelimTokenType::CloseParen.as_str(), ")");
286+
assert_eq!(DelimTokenType::OpenBracket.as_str(), "[");
287+
assert_eq!(DelimTokenType::CloseBracket.as_str(), "]");
288+
assert_eq!(DelimTokenType::OpenBrace.as_str(), "{");
289+
assert_eq!(DelimTokenType::CloseBrace.as_str(), "}");
290+
assert_eq!(DelimTokenType::Unknown.as_str(), "??");
291+
292+
assert_eq!(DelimTokenType::OpenParen.string(), "(");
293+
assert_eq!(DelimTokenType::Unknown.string(), "??");
294+
}
295+
284296
#[test]
285297
fn test_check_op() {
286298
use super::check_op;

0 commit comments

Comments
 (0)