- Notifications
You must be signed in to change notification settings - Fork5
Add mixed-script detection#6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionsCargo.toml
This file contains hidden or 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
20 changes: 20 additions & 0 deletionssrc/general_security_profile.rs
This file contains hidden or 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,20 @@ | ||
| //! Utilities for working with the [General Security Profile](https://www.unicode.org/reports/tr39/#General_Security_Profile) | ||
| //! for identifiers | ||
| use crate::tables::identifier_status as is; | ||
| /// Methods for determining characters not restricted from use for identifiers. | ||
| pub trait GeneralSecurityProfile { | ||
| /// Returns whether the character is not restricted from use for identifiers. | ||
| fn identifier_allowed(self) -> bool; | ||
| } | ||
| impl GeneralSecurityProfile for char { | ||
| #[inline] | ||
| fn identifier_allowed(self) -> bool { is::identifier_status_allowed(self) } | ||
| } | ||
| impl GeneralSecurityProfile for &'_ str { | ||
| #[inline] | ||
| fn identifier_allowed(self) -> bool { self.chars().all(is::identifier_status_allowed) } | ||
| } |
23 changes: 9 additions & 14 deletionssrc/lib.rs
This file contains hidden or 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
129 changes: 129 additions & 0 deletionssrc/mixed_script.rs
This file contains hidden or 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,129 @@ | ||
| //! [Mixed-script detection](https://www.unicode.org/reports/tr39/#Mixed_Script_Detection) | ||
| use unicode_script::{Script, ScriptExtension}; | ||
| /// An Augmented script set, as defined by UTS 39 | ||
| /// | ||
| /// https://www.unicode.org/reports/tr39/#def-augmented-script-set | ||
| pub struct AugmentedScriptSet { | ||
| /// The base ScriptExtension value | ||
| pub base: ScriptExtension, | ||
| /// Han With Bopomofo | ||
| pub hanb: bool, | ||
| /// Japanese | ||
| pub jpan: bool, | ||
| /// Korean | ||
| pub kore: bool, | ||
| } | ||
| impl From<ScriptExtension> for AugmentedScriptSet { | ||
| fn from(ext: ScriptExtension) -> Self { | ||
| let mut hanb = false; | ||
| let mut jpan = false; | ||
| let mut kore = false; | ||
| if ext == ScriptExtension::Single(Script::Common) || | ||
| ext == ScriptExtension::Single(Script::Inherited) || | ||
| ext.contains_script(Script::Han) { | ||
| hanb = true; | ||
| jpan = true; | ||
| kore = true; | ||
| } else { | ||
| if ext.contains_script(Script::Hiragana) || ext.contains_script(Script::Katakana) { | ||
| jpan = true; | ||
| } | ||
| if ext.contains_script(Script::Hangul) { | ||
| kore = true; | ||
| } | ||
| if ext.contains_script(Script::Bopomofo) { | ||
| hanb = true; | ||
| } | ||
| } | ||
| Self { | ||
| base: ext, | ||
| hanb, jpan, kore | ||
| } | ||
| } | ||
| } | ||
| impl From<char> for AugmentedScriptSet { | ||
| fn from(c: char) -> Self { | ||
| AugmentedScriptSet::for_char(c) | ||
| } | ||
| } | ||
| impl From<&'_ str> for AugmentedScriptSet { | ||
| fn from(s: &'_ str) -> Self { | ||
| AugmentedScriptSet::for_str(s) | ||
| } | ||
| } | ||
| impl Default for AugmentedScriptSet { | ||
| fn default() -> Self { | ||
| AugmentedScriptSet { | ||
| base: ScriptExtension::Single(Script::Common), | ||
| hanb: true, | ||
| jpan: true, | ||
| kore: true, | ||
| } | ||
| } | ||
| } | ||
| impl AugmentedScriptSet { | ||
| /// Intersect this set with another | ||
| pub fn intersect(mut self, other: Self) -> Self { | ||
| self.base = self.base.intersect(other.base); | ||
| self.hanb = self.hanb && other.hanb; | ||
| self.jpan = self.jpan && other.jpan; | ||
| self.kore = self.kore && other.kore; | ||
| self | ||
| } | ||
| /// Check if the set is empty | ||
| pub fn is_empty(&self) -> bool { | ||
| self.base.is_empty() && ! self.hanb && !self.jpan && !self.kore | ||
| } | ||
| /// Check if the set is "All" (Common or Inherited) | ||
| pub fn is_all(&self) -> bool { | ||
| self.base == ScriptExtension::Single(Script::Common) || | ||
| self.base == ScriptExtension::Single(Script::Inherited) | ||
| } | ||
| /// Construct an AugmentedScriptSet for a given character | ||
| pub fn for_char(c: char) -> Self { | ||
| ScriptExtension::from(c).into() | ||
| } | ||
| /// Find the [resolved script set](https://www.unicode.org/reports/tr39/#def-resolved-script-set) of a given string | ||
| pub fn for_str(s: &str) -> Self { | ||
| let mut set = AugmentedScriptSet::default(); | ||
| for ch in s.chars() { | ||
| set = set.intersect(ch.into()) | ||
| } | ||
| set | ||
| } | ||
| } | ||
| /// Extension trait for [mixed-script detection](https://www.unicode.org/reports/tr39/#Mixed_Script_Detection) | ||
| pub trait MixedScript { | ||
| /// Check if a string is [single-script](https://www.unicode.org/reports/tr39/#def-single-script) | ||
| /// | ||
| /// Note that a single-script string may still contain multiple Script properties! | ||
| fn is_single_script(self) -> bool; | ||
| /// Find the [resolved script set](https://www.unicode.org/reports/tr39/#def-resolved-script-set) of a given string | ||
| fn resolve_script_set(self) -> AugmentedScriptSet; | ||
| } | ||
| impl MixedScript for &'_ str { | ||
| fn is_single_script(self) -> bool { | ||
| !AugmentedScriptSet::for_str(self).is_empty() | ||
| } | ||
| fn resolve_script_set(self) -> AugmentedScriptSet { | ||
| self.into() | ||
| } | ||
| } |
14 changes: 7 additions & 7 deletionssrc/tests.rs
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.