Skip to content

Commit

Permalink
first return all-hyphens & all_underscores
Browse files Browse the repository at this point in the history
  • Loading branch information
ToBinio committed Jul 31, 2023
1 parent b63ec37 commit 6b66356
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ impl Iterator for Names {
return None;
}

//map the count so the first value is the last one (all "-"), the second one is the first one (all "_")...
let used_count = *count as isize - 1 + self.max_count as isize;

for (sep_index, char_index) in self.separator_indexes[..self.separator_count].iter().enumerate() {
let char = if *count & (1 << sep_index) == 0 { b'-' } else { b'_' };
let char = if used_count & (1 << sep_index) == 0 { b'_' } else { b'-' };
// SAFETY: We validated that `char_index` is a valid UTF-8 codepoint
#[allow(unsafe_code)]
unsafe {
Expand Down
12 changes: 9 additions & 3 deletions tests/names/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,25 @@ fn max_permutation_count_causes_error() {
fn permutations() {
for (name, expected) in [
("parking_lot", &["parking_lot", "parking-lot"] as &[_]), // the input name is always the first one returned.
(
"a-b_c-d", // input name -> all-hyphens -> all_underscores -> rest
&[
"a-b_c-d", "a-b-c-d", "a_b_c_d", "a-b_c_d", "a_b-c_d", "a-b-c_d", "a_b_c-d", "a_b-c-d",
],
),
("a_b", &["a_b", "a-b"]),
("a-b", &["a-b", "a_b"]),
("a-b-c", &["a-b-c", "a_b-c", "a-b_c", "a_b_c"]),
("a-b-c", &["a-b-c", "a_b_c", "a-b_c", "a_b-c"]),
(
"a-b-c-d",
&[
"a-b-c-d", "a_b-c-d", "a-b_c-d", "a_b_c-d", "a-b-c_d", "a_b-c_d", "a-b_c_d", "a_b_c_d",
"a-b-c-d", "a_b_c_d", "a-b_c_d", "a_b-c_d", "a-b-c_d", "a_b_c-d", "a-b_c-d", "a_b-c-d",
],
),
(
"a_b_c_d",
&[
"a_b_c_d", "a-b-c-d", "a_b-c-d", "a-b_c-d", "a_b_c-d", "a-b-c_d", "a_b-c_d", "a-b_c_d",
"a_b_c_d", "a-b-c-d", "a-b_c_d", "a_b-c_d", "a-b-c_d", "a_b_c-d", "a-b_c-d", "a_b-c-d",
],
),
] {
Expand Down

0 comments on commit 6b66356

Please sign in to comment.