Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Manishearth merged 4 commits intounicode-rs:masterfrompopzxc:parser-functions
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionsscripts/unicode.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 deletionssrc/lib.rs
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp