Skip to content

ID_Compat_Math_Start and ID_Compat_Math_Continue #45

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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
  • Loading branch information
Danvil committed Jul 18, 2025
commit d7f2543d53b6f31d00b109c2a86eb34faa84819f
16 changes: 12 additions & 4 deletions scripts/unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
26 changes: 26 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}
45 changes: 45 additions & 0 deletions src/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
54 changes: 44 additions & 10 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use std::prelude::v1::*;
#[cfg(feature = "bench")]
use test::Bencher;

#[cfg(feature = "bench")]
use UnicodeXID;

#[cfg(feature = "bench")]
Expand All @@ -24,7 +23,7 @@ fn cargo_is_xid_start(b: &mut Bencher) {
let string = iter::repeat('a').take(4096).collect::<String>();

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")]
Expand All @@ -42,7 +41,7 @@ fn cargo_xid_continue(b: &mut Bencher) {
let string = iter::repeat('a').take(4096).collect::<String>();

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")]
Expand All @@ -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 <U+{:X}>: {}",
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);
}
}

Expand All @@ -69,17 +76,17 @@ 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);
}
}

#[test]
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);
}
}

Expand All @@ -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