Skip to content

Add quick check implementations for NFKC & NFKD #36

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

Merged
merged 1 commit into from
Jan 14, 2019
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
18 changes: 18 additions & 0 deletions scripts/unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,12 +347,24 @@ def gen_nfc_qc(prop_tables, out):
gen_qc_match(prop_tables['NFC_QC'], out)
out.write("}\n")

def gen_nfkc_qc(prop_tables, out):
out.write("#[inline]\n")
out.write("pub fn qc_nfkc(c: char) -> IsNormalized {\n")
gen_qc_match(prop_tables['NFKC_QC'], out)
out.write("}\n")

def gen_nfd_qc(prop_tables, out):
out.write("#[inline]\n")
out.write("pub fn qc_nfd(c: char) -> IsNormalized {\n")
gen_qc_match(prop_tables['NFD_QC'], out)
out.write("}\n")

def gen_nfkd_qc(prop_tables, out):
out.write("#[inline]\n")
out.write("pub fn qc_nfkd(c: char) -> IsNormalized {\n")
gen_qc_match(prop_tables['NFKD_QC'], out)
out.write("}\n")

def gen_combining_mark(general_category_mark, out):
out.write("#[inline]\n")
out.write("pub fn is_combining_mark(c: char) -> bool {\n")
Expand Down Expand Up @@ -441,9 +453,15 @@ def gen_tests(tests, out):
gen_nfc_qc(data.norm_props, out)
out.write("\n")

gen_nfkc_qc(data.norm_props, out)
out.write("\n")

gen_nfd_qc(data.norm_props, out)
out.write("\n")

gen_nfkd_qc(data.norm_props, out)
out.write("\n")

gen_stream_safe(data.ss_leading, data.ss_trailing, out)
out.write("\n")

Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ pub use quick_check::{
IsNormalized,
is_nfc,
is_nfc_quick,
is_nfkc,
is_nfkc_quick,
is_nfc_stream_safe,
is_nfc_stream_safe_quick,
is_nfd,
is_nfd_quick,
is_nfkd,
is_nfkd_quick,
is_nfd_stream_safe,
is_nfd_stream_safe_quick,
};
Expand Down
33 changes: 33 additions & 0 deletions src/quick_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,25 @@ pub fn is_nfc_quick<I: Iterator<Item=char>>(s: I) -> IsNormalized {
quick_check(s, tables::qc_nfc, false)
}


/// Quickly check if a string is in NFKC.
#[inline]
pub fn is_nfkc_quick<I: Iterator<Item=char>>(s: I) -> IsNormalized {
quick_check(s, tables::qc_nfkc, false)
}

/// Quickly check if a string is in NFD.
#[inline]
pub fn is_nfd_quick<I: Iterator<Item=char>>(s: I) -> IsNormalized {
quick_check(s, tables::qc_nfd, false)
}

/// Quickly check if a string is in NFKD.
#[inline]
pub fn is_nfkd_quick<I: Iterator<Item=char>>(s: I) -> IsNormalized {
quick_check(s, tables::qc_nfkd, false)
}

/// Quickly check if a string is Stream-Safe NFC.
#[inline]
pub fn is_nfc_stream_safe_quick<I: Iterator<Item=char>>(s: I) -> IsNormalized {
Expand All @@ -98,6 +111,16 @@ pub fn is_nfc(s: &str) -> bool {
}
}

/// Authoritatively check if a string is in NFKC.
#[inline]
pub fn is_nfkc(s: &str) -> bool {
match is_nfkc_quick(s.chars()) {
IsNormalized::Yes => true,
IsNormalized::No => false,
IsNormalized::Maybe => s.chars().eq(s.chars().nfkc()),
}
}

/// Authoritatively check if a string is in NFD.
#[inline]
pub fn is_nfd(s: &str) -> bool {
Expand All @@ -108,6 +131,16 @@ pub fn is_nfd(s: &str) -> bool {
}
}

/// Authoritatively check if a string is in NFKD.
#[inline]
pub fn is_nfkd(s: &str) -> bool {
match is_nfkd_quick(s.chars()) {
IsNormalized::Yes => true,
IsNormalized::No => false,
IsNormalized::Maybe => s.chars().eq(s.chars().nfkd()),
}
}

/// Authoritatively check if a string is Stream-Safe NFC.
#[inline]
pub fn is_nfc_stream_safe(s: &str) -> bool {
Expand Down
Loading