Skip to content

Commit

Permalink
Appease clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
David Purdum committed Sep 8, 2022
1 parent 7b47a6f commit 8a19a64
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 84 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ pub trait Casing<T: AsRef<str>> {
/// .to_case(Case::Snake)
/// );
/// ```
#[allow(clippy::wrong_self_convention)]
fn from_case(&self, case: Case) -> StateConverter<T>;

/// Creates a `StateConverter` struct initialized with the boundaries
Expand Down
86 changes: 3 additions & 83 deletions src/segmentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl Boundary {
one_iter.any(|a| boundary.detect_one(a))
|| two_iter_and_upper.any(|((a, b), is_acro)| boundary.detect_two(a, b) && !is_acro)
|| three_iter.any(|((a, b), c)| boundary.detect_three(a, b, c))
}).map(|b| *b).collect()
}).copied().collect()
}

/// The default list of boundaries used when `Casing::to_case` is called directly
Expand Down Expand Up @@ -330,53 +330,7 @@ fn grapheme_is_lowercase(c: &str) -> bool {
c.to_uppercase() != c.to_lowercase() && c == c.to_lowercase()
}

// idea: make a bitset for each boundary. Its fixed size,
// and can be copied. Also no fear in adding duplicates

// gross
pub fn split_old<'a, T: ?Sized>(s: &'a T, boundaries: &[Boundary]) -> Vec<&'a str>
where
T: AsRef<str>,
{
let s = s.as_ref();

let single_splits = s
.graphemes(true)
.enumerate()
.filter(|(_, c)| boundaries.iter().any(|b| b.detect_one(*c)))
.map(|(i, _)| i + 1)
.collect();

let words = replace_at_indicies(s, single_splits);

let final_words = words.iter().flat_map(|&w| {
let left_iter = w.graphemes(true);
let mid_iter = w.graphemes(true).skip(1);
let right_iter = w.graphemes(true).skip(2);

let three_iter = left_iter.clone().zip(mid_iter.clone()).zip(right_iter);
let two_iter = left_iter.clone().zip(mid_iter);

let mut splits: Vec<usize> = three_iter
.enumerate()
.filter(|(_, ((c, d), e))| boundaries.iter().any(|b| b.detect_three(*c, *d, *e)))
.map(|(i, _)| i + 1)
.chain(
two_iter
.enumerate()
.filter(|(_, (c, d))| boundaries.iter().any(|b| b.detect_two(*c, *d)))
.map(|(i, _)| i + 1),
)
.collect();
splits.sort_unstable();

split_on_indicies(w, splits)
});

final_words.rev().filter(|s| !s.is_empty()).collect()
}

pub fn split<'a, T: ?Sized>(s: &'a T, boundaries: &[Boundary]) -> Vec<String>
pub fn split<T>(s: T, boundaries: &[Boundary]) -> Vec<String>
where
T: AsRef<str>,
{
Expand Down Expand Up @@ -426,8 +380,6 @@ where
words.push(std::mem::take(&mut word));
word.push_str(c);
}
// dont push an empty string, do nothing
_ => {}
}
}
words.push(word);
Expand Down Expand Up @@ -460,39 +412,7 @@ where
}
*/

return words.into_iter().filter(|s| !s.is_empty()).collect();
}

pub fn replace_at_indicies(s: &str, splits: Vec<usize>) -> Vec<&str> {
let mut words = Vec::new();

let mut first = s;
let mut second;
for &x in splits.iter().rev() {
let pair = first.split_at(x);
first = &pair.0[..(pair.0.len() - 1)];
second = pair.1;
words.push(second);
}
words.push(first);

words
}

pub fn split_on_indicies(s: &str, splits: Vec<usize>) -> Vec<&str> {
let mut words = Vec::new();

let mut first = s;
let mut second;
for &x in splits.iter().rev() {
let pair = first.split_at(x);
first = pair.0;
second = pair.1;
words.push(second);
}
words.push(first);

words
words.into_iter().filter(|s| !s.is_empty()).collect()
}

#[cfg(test)]
Expand Down
4 changes: 3 additions & 1 deletion tests/string_types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use convert_case::{Case, Casing};

use std::ffi::{OsString};
// use std::ffi::{OsString};

#[test]
fn string_type() {
Expand Down Expand Up @@ -29,6 +29,7 @@ fn string_ref_type() {
);
}

/*
#[test]
fn os_string_type() {
let s: OsString = OsString::from("rust_programming_language");
Expand All @@ -37,3 +38,4 @@ fn os_string_type() {
s.to_case(Case::Pascal),
);
}
*/

0 comments on commit 8a19a64

Please sign in to comment.