Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

異字体セレクタを含む入力への対応: 文字列から異字体セレクタを取り除くメソッドを定義 #401

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod converter;
pub(crate) mod extension;
pub mod sequence_matcher;
mod trimmer;
62 changes: 62 additions & 0 deletions core/src/util/extension.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
pub(crate) trait CharExt {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [clippy] reported by reviewdog 🐶

warning: trait `CharExt` is never used
 --> core/src/util/extension.rs:1:18
  |
1 | pub(crate) trait CharExt {
  |                  ^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

次のPRで使用箇所が発生する予定

fn is_variation_selector(&self) -> bool;
}

impl CharExt for char {
/// 異字体セレクタかどうかを判別します
fn is_variation_selector(&self) -> bool {
matches!(self, '\u{FE00}'..='\u{FE0F}' | '\u{E0100}'..='\u{E01EF}')
}
}

pub(crate) trait StrExt {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [clippy] reported by reviewdog 🐶

warning: trait `StrExt` is never used
  --> core/src/util/extension.rs:12:18
   |
12 | pub(crate) trait StrExt {
   |                  ^^^^^^

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

次のPRで使用箇所が発生する予定

fn strip_variation_selectors(&self) -> String;
}

impl StrExt for str {
/// 文字列から異字体セレクタを取り除きます
fn strip_variation_selectors(&self) -> String {
self.chars()
.filter(|c| !c.is_variation_selector())
.collect()
}
}

#[cfg(test)]
mod tests {
use crate::util::extension::{CharExt, StrExt};

#[test]
fn is_variation_selector() {
assert_eq!('あ'.is_variation_selector(), false);
assert_eq!('亜'.is_variation_selector(), false);

assert_eq!('\u{FDFF}'.is_variation_selector(), false);
assert_eq!('\u{FE00}'.is_variation_selector(), true);

assert_eq!('\u{FE0F}'.is_variation_selector(), true);
assert_eq!('\u{FE10}'.is_variation_selector(), false);

assert_eq!('\u{E00FF}'.is_variation_selector(), false);
assert_eq!('\u{E0100}'.is_variation_selector(), true);

assert_eq!('\u{E01EF}'.is_variation_selector(), true);
assert_eq!('\u{E01F0}'.is_variation_selector(), false);
}

#[test]
fn strip_variation_selectors_逢坂() {
let normal = "\u{9022}\u{5742}"; // 逢坂
let variant = "\u{9022}\u{E0101}\u{5742}"; // 逢󠄁坂
assert_ne!(normal, variant);
assert_eq!(normal, variant.strip_variation_selectors());
}

#[test]
fn strip_variation_selectors_茨城() {
let normal = "\u{8328}\u{57CE}";
let variant = "\u{8328}\u{E0100}\u{57CE}";
assert_ne!(normal, variant);
assert_eq!(normal, variant.strip_variation_selectors());
}
}
Loading