Skip to content

Commit

Permalink
random syntax updates related string performance; tag 0.1.10
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Oct 29, 2021
1 parent 4bff94b commit 5f776ca
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cirru_parser"
version = "0.1.9"
version = "0.1.10"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2018"
license = "MIT"
Expand Down
10 changes: 6 additions & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ mod s_expr;
mod tree;
mod writer;

use std::cmp;

use primes::CirruLexState;
use tree::{resolve_comma, resolve_dollar};

Expand Down Expand Up @@ -286,16 +288,16 @@ pub fn resolve_indentations(initial_tokens: CirruLexItemList) -> CirruLexItemLis
acc.push(CirruLexItem::Close);
pointer += 1;
}
CirruLexItem::Indent(n) => match n {
_ if n > level => {
CirruLexItem::Indent(n) => match n.cmp(&level) {
cmp::Ordering::Greater => {
let delta = n - level;
for _ in 0..delta {
acc.push(CirruLexItem::Open);
}
pointer += 1;
level = n;
}
_ if n < level => {
cmp::Ordering::Less => {
let delta = level - n;
for _ in 0..delta {
acc.push(CirruLexItem::Close);
Expand All @@ -305,7 +307,7 @@ pub fn resolve_indentations(initial_tokens: CirruLexItemList) -> CirruLexItemLis
pointer += 1;
level = n;
}
_ => {
cmp::Ordering::Equal => {
if acc.is_empty() {
acc = vec![];
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/primes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ fn is_normal_str(tok: &str) -> bool {
/// escape_cirru_leaf("a b"); // "\"a b\""
/// ```
pub fn escape_cirru_leaf(s: &str) -> String {
let mut chunk = String::from("\"");
let mut chunk = String::with_capacity(s.len() + 1);
chunk.push('\"');
if is_normal_str(s) {
chunk.push_str(s);
} else {
Expand Down
5 changes: 3 additions & 2 deletions src/s_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ pub fn ends_with_newline(s: &str) -> bool {
}

pub fn gen_newline(n: usize) -> String {
let mut chunk: String = String::from("\n");
let mut chunk: String = String::with_capacity(2 * n + 2);
chunk.push('\n');
for _ in 0..n {
chunk = format!("{} ", chunk);
chunk.push_str(" ");
}
chunk
}
Expand Down
8 changes: 4 additions & 4 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ pub fn resolve_comma(xs: &[Cirru]) -> Vec<Cirru> {
}
}

fn comma_helper(intial_after: &[Cirru]) -> Vec<Cirru> {
let mut before: Vec<Cirru> = vec![];
let after: &[Cirru] = intial_after;
fn comma_helper(initial_after: &[Cirru]) -> Vec<Cirru> {
let mut before: Vec<Cirru> = Vec::with_capacity(initial_after.len());
let after: &[Cirru] = initial_after;

let mut pointer = 0;

Expand Down Expand Up @@ -51,7 +51,7 @@ fn comma_helper(intial_after: &[Cirru]) -> Vec<Cirru> {

pub fn resolve_dollar(xs: &[Cirru]) -> Vec<Cirru> {
if xs.is_empty() {
return vec![];
vec![]
} else {
dollar_helper(xs)
}
Expand Down
8 changes: 5 additions & 3 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ fn generate_leaf(s: &str) -> String {
if all_allowed {
s.to_string()
} else {
let mut ret = String::from("\"");
let mut ret = String::with_capacity(s.len() + 2);
ret.push('"');
ret.push_str(&str::escape_default(s).to_string());
ret.push('"');
ret
Expand Down Expand Up @@ -103,15 +104,16 @@ fn generate_inline_expr(xs: &[Cirru]) -> String {
}

fn render_spaces(n: usize) -> String {
let mut result = String::from("");
let mut result = String::with_capacity(n * 2);
for _ in 0..n {
result.push_str(" ");
}
result
}

fn render_newline(n: usize) -> String {
let mut ret = String::from("\n");
let mut ret = String::with_capacity(n * 2);
ret.push('\n');
ret.push_str(&render_spaces(n));
ret
}
Expand Down

0 comments on commit 5f776ca

Please sign in to comment.