diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /vendor/unicode-security/src/confusable_detection.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/unicode-security/src/confusable_detection.rs')
-rw-r--r-- | vendor/unicode-security/src/confusable_detection.rs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/vendor/unicode-security/src/confusable_detection.rs b/vendor/unicode-security/src/confusable_detection.rs new file mode 100644 index 000000000..dd1a0d25d --- /dev/null +++ b/vendor/unicode-security/src/confusable_detection.rs @@ -0,0 +1,39 @@ +//! [Confusable detection](https://www.unicode.org/reports/tr39/#Confusable_Detection) + +use core::iter; + +enum OnceOrMore<T, I> { + Once(iter::Once<T>), + More(I), +} + +impl<T, I> Iterator for OnceOrMore<T, I> +where + I: Iterator<Item = T>, +{ + type Item = T; + + fn next(&mut self) -> Option<T> { + use OnceOrMore::*; + match self { + Once(v) => v.next(), + More(i) => i.next(), + } + } +} + +type StaticSliceIterCloned = core::iter::Cloned<core::slice::Iter<'static, char>>; + +fn char_prototype(c: char) -> OnceOrMore<char, StaticSliceIterCloned> { + use crate::tables::confusable_detection::char_confusable_prototype; + match char_confusable_prototype(c) { + None => OnceOrMore::Once(iter::once(c)), + Some(l) => OnceOrMore::More(l.iter().cloned()), + } +} + +/// Calculate skeleton for string, as defined by UTS 39 +pub fn skeleton(s: &str) -> impl Iterator<Item = char> + '_ { + use unicode_normalization::UnicodeNormalization; + s.chars().nfd().flat_map(char_prototype).nfd() +} |