Skip to content

Commit 4238c50

Browse files
authored
Remove itertools as a dependency (#34)
1 parent a1b21a7 commit 4238c50

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ keywords = ["sql"]
1313
categories = ["development-tools"]
1414

1515
[dependencies]
16-
itertools = "0.12"
1716
nom = "7.0.0"
1817
unicode_categories = "0.1.1"
1918

src/formatter.rs

+30-26
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
use std::borrow::Cow;
2+
13
use crate::indentation::Indentation;
24
use crate::inline_block::InlineBlock;
35
use crate::params::Params;
46
use crate::tokenizer::{Token, TokenKind};
57
use crate::{FormatOptions, QueryParams};
6-
use itertools::Itertools;
7-
use std::borrow::Cow;
88

99
pub(crate) fn format(tokens: &[Token<'_>], params: &QueryParams, options: FormatOptions) -> String {
1010
let mut formatter = Formatter::new(tokens, params, options);
@@ -223,25 +223,25 @@ impl<'a> Formatter<'a> {
223223
}
224224

225225
fn indent_comment(&self, token: &str) -> String {
226-
token
227-
.split('\n')
228-
.enumerate()
229-
.map(|(i, line)| {
230-
if i == 0 {
231-
return line.to_string();
232-
}
233-
if !line.starts_with(|c| c == ' ' || c == '\t') {
234-
return line.to_string();
235-
}
236-
format!(
237-
"{} {}",
238-
self.indentation.get_indent(),
239-
line.chars()
240-
.skip_while(|&c| c == ' ' || c == '\t')
241-
.collect::<String>()
242-
)
243-
})
244-
.join("\n")
226+
let mut combined = String::with_capacity(token.len() + 4);
227+
for (i, line) in token.split('\n').enumerate() {
228+
if i == 0 {
229+
combined.push_str(line)
230+
} else if line.starts_with([' ', '\t']) {
231+
let indent = self.indentation.get_indent();
232+
let start_trimmed = line.trim_start_matches([' ', '\t']);
233+
combined.reserve(indent.len() + start_trimmed.len() + 2);
234+
combined.push('\n');
235+
combined.push_str(&indent);
236+
combined.push(' ');
237+
combined.push_str(start_trimmed);
238+
} else {
239+
combined.reserve(line.len() + 1);
240+
combined.push('\n');
241+
combined.push_str(line);
242+
}
243+
}
244+
combined
245245
}
246246

247247
fn format_reserved_word<'t>(&self, token: &'t str) -> Cow<'t, str> {
@@ -252,12 +252,16 @@ impl<'a> Formatter<'a> {
252252
}
253253
}
254254

255-
// Replace any sequence of whitespace characters with single space
255+
/// Replace any sequence of whitespace characters with single space
256256
fn equalize_whitespace(&self, token: &str) -> String {
257-
token
258-
.split(char::is_whitespace)
259-
.filter(|s| !s.is_empty())
260-
.join(" ")
257+
let mut combined = String::with_capacity(token.len());
258+
for s in token.split(char::is_whitespace).filter(|s| !s.is_empty()) {
259+
if !combined.is_empty() {
260+
combined.push(' ');
261+
}
262+
combined.push_str(s);
263+
}
264+
combined
261265
}
262266

263267
fn previous_token(&self) -> Option<&Token<'_>> {

0 commit comments

Comments
 (0)