-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Initial version of public API surface
Also fixed `pedantic` `clippy` lints
- Loading branch information
Showing
43 changed files
with
759 additions
and
356 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright 2023 Alex Povel | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# betterletter | ||
|
||
Substitute alternative, ASCII-only spellings of special characters with their Unicode | ||
equivalents. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use itertools::Itertools; | ||
|
||
pub fn _power_set<C, T>(collection: C) -> Vec<Vec<T>> | ||
where | ||
C: IntoIterator<Item = T>, | ||
T: Clone, | ||
{ | ||
power_set_impl(collection, true) | ||
} | ||
|
||
pub fn power_set_without_empty<C, T>(collection: C) -> Vec<Vec<T>> | ||
where | ||
C: IntoIterator<Item = T>, | ||
T: Clone, | ||
{ | ||
power_set_impl(collection, false) | ||
} | ||
|
||
fn power_set_impl<C, T>(collection: C, include_empty_set: bool) -> Vec<Vec<T>> | ||
where | ||
C: IntoIterator<Item = T>, | ||
T: Clone, | ||
{ | ||
let vec = collection.into_iter().collect_vec(); | ||
|
||
// https://en.wikipedia.org/wiki/Power_set#Properties | ||
let mut result = Vec::with_capacity(2usize.checked_pow(vec.len() as u32).expect("Overflow")); | ||
|
||
let start = if include_empty_set { 0 } else { 1 }; | ||
|
||
for i in start..=vec.len() { | ||
result.extend(vec.iter().cloned().combinations(i)); | ||
} | ||
|
||
result | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::{_power_set, power_set_without_empty}; | ||
use crate::instrament; | ||
use rstest::rstest; | ||
|
||
instrament! { | ||
#[rstest] | ||
fn test_power_set( | ||
#[values(vec![], vec![1], vec![1, 2], vec![1, 2, 3])] | ||
collection: Vec<i32> | ||
) (|data: &TestPowerSet| { | ||
let result = _power_set(collection.clone()); | ||
insta::assert_yaml_snapshot!(data.to_string(), result); | ||
}) | ||
} | ||
|
||
instrament! { | ||
#[rstest] | ||
fn test_power_set_without_empty( | ||
#[values(vec![], vec![1], vec![1, 2], vec![1, 2, 3])] | ||
collection: Vec<i32> | ||
) (|data: &TestPowerSetWithoutEmpty| { | ||
let result = power_set_without_empty(collection.clone()); | ||
insta::assert_yaml_snapshot!(data.to_string(), result); | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,6 @@ | ||
use log::trace; | ||
#![allow(clippy::cargo_common_metadata)] | ||
|
||
pub mod instrament; | ||
|
||
pub fn titlecase(word: &str) -> String { | ||
let mut chars = word.chars(); | ||
let mut result = String::with_capacity(word.len()); | ||
|
||
if let Some(c) = chars.next() { | ||
for upper in c.to_uppercase() { | ||
result.push(upper); | ||
} | ||
} | ||
|
||
for c in chars { | ||
for lower in c.to_lowercase() { | ||
result.push(lower); | ||
} | ||
} | ||
|
||
result | ||
} | ||
|
||
pub fn is_compound_word(word: &str, predicate: &impl Fn(&str) -> bool) -> bool { | ||
trace!("Checking if word is valid compound word: '{}'", word); | ||
|
||
let indices = word.char_indices().skip(1); | ||
|
||
// Greedily fetch the longest possible prefix. Otherwise, we short-circuit and might | ||
// end up looking for (for example) "He" of "Heizölrechnung" and its suffix | ||
// "izölrechnung" (not a word), whereas we could have found "Heizöl" and "Rechnung" | ||
// instead. | ||
let mut highest_valid_index = None; | ||
for (i, _) in indices { | ||
let prefix = &word[..i]; | ||
|
||
if predicate(prefix) { | ||
highest_valid_index = Some(i); | ||
} | ||
} | ||
|
||
match highest_valid_index { | ||
Some(i) => { | ||
let suffix = &word[i..]; | ||
|
||
trace!( | ||
"Prefix '{}' found in word list, seeing if suffix '{}' is valid.", | ||
&word[..i], | ||
suffix | ||
); | ||
|
||
predicate(&titlecase(suffix)) | ||
|| predicate(suffix) | ||
|| is_compound_word(&titlecase(suffix), predicate) | ||
|| is_compound_word(suffix, predicate) | ||
} | ||
None => false, | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use rstest::rstest; | ||
|
||
#[rstest] | ||
#[case("hello", "Hello")] | ||
#[case("bItTe", "Bitte")] | ||
#[case("dANKE", "Danke")] | ||
#[case("übel", "Übel")] | ||
#[case("uebel", "Uebel")] | ||
#[case("😀", "😀")] | ||
#[case("ßuper", "SSuper")] | ||
#[case("ẞuperduper", "ẞuperduper")] | ||
#[case("WOW!!", "Wow!!")] | ||
#[case("ẞß", "ẞß")] | ||
fn test_titlecase(#[case] word: &str, #[case] expected: &str) { | ||
assert_eq!(titlecase(word), expected); | ||
} | ||
|
||
const WORDS: &[&str] = &["Süßwasser", "schwimm", "Bäder", "Mauer", "Dübel", "Kübel"]; | ||
|
||
#[rstest] | ||
#[case("Süßwasserschwimmbäder", true)] | ||
#[case("Mauerdübel", true)] | ||
#[case("Mauerdübelkübel", true)] | ||
#[case("Not a compound word", false)] | ||
#[case("Mauer好", false)] | ||
#[case("Mauerdjieojoid", false)] | ||
fn test_is_compound_word(#[case] word: &str, #[case] expected: bool) { | ||
assert_eq!(is_compound_word(word, &|w| WORDS.contains(&w)), expected); | ||
} | ||
} | ||
pub mod itertools; | ||
pub mod lookup; | ||
pub mod strings; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
..._iteration__tests__test_power_set-[].snap → ..._itertools__tests__test_power_set-[].snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
source: core/src/util/iteration.rs | ||
source: common/src/itertools.rs | ||
expression: result | ||
info: | ||
collection: [] | ||
|
2 changes: 1 addition & 1 deletion
2
...__tests__test_power_set-[_1,_2,_3,_].snap → ...__tests__test_power_set-[_1,_2,_3,_].snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
source: core/src/util/iteration.rs | ||
source: common/src/itertools.rs | ||
expression: result | ||
info: | ||
collection: | ||
|
2 changes: 1 addition & 1 deletion
2
...ion__tests__test_power_set-[_1,_2,_].snap → ...ols__tests__test_power_set-[_1,_2,_].snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
source: core/src/util/iteration.rs | ||
source: common/src/itertools.rs | ||
expression: result | ||
info: | ||
collection: | ||
|
2 changes: 1 addition & 1 deletion
2
...ration__tests__test_power_set-[_1,_].snap → ...rtools__tests__test_power_set-[_1,_].snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
source: core/src/util/iteration.rs | ||
source: common/src/itertools.rs | ||
expression: result | ||
info: | ||
collection: | ||
|
2 changes: 1 addition & 1 deletion
2
...sts__test_power_set_without_empty-[].snap → ...sts__test_power_set_without_empty-[].snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
source: core/src/util/iteration.rs | ||
source: common/src/itertools.rs | ||
expression: result | ||
info: | ||
collection: [] | ||
|
2 changes: 1 addition & 1 deletion
2
...power_set_without_empty-[_1,_2,_3,_].snap → ...power_set_without_empty-[_1,_2,_3,_].snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
source: core/src/util/iteration.rs | ||
source: common/src/itertools.rs | ||
expression: result | ||
info: | ||
collection: | ||
|
2 changes: 1 addition & 1 deletion
2
...st_power_set_without_empty-[_1,_2,_].snap → ...st_power_set_without_empty-[_1,_2,_].snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
source: core/src/util/iteration.rs | ||
source: common/src/itertools.rs | ||
expression: result | ||
info: | ||
collection: | ||
|
2 changes: 1 addition & 1 deletion
2
..._test_power_set_without_empty-[_1,_].snap → ..._test_power_set_without_empty-[_1,_].snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
source: core/src/util/iteration.rs | ||
source: common/src/itertools.rs | ||
expression: result | ||
info: | ||
collection: | ||
|
Oops, something went wrong.