From d7f2543d53b6f31d00b109c2a86eb34faa84819f Mon Sep 17 00:00:00 2001 From: David Weikersdorfer <517608+Danvil@users.noreply.github.com> Date: Thu, 17 Jul 2025 18:20:41 -0700 Subject: [PATCH] ID_Compat_Math_Start and ID_Compat_Math_Continue * Adds two new functions to check if a character is part of the Mathematical Compatibility Notation Profile as defined by Unicode. * Small cleanup of tests and improved error message for failed tests --- scripts/unicode.py | 16 ++++++++++---- src/lib.rs | 26 ++++++++++++++++++++++ src/tables.rs | 45 ++++++++++++++++++++++++++++++++++++++ src/tests.rs | 54 +++++++++++++++++++++++++++++++++++++--------- 4 files changed, 127 insertions(+), 14 deletions(-) diff --git a/scripts/unicode.py b/scripts/unicode.py index 37d4fbb..e5fcf02 100755 --- a/scripts/unicode.py +++ b/scripts/unicode.py @@ -158,14 +158,12 @@ def emit_table(f, name, t_data, t_type = "&[(char, char)]", is_pub=True, format_table_content(f, data, 8) f.write("\n ];\n\n") -def emit_property_module(f, mod, tbl, emit): - f.write("pub mod %s {\n" % mod) +def emit_property_module_contents(f, tbl, emit): for cat in sorted(emit): emit_table(f, "%s_table" % cat, tbl[cat]) f.write(" pub fn %s(c: char) -> bool {\n" % cat) f.write(" super::bsearch_range_table(c, %s_table)\n" % cat) f.write(" }\n\n") - f.write("}\n\n") if __name__ == "__main__": r = "tables.rs" @@ -187,6 +185,16 @@ def emit_property_module(f, mod, tbl, emit): """ % unicode_version) emit_bsearch_range_table(rf) + rf.write("pub mod derived_property {\n") + + # XID_Start and XID_Continue want_derived = ["XID_Start", "XID_Continue"] derived = load_properties("DerivedCoreProperties.txt", want_derived) - emit_property_module(rf, "derived_property", derived, want_derived) + emit_property_module_contents(rf, derived, want_derived) + + # ID_Compat_Math_Start and ID_Compat_Math_Continue + want_derived = ["ID_Compat_Math_Start", "ID_Compat_Math_Continue"] + derived = load_properties("PropList.txt", want_derived) + emit_property_module_contents(rf, derived, want_derived) + + rf.write("}\n\n") diff --git a/src/lib.rs b/src/lib.rs index 3a982e5..b34f5bc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -67,6 +67,22 @@ pub trait UnicodeXID { /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications), /// mostly similar to 'ID_Continue' but modified for closure under NFKx. fn is_xid_continue(self) -> bool; + + /// Returns whether the specified `char` satisfies the + /// `ID_Compat_Math_Start` Unicode property. + /// + /// `ID_Compat_Math_Start` is a Unicode Property specified in + /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications) + /// as part of the Mathematical Compatibility Notation Profile. + fn is_id_compat_math_start(self) -> bool; + + /// Returns whether the specified `char` satisfies the + /// `ID_Compat_Math_Continue` Unicode property. + /// + /// `ID_Compat_Math_Continue` is a Unicode Property specified in + /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications) + /// as part of the Mathematical Compatibility Notation Profile. + fn is_id_compat_math_continue(self) -> bool; } impl UnicodeXID for char { @@ -87,4 +103,14 @@ impl UnicodeXID for char { || self == '_' || (self > '\x7f' && derived_property::XID_Continue(self)) } + + #[inline] + fn is_id_compat_math_start(self) -> bool { + derived_property::ID_Compat_Math_Start(self) + } + + #[inline] + fn is_id_compat_math_continue(self) -> bool { + derived_property::ID_Compat_Math_Continue(self) + } } diff --git a/src/tables.rs b/src/tables.rs index 972c1d3..83c7dee 100644 --- a/src/tables.rs +++ b/src/tables.rs @@ -1534,4 +1534,49 @@ pub mod derived_property { pub fn XID_Start(c: char) -> bool { super::bsearch_range_table(c, XID_Start_table) } + + static ID_Compat_Math_Continue_table: &[(char, char)] = &[ + ('\u{b2}', '\u{b3}'), + ('\u{b9}', '\u{b9}'), + ('\u{2070}', '\u{2070}'), + ('\u{2074}', '\u{207e}'), + ('\u{2080}', '\u{208e}'), + ('\u{2202}', '\u{2202}'), + ('\u{2207}', '\u{2207}'), + ('\u{221e}', '\u{221e}'), + ('\u{1d6c1}', '\u{1d6c1}'), + ('\u{1d6db}', '\u{1d6db}'), + ('\u{1d6fb}', '\u{1d6fb}'), + ('\u{1d715}', '\u{1d715}'), + ('\u{1d735}', '\u{1d735}'), + ('\u{1d74f}', '\u{1d74f}'), + ('\u{1d76f}', '\u{1d76f}'), + ('\u{1d789}', '\u{1d789}'), + ('\u{1d7a9}', '\u{1d7a9}'), + ('\u{1d7c3}', '\u{1d7c3}'), + ]; + + pub fn ID_Compat_Math_Continue(c: char) -> bool { + super::bsearch_range_table(c, ID_Compat_Math_Continue_table) + } + + static ID_Compat_Math_Start_table: &[(char, char)] = &[ + ('\u{2202}', '\u{2202}'), + ('\u{2207}', '\u{2207}'), + ('\u{221e}', '\u{221e}'), + ('\u{1d6c1}', '\u{1d6c1}'), + ('\u{1d6db}', '\u{1d6db}'), + ('\u{1d6fb}', '\u{1d6fb}'), + ('\u{1d715}', '\u{1d715}'), + ('\u{1d735}', '\u{1d735}'), + ('\u{1d74f}', '\u{1d74f}'), + ('\u{1d76f}', '\u{1d76f}'), + ('\u{1d789}', '\u{1d789}'), + ('\u{1d7a9}', '\u{1d7a9}'), + ('\u{1d7c3}', '\u{1d7c3}'), + ]; + + pub fn ID_Compat_Math_Start(c: char) -> bool { + super::bsearch_range_table(c, ID_Compat_Math_Start_table) + } } diff --git a/src/tests.rs b/src/tests.rs index 744c80d..141910f 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -15,7 +15,6 @@ use std::prelude::v1::*; #[cfg(feature = "bench")] use test::Bencher; -#[cfg(feature = "bench")] use UnicodeXID; #[cfg(feature = "bench")] @@ -24,7 +23,7 @@ fn cargo_is_xid_start(b: &mut Bencher) { let string = iter::repeat('a').take(4096).collect::(); b.bytes = string.len() as u64; - b.iter(|| string.chars().all(super::UnicodeXID::is_xid_start)); + b.iter(|| string.chars().all(UnicodeXID::is_xid_start)); } #[cfg(feature = "bench")] @@ -42,7 +41,7 @@ fn cargo_xid_continue(b: &mut Bencher) { let string = iter::repeat('a').take(4096).collect::(); b.bytes = string.len() as u64; - b.iter(|| string.chars().all(super::UnicodeXID::is_xid_continue)); + b.iter(|| string.chars().all(UnicodeXID::is_xid_continue)); } #[cfg(feature = "bench")] @@ -54,12 +53,20 @@ fn stdlib_xid_continue(b: &mut Bencher) { b.iter(|| string.chars().all(char::is_xid_continue)); } +fn assert_ch_pretty(ok: bool, ch: char) { + assert!( + ok, + "test failed for unicode character : {}", + ch as u32, ch + ); +} + #[test] fn test_is_xid_start() { let chars = ['A', 'Z', 'a', 'z', '\u{1000d}', '\u{10026}']; - for ch in &chars { - assert!(super::UnicodeXID::is_xid_start(*ch), "{}", ch); + for &ch in &chars { + assert_ch_pretty(UnicodeXID::is_xid_start(ch), ch); } } @@ -69,8 +76,8 @@ fn test_is_not_xid_start() { '\x00', '\x01', '0', '9', ' ', '[', '<', '{', '(', '\u{02c2}', '\u{ffff}', ]; - for ch in &chars { - assert!(!super::UnicodeXID::is_xid_start(*ch), "{}", ch); + for &ch in &chars { + assert_ch_pretty(!UnicodeXID::is_xid_start(ch), ch); } } @@ -78,8 +85,8 @@ fn test_is_not_xid_start() { fn test_is_xid_continue() { let chars = ['0', '9', 'A', 'Z', 'a', 'z', '_', '\u{1000d}', '\u{10026}']; - for ch in &chars { - assert!(super::UnicodeXID::is_xid_continue(*ch), "{}", ch); + for &ch in &chars { + assert_ch_pretty(UnicodeXID::is_xid_continue(ch), ch); } } @@ -90,6 +97,33 @@ fn test_is_not_xid_continue() { ]; for &ch in &chars { - assert!(!super::UnicodeXID::is_xid_continue(ch), "{}", ch); + assert_ch_pretty(!UnicodeXID::is_xid_continue(ch), ch); + } +} + +#[test] +fn test_is_id_compat_math_start() { + let chars = ['∇', '∂', '∞']; + + for &ch in &chars { + assert_ch_pretty(UnicodeXID::is_id_compat_math_start(ch), ch); + } +} + +#[test] +fn test_is_not_id_compat_math_start() { + let chars = ['²', '³', '₂', '₃']; + + for &ch in &chars { + assert_ch_pretty(!UnicodeXID::is_xid_start(ch), ch); + } +} + +#[test] +fn test_is_id_compat_math_continue() { + let chars = ['∇', '∂', '∞', '²', '³', '₂', '₃']; + + for &ch in &chars { + assert_ch_pretty(UnicodeXID::is_id_compat_math_continue(ch), ch); } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy