Skip to content

Commit 7713f8a

Browse files
authored
Merge pull request #36 from djudd/compatibility-quick-checks
Add quick check implementations for NFKC & NFKD
2 parents 4e72fd1 + f2b8ffa commit 7713f8a

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
@@ -352,12 +352,24 @@ def gen_nfc_qc(prop_tables, out):
352352
gen_qc_match(prop_tables['NFC_QC'], out)
353353
out.write("}\n")
354354

355+
def gen_nfkc_qc(prop_tables, out):
356+
out.write("#[inline]\n")
357+
out.write("pub fn qc_nfkc(c: char) -> IsNormalized {\n")
358+
gen_qc_match(prop_tables['NFKC_QC'], out)
359+
out.write("}\n")
360+
355361
def gen_nfd_qc(prop_tables, out):
356362
out.write("#[inline]\n")
357363
out.write("pub fn qc_nfd(c: char) -> IsNormalized {\n")
358364
gen_qc_match(prop_tables['NFD_QC'], out)
359365
out.write("}\n")
360366

367+
def gen_nfkd_qc(prop_tables, out):
368+
out.write("#[inline]\n")
369+
out.write("pub fn qc_nfkd(c: char) -> IsNormalized {\n")
370+
gen_qc_match(prop_tables['NFKD_QC'], out)
371+
out.write("}\n")
372+
361373
def gen_combining_mark(general_category_mark, out):
362374
out.write("#[inline]\n")
363375
out.write("pub fn is_combining_mark(c: char) -> bool {\n")
@@ -446,9 +458,15 @@ def gen_tests(tests, out):
446458
gen_nfc_qc(data.norm_props, out)
447459
out.write("\n")
448460

461+
gen_nfkc_qc(data.norm_props, out)
462+
out.write("\n")
463+
449464
gen_nfd_qc(data.norm_props, out)
450465
out.write("\n")
451466

467+
gen_nfkd_qc(data.norm_props, out)
468+
out.write("\n")
469+
452470
gen_stream_safe(data.ss_leading, data.ss_trailing, out)
453471
out.write("\n")
454472

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)