Skip to content

Commit f2b8ffa

Browse files
author
David Judd
committed
Add quick check implementations for NFKC & NFKD
1 parent f08c00c commit f2b8ffa

File tree

5 files changed

+1049
-0
lines changed

5 files changed

+1049
-0
lines changed

scripts/unicode.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,24 @@ def gen_nfc_qc(prop_tables, out):
347347
gen_qc_match(prop_tables['NFC_QC'], out)
348348
out.write("}\n")
349349

350+
def gen_nfkc_qc(prop_tables, out):
351+
out.write("#[inline]\n")
352+
out.write("pub fn qc_nfkc(c: char) -> IsNormalized {\n")
353+
gen_qc_match(prop_tables['NFKC_QC'], out)
354+
out.write("}\n")
355+
350356
def gen_nfd_qc(prop_tables, out):
351357
out.write("#[inline]\n")
352358
out.write("pub fn qc_nfd(c: char) -> IsNormalized {\n")
353359
gen_qc_match(prop_tables['NFD_QC'], out)
354360
out.write("}\n")
355361

362+
def gen_nfkd_qc(prop_tables, out):
363+
out.write("#[inline]\n")
364+
out.write("pub fn qc_nfkd(c: char) -> IsNormalized {\n")
365+
gen_qc_match(prop_tables['NFKD_QC'], out)
366+
out.write("}\n")
367+
356368
def gen_combining_mark(general_category_mark, out):
357369
out.write("#[inline]\n")
358370
out.write("pub fn is_combining_mark(c: char) -> bool {\n")
@@ -441,9 +453,15 @@ def gen_tests(tests, out):
441453
gen_nfc_qc(data.norm_props, out)
442454
out.write("\n")
443455

456+
gen_nfkc_qc(data.norm_props, out)
457+
out.write("\n")
458+
444459
gen_nfd_qc(data.norm_props, out)
445460
out.write("\n")
446461

462+
gen_nfkd_qc(data.norm_props, out)
463+
out.write("\n")
464+
447465
gen_stream_safe(data.ss_leading, data.ss_trailing, out)
448466
out.write("\n")
449467

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,14 @@ pub use quick_check::{
4747
IsNormalized,
4848
is_nfc,
4949
is_nfc_quick,
50+
is_nfkc,
51+
is_nfkc_quick,
5052
is_nfc_stream_safe,
5153
is_nfc_stream_safe_quick,
5254
is_nfd,
5355
is_nfd_quick,
56+
is_nfkd,
57+
is_nfkd_quick,
5458
is_nfd_stream_safe,
5559
is_nfd_stream_safe_quick,
5660
};

src/quick_check.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,25 @@ pub fn is_nfc_quick<I: Iterator<Item=char>>(s: I) -> IsNormalized {
7070
quick_check(s, tables::qc_nfc, false)
7171
}
7272

73+
74+
/// Quickly check if a string is in NFKC.
75+
#[inline]
76+
pub fn is_nfkc_quick<I: Iterator<Item=char>>(s: I) -> IsNormalized {
77+
quick_check(s, tables::qc_nfkc, false)
78+
}
79+
7380
/// Quickly check if a string is in NFD.
7481
#[inline]
7582
pub fn is_nfd_quick<I: Iterator<Item=char>>(s: I) -> IsNormalized {
7683
quick_check(s, tables::qc_nfd, false)
7784
}
7885

86+
/// Quickly check if a string is in NFKD.
87+
#[inline]
88+
pub fn is_nfkd_quick<I: Iterator<Item=char>>(s: I) -> IsNormalized {
89+
quick_check(s, tables::qc_nfkd, false)
90+
}
91+
7992
/// Quickly check if a string is Stream-Safe NFC.
8093
#[inline]
8194
pub fn is_nfc_stream_safe_quick<I: Iterator<Item=char>>(s: I) -> IsNormalized {
@@ -98,6 +111,16 @@ pub fn is_nfc(s: &str) -> bool {
98111
}
99112
}
100113

114+
/// Authoritatively check if a string is in NFKC.
115+
#[inline]
116+
pub fn is_nfkc(s: &str) -> bool {
117+
match is_nfkc_quick(s.chars()) {
118+
IsNormalized::Yes => true,
119+
IsNormalized::No => false,
120+
IsNormalized::Maybe => s.chars().eq(s.chars().nfkc()),
121+
}
122+
}
123+
101124
/// Authoritatively check if a string is in NFD.
102125
#[inline]
103126
pub fn is_nfd(s: &str) -> bool {
@@ -108,6 +131,16 @@ pub fn is_nfd(s: &str) -> bool {
108131
}
109132
}
110133

134+
/// Authoritatively check if a string is in NFKD.
135+
#[inline]
136+
pub fn is_nfkd(s: &str) -> bool {
137+
match is_nfkd_quick(s.chars()) {
138+
IsNormalized::Yes => true,
139+
IsNormalized::No => false,
140+
IsNormalized::Maybe => s.chars().eq(s.chars().nfkd()),
141+
}
142+
}
143+
111144
/// Authoritatively check if a string is Stream-Safe NFC.
112145
#[inline]
113146
pub fn is_nfc_stream_safe(s: &str) -> bool {

0 commit comments

Comments
 (0)