Skip to content

Commit 9d43c8b

Browse files
committed
trivial string new change; tag 0.1.29
1 parent f0d25e5 commit 9d43c8b

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cirru_parser"
3-
version = "0.1.28"
3+
version = "0.1.29"
44
authors = ["jiyinyiyong <[email protected]>"]
55
edition = "2021"
66
license = "MIT"

src/parser.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -114,33 +114,33 @@ pub fn lex(initial_code: &str) -> Result<CirruLexItemList, String> {
114114
// guessed an initial length
115115
let mut acc: CirruLexItemList = Vec::with_capacity(initial_code.len() >> 4);
116116
let mut state = CirruLexState::Indent;
117-
let mut buffer = String::with_capacity(8); // guessed
117+
let mut buffer = String::new();
118118
let code = initial_code;
119119

120120
for (idx, c) in code.chars().enumerate() {
121121
match state {
122122
CirruLexState::Space => match c {
123123
' ' => {
124124
state = CirruLexState::Space;
125-
buffer = String::from("");
125+
buffer = String::new();
126126
}
127127
'\n' => {
128128
state = CirruLexState::Indent;
129-
buffer = String::from("");
129+
buffer = String::new();
130130
}
131131
'(' => {
132132
acc.push(CirruLexItem::Open);
133133
state = CirruLexState::Space;
134-
buffer = String::from("")
134+
buffer = String::new()
135135
}
136136
')' => {
137137
acc.push(CirruLexItem::Close);
138138
state = CirruLexState::Space;
139-
buffer = String::from("")
139+
buffer = String::new()
140140
}
141141
'"' => {
142142
state = CirruLexState::Str;
143-
buffer = String::from("");
143+
buffer = String::new();
144144
}
145145
_ => {
146146
state = CirruLexState::Token;
@@ -151,29 +151,29 @@ pub fn lex(initial_code: &str) -> Result<CirruLexItemList, String> {
151151
' ' => {
152152
acc.push(CirruLexItem::Str(buffer));
153153
state = CirruLexState::Space;
154-
buffer = String::from("");
154+
buffer = String::new();
155155
}
156156
'"' => {
157157
acc.push(CirruLexItem::Str(buffer));
158158
state = CirruLexState::Str;
159-
buffer = String::from("");
159+
buffer = String::new();
160160
}
161161
'\n' => {
162162
acc.push(CirruLexItem::Str(buffer));
163163
state = CirruLexState::Indent;
164-
buffer = String::from("");
164+
buffer = String::new();
165165
}
166166
'(' => {
167167
acc.push(CirruLexItem::Str(buffer));
168168
acc.push(CirruLexItem::Open);
169169
state = CirruLexState::Space;
170-
buffer = String::from("")
170+
buffer = String::new()
171171
}
172172
')' => {
173173
acc.push(CirruLexItem::Str(buffer));
174174
acc.push(CirruLexItem::Close);
175175
state = CirruLexState::Space;
176-
buffer = String::from("")
176+
buffer = String::new()
177177
}
178178
_ => {
179179
state = CirruLexState::Token;
@@ -184,7 +184,7 @@ pub fn lex(initial_code: &str) -> Result<CirruLexItemList, String> {
184184
'"' => {
185185
acc.push(CirruLexItem::Str(buffer));
186186
state = CirruLexState::Space;
187-
buffer = String::from("");
187+
buffer = String::new();
188188
}
189189
'\\' => {
190190
state = CirruLexState::Escape;
@@ -239,20 +239,20 @@ pub fn lex(initial_code: &str) -> Result<CirruLexItemList, String> {
239239
}
240240
'\n' => {
241241
state = CirruLexState::Indent;
242-
buffer = String::from("");
242+
buffer = String::new();
243243
}
244244
'"' => {
245245
let level = parse_indentation(buffer.len() as u8)?;
246246
acc.push(level);
247247
state = CirruLexState::Str;
248-
buffer = String::from("");
248+
buffer = String::new();
249249
}
250250
'(' => {
251251
let level = parse_indentation(buffer.len() as u8)?;
252252
acc.push(level);
253253
acc.push(CirruLexItem::Open);
254254
state = CirruLexState::Space;
255-
buffer = String::from("");
255+
buffer = String::new();
256256
}
257257
')' => return Err(String::from("unexpected ) at line start")),
258258
_ => {
@@ -279,11 +279,12 @@ pub fn lex(initial_code: &str) -> Result<CirruLexItemList, String> {
279279

280280
/// internal function for figuring out indentations after lexing
281281
pub fn resolve_indentations(tokens: &[CirruLexItem]) -> CirruLexItemList {
282-
let mut acc: CirruLexItemList = Vec::with_capacity(tokens.len() >> 1);
282+
let size = tokens.len();
283+
let mut acc: CirruLexItemList = Vec::new();
283284
let mut level = 0;
284285
let mut pointer = 0;
285286
loop {
286-
if pointer >= tokens.len() {
287+
if pointer >= size {
287288
if acc.is_empty() {
288289
return vec![];
289290
}

0 commit comments

Comments
 (0)