Skip to content

Commit

Permalink
Factor out longest_prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-bourne committed Nov 30, 2023
1 parent a3de039 commit 37b8dad
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions packages/rust-book/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ impl Book {
}

fn build_modules(&self, module_path: &[&str]) -> Result<()> {
// TODO: Divide this up into functions
// TODO: Handle `mod.rs` modules
let path = if module_path.is_empty() {
PathBuf::from("lib")
Expand Down Expand Up @@ -136,30 +135,9 @@ impl Book {
expect_kind(SyntaxKind::L_CURLY, stmts.pop_front())?;
expect_kind(SyntaxKind::R_CURLY, stmts.pop_back())?;

// Find prefix
let body_text = stmts.iter().map(|s| s.to_string()).collect::<String>();
let mut ws_prefixes = body_text.lines().filter_map(whitespace_prefix);

let longest_prefix = if let Some(mut longest_prefix) = ws_prefixes.next() {
for prefix in ws_prefixes {
// We can use `split_at` with `find_position` as our strings
// only contain single byte chars (' ' or '\t').
longest_prefix = longest_prefix
.split_at(
longest_prefix
.chars()
.zip(prefix.chars())
.find_position(|(x, y)| x != y)
.map(|(position, _ch)| position)
.unwrap_or_else(|| min(longest_prefix.len(), prefix.len())),
)
.0;
}

longest_prefix
} else {
""
};
let ws_prefixes = body_text.lines().filter_map(whitespace_prefix);
let longest_prefix = longest_prefix(ws_prefixes);

if stmts
.front()
Expand Down Expand Up @@ -203,6 +181,29 @@ impl Book {
}
}

fn longest_prefix<'a>(mut prefixes: impl Iterator<Item = &'a str>) -> &'a str {
if let Some(mut longest_prefix) = prefixes.next() {
for prefix in prefixes {
// We can use `split_at` with `find_position` as our strings
// only contain single byte chars (' ' or '\t').
longest_prefix = longest_prefix
.split_at(
longest_prefix
.chars()
.zip(prefix.chars())
.find_position(|(x, y)| x != y)
.map(|(position, _ch)| position)
.unwrap_or_else(|| min(longest_prefix.len(), prefix.len())),
)
.0;
}

longest_prefix
} else {
""
}
}

fn write_body(
stmts: impl IntoIterator<Item = NodeOrToken<SyntaxNode, SyntaxToken>>,
mut output_file: BufWriter<File>,
Expand Down

0 comments on commit 37b8dad

Please sign in to comment.