Skip to content

Commit

Permalink
Revert "fn replace_string_in_place"
Browse files Browse the repository at this point in the history
This reverts commit 234f9d2.
  • Loading branch information
Joshix-1 committed Jun 2, 2024
1 parent eea2388 commit e8e49c6
Showing 1 changed file with 13 additions and 48 deletions.
61 changes: 13 additions & 48 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use std::io;
use std::io::BufRead;
use std::io::BufReader;
use std::iter::Iterator;
use std::ops::Range;
use std::path::{Path, PathBuf};
use std::time::Instant;
use unicode_segmentation::UnicodeSegmentation;
Expand Down Expand Up @@ -58,26 +57,8 @@ impl WordsData {
fn read_lines(&self) -> impl Iterator<Item = String> {
read_lines_of_file(Path::new(self.path.as_str()))
.expect("Reading file should not fail.")
.filter_map(|mut word: String| {
if word.is_empty() {
return None;
}
replace_string_in_place(
&mut word,
#[inline]
|ch| {
ch.is_uppercase().then(
#[inline]
|| ch.to_lowercase(),
)
},
#[inline]
|string, range, repl| {
string.replace_range(range, repl.to_string().as_str())
},
);
Some(word)
})
.filter(|word| !word.is_empty())
.map(|word| word.to_lowercase())
.map(self.conv)
}

Expand Down Expand Up @@ -175,38 +156,22 @@ fn str_contains_umlaut(string: String) -> bool {
#[allow(clippy::ptr_arg)]
#[allow(clippy::needless_pass_by_value)]
fn replace_umlauts(mut string: String) -> String {
replace_string_in_place(
&mut string,
#[inline]
|ch| {
while let Some((idx, ch, repl_index)) =
string.char_indices().find_map(|(idx, ch)| {
UMLAUTS
.iter()
.position(|u| u == &ch)
.and_then(|repl_idx| ASCII_UMLAUT_REPLACEMENTS.get(repl_idx))
.copied()
},
#[inline]
|s, range, repl| s.replace_range(range, repl),
);
string
}

#[inline]
fn replace_string_in_place<S, RR, RC>(
string: &mut String,
replace_char: RC,
replace_range: RR,
) where
RC: Fn(char) -> Option<S>,
RR: Fn(&mut String, Range<usize>, S),
{
while let Some((idx, ch, repl)) = string
.char_indices()
.rev()
.find_map(|(idx, ch)| replace_char(ch).map(|repl| (idx, ch, repl)))
.map(|repl_idx| (idx, ch, repl_idx))
})
{
replace_range(string, idx..idx + ch.len_utf8(), repl);
string.replace_range(
idx..idx + ch.len_utf8(),
ASCII_UMLAUT_REPLACEMENTS
.get(repl_index)
.expect("Can find replacement"),
);
}
string
}

const WORDS_DIR: &str = "./words/";
Expand Down

0 comments on commit e8e49c6

Please sign in to comment.