From 6b003b64316f6d65a6b2089207fe348e4ea1875a Mon Sep 17 00:00:00 2001 From: Paolo Barbolini Date: Sun, 12 Sep 2021 19:14:25 +0200 Subject: [PATCH 1/3] Reduce allocations --- src/formatter.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/formatter.rs b/src/formatter.rs index 0f58255..9422941 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -223,6 +223,8 @@ impl<'a> Formatter<'a> { } fn indent_comment(&self, token: &str) -> String { + let indent = self.indentation.get_indent(); + token .split('\n') .enumerate() @@ -235,10 +237,8 @@ impl<'a> Formatter<'a> { } format!( "{} {}", - self.indentation.get_indent(), - line.chars() - .skip_while(|&c| c == ' ' || c == '\t') - .collect::() + &indent, + line.trim_start_matches(|c| c == ' ' || c == '\t'), ) }) .join("\n") From 1f9dccddb4c5ebf79c54c482e0fe88228d93e5cf Mon Sep 17 00:00:00 2001 From: Paolo Barbolini Date: Sun, 12 Sep 2021 19:45:54 +0200 Subject: [PATCH 2/3] Upgrade to 2021 edition --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index a1ae21b..77eb522 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sqlformat" version = "0.1.8" authors = ["Josh Holmer "] -edition = "2018" +edition = "2021" license = "MIT OR Apache-2.0" homepage = "https://github.com/shssoichiro/sqlformat-rs" repository = "https://github.com/shssoichiro/sqlformat-rs" From a725c3aee8a589e1a7219cb7c474dd2eb888bea3 Mon Sep 17 00:00:00 2001 From: Paolo Barbolini Date: Sun, 12 Sep 2021 19:46:23 +0200 Subject: [PATCH 3/3] Remove itertools --- Cargo.toml | 1 - src/formatter.rs | 18 ++++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 77eb522..d7bcf99 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,6 @@ keywords = ["sql"] categories = ["development-tools"] [dependencies] -itertools = "0.10" nom = "7.0.0" unicode_categories = "0.1.1" diff --git a/src/formatter.rs b/src/formatter.rs index 9422941..b28e026 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -3,7 +3,6 @@ use crate::inline_block::InlineBlock; use crate::params::Params; use crate::tokenizer::{Token, TokenKind}; use crate::{FormatOptions, QueryParams}; -use itertools::Itertools; use std::borrow::Cow; pub(crate) fn format(tokens: &[Token<'_>], params: &QueryParams, options: FormatOptions) -> String { @@ -230,18 +229,20 @@ impl<'a> Formatter<'a> { .enumerate() .map(|(i, line)| { if i == 0 { - return line.to_string(); + return [line, "", ""]; } if !line.starts_with(|c| c == ' ' || c == '\t') { - return line.to_string(); + return [line, "", ""]; } - format!( - "{} {}", + [ &indent, + " ", line.trim_start_matches(|c| c == ' ' || c == '\t'), - ) + ] }) - .join("\n") + .intersperse(["\n", "", ""]) + .flatten() + .collect() } fn format_reserved_word<'t>(&self, token: &'t str) -> Cow<'t, str> { @@ -257,7 +258,8 @@ impl<'a> Formatter<'a> { token .split(char::is_whitespace) .filter(|s| !s.is_empty()) - .join(" ") + .intersperse(" ") + .collect() } fn previous_token(&self) -> Option<&Token<'_>> {