Skip to content

Add function to parse script from string #7

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

Merged
merged 4 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
73 changes: 69 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,26 @@ 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 {
"" => Some(Script::Unknown),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this branch exist? Perhaps we should not return an Option and use Unknown as the catch all

Copy link
Contributor Author

@popzxc popzxc Jun 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Option may be helpful, because if some folk will accidentally use from_short_name instead of from_full_namethey'll get a None, which is easier to find early.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, I'm not insisting, if you think that it's better to not use Option here, I'll be happy to change the code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay returning an Option but we shouldn't have the empty branch imo

"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 +334,10 @@ def emit_enums(f, script_list, extension_list, longforms):
f.write(""" _ => unreachable!(),
}
}
""")

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

Expand All @@ -281,8 +346,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 +399,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
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