Skip to content
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

Add function to parse script from string #7

Merged
merged 4 commits into from
Jun 28, 2021
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
72 changes: 68 additions & 4 deletions scripts/unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,16 @@

#![allow(missing_docs, non_upper_case_globals, non_snake_case)]

use super::ScriptExtension;
pub use tables_impl::*;

#[rustfmt::skip]
mod tables_impl {
use crate::ScriptExtension;
'''

# Close `mod impl {`
ending='''
}
'''

UNICODE_VERSION = (13, 0, 0)
Expand Down Expand Up @@ -239,7 +248,21 @@ def emit_enums(f, script_list, extension_list, longforms):
f.write(" /// %s\n pub const %s: ScriptExtension = %s;\n" % (longform, name, expr))
f.write("""}

impl Script {
""")

# Generate implementation for the `Script`
generate_script_impl(f)


def generate_script_impl(f):
"""Generates an `impl Script { ... }` section with all the required functions"""

# Open `impl Script` section.
f.write("""impl Script {
""")

# Generate impl of `inner_full_name`.
f.write("""
#[inline]
pub(crate) fn inner_full_name(self) -> &'static str {
match self {
Expand All @@ -251,7 +274,26 @@ def emit_enums(f, script_list, extension_list, longforms):
f.write(" Script::%s => \"%s\",\n" % (longforms[script], longforms[script]))
f.write(""" }
}
""")

# Generate impl of `inner_from_full_name`.
f.write("""
#[inline]
pub(crate) fn inner_from_full_name(input: &str) -> Option<Self> {
match input {
"Unknown" => Some(Script::Unknown),
"Common" => Some(Script::Common),
"Inherited" => Some(Script::Inherited),
""")
for script in script_list:
f.write(" \"%s\" => Some(Script::%s),\n" % (longforms[script], longforms[script]))
f.write(" _ => None,\n" )
f.write(""" }
}
""")

# Generate impl of `inner_short_name`
f.write("""
#[inline]
pub(crate) fn inner_short_name(self) -> &'static str {
match self {
Expand All @@ -263,7 +305,25 @@ def emit_enums(f, script_list, extension_list, longforms):
f.write(" Script::%s => \"%s\",\n" % (longforms[script], script))
f.write(""" }
}
""")

# Generate impl of `inner_from_short_name`
f.write("""
#[inline]
pub(crate) fn inner_from_short_name(input: &str) -> Option<Self> {
match input {
"Zyyy" => Some(Script::Common),
"Zinh" => Some(Script::Inherited),
""")
for script in script_list:
f.write(" \"%s\" => Some(Script::%s),\n" % (script, longforms[script]))
f.write(""" _ => None,\n""")
f.write(""" }
}
""")

# Generate impl of `for_integer`
f.write("""
#[inline]
pub(crate) fn for_integer(value: u8) -> Self {
match value {
Expand All @@ -273,6 +333,10 @@ def emit_enums(f, script_list, extension_list, longforms):
f.write(""" _ => unreachable!(),
}
}
""")

# Close `impl Script` section
f.write("""
}
""")

Expand All @@ -281,8 +345,6 @@ def extension_name(ext):
return "script_extensions::%s" % "_".join([e.upper() for e in ext])




if __name__ == "__main__":
r = "tables.rs"
if os.path.exists(r):
Expand Down Expand Up @@ -336,3 +398,5 @@ def extension_name(ext):
is_pub=False , pfun=lambda x: "(%s,%s,%s)" % (escape_char(x[0]), escape_char(x[1]), extension_name(x[2])))

# emit_table(rf, "FOObar", properties)

rf.write(ending)
19 changes: 16 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#![cfg_attr(not(test), no_std)]
#![cfg_attr(feature = "bench", feature(test))]

#[rustfmt::skip]
mod tables;

use core::convert::TryFrom;
Expand All @@ -15,16 +14,30 @@ use tables::{get_script, get_script_extension, NEXT_SCRIPT};
pub use tables::{Script, UNICODE_VERSION};

impl Script {
/// Get the full name of a script
/// Get the full name of a script.
pub fn full_name(self) -> &'static str {
self.inner_full_name()
}

/// Get the four-character short name of a script
/// Attempts to parse script name from the provided string.
/// Returns `None` if the provided string does not represent a valid
/// script full name.
pub fn from_full_name(input: &str) -> Option<Self> {
Self::inner_from_full_name(input)
}

/// Get the four-character short name of a script.
pub fn short_name(self) -> &'static str {
self.inner_short_name()
}

/// Attempts to parse script name from the provided string.
/// Returns `None` if the provided string does not represent a valid
/// script four-character short name.
pub fn from_short_name(input: &str) -> Option<Self> {
Self::inner_from_short_name(input)
}

/// Is this script "Recommended" according to
/// [UAX #31](www.unicode.org/reports/tr31/#Table_Recommended_Scripts)?
pub fn is_recommended(self) -> bool {
Expand Down
Loading