replaced Box<str> with SmolStr

This commit is contained in:
Moritz Gmeiner 2024-09-02 05:19:30 +02:00
commit da6a820638
21 changed files with 137 additions and 85 deletions

View file

@ -1,4 +1,5 @@
use phf::phf_map;
use smol_str::SmolStr;
use super::{CodePos, LexerError, Token, TokenType};
@ -130,8 +131,9 @@ impl Lexer {
// advance until either source is empty or newline if found
while !self.source_is_empty() && self.advance() != '\n' {}
let comment: Box<str> =
self.source[self.start + 2..self.current].iter().collect();
let comment = SmolStr::from_iter(
self.source[self.start + 2..self.current].iter().cloned(),
);
self.push_token(TokenType::Comment(comment));
} else if self.consume('*') {
@ -170,9 +172,11 @@ impl Lexer {
self.advance();
}
let comment: Box<str> = self.source[self.start + 2..self.current - 2]
.iter()
.collect();
let comment = SmolStr::from_iter(
self.source[self.start + 2..self.current - 2]
.iter()
.cloned(),
);
self.push_token(TokenType::Comment(comment));
} else {
@ -289,11 +293,6 @@ impl Lexer {
}
}
// let string_literal: Box<str> = self.source[self.start + 1..self.current - 1]
// .iter()
// .collect();
// Some(TokenType::String(Box::new(string_literal)))
self.tokens.push(Token::new_string(s, self.code_pos));
}

View file

@ -1,5 +1,7 @@
use std::fmt::{Debug, Display};
use smol_str::SmolStr;
use super::CodePos;
#[repr(u8)]
@ -17,13 +19,13 @@ pub enum TokenType {
Less, LessEqual,
// Identifier and literals
Identifier(Box<str>), String(Box<str>), Number(f64),
Identifier(SmolStr), String(SmolStr), Number(f64),
// Keywords
And, Break, Class, Else, False, Fun, For, If, Nil, Or,
Print, Return, Super, This, True, Var, While,
Comment(Box<str>),
Comment(SmolStr),
#[allow(clippy::upper_case_acronyms)]
EOF
@ -42,14 +44,14 @@ impl Token {
}
}
pub fn new_string(s: impl Into<Box<str>>, code_pos: CodePos) -> Self {
pub fn new_string(s: impl Into<SmolStr>, code_pos: CodePos) -> Self {
Token {
token_type: TokenType::String(s.into()),
code_pos,
}
}
pub fn new_identifier(name: impl Into<Box<str>>, code_pos: CodePos) -> Self {
pub fn new_identifier(name: impl Into<SmolStr>, code_pos: CodePos) -> Self {
Token {
token_type: TokenType::Identifier(name.into()),
code_pos,