Skip to content

Commit b0cda40

Browse files
authored
Merge pull request #7 from popzxc/parser-functions
Add function to parse script from string
2 parents 95d0115 + d2c5a10 commit b0cda40

File tree

3 files changed

+498
-86
lines changed

3 files changed

+498
-86
lines changed

scripts/unicode.py

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,16 @@
3434
3535
#![allow(missing_docs, non_upper_case_globals, non_snake_case)]
3636
37-
use super::ScriptExtension;
37+
pub use tables_impl::*;
38+
39+
#[rustfmt::skip]
40+
mod tables_impl {
41+
use crate::ScriptExtension;
42+
'''
43+
44+
# Close `mod impl {`
45+
ending='''
46+
}
3847
'''
3948

4049
UNICODE_VERSION = (13, 0, 0)
@@ -239,7 +248,21 @@ def emit_enums(f, script_list, extension_list, longforms):
239248
f.write(" /// %s\n pub const %s: ScriptExtension = %s;\n" % (longform, name, expr))
240249
f.write("""}
241250
242-
impl Script {
251+
""")
252+
253+
# Generate implementation for the `Script`
254+
generate_script_impl(f)
255+
256+
257+
def generate_script_impl(f):
258+
"""Generates an `impl Script { ... }` section with all the required functions"""
259+
260+
# Open `impl Script` section.
261+
f.write("""impl Script {
262+
""")
263+
264+
# Generate impl of `inner_full_name`.
265+
f.write("""
243266
#[inline]
244267
pub(crate) fn inner_full_name(self) -> &'static str {
245268
match self {
@@ -251,7 +274,26 @@ def emit_enums(f, script_list, extension_list, longforms):
251274
f.write(" Script::%s => \"%s\",\n" % (longforms[script], longforms[script]))
252275
f.write(""" }
253276
}
277+
""")
278+
279+
# Generate impl of `inner_from_full_name`.
280+
f.write("""
281+
#[inline]
282+
pub(crate) fn inner_from_full_name(input: &str) -> Option<Self> {
283+
match input {
284+
"Unknown" => Some(Script::Unknown),
285+
"Common" => Some(Script::Common),
286+
"Inherited" => Some(Script::Inherited),
287+
""")
288+
for script in script_list:
289+
f.write(" \"%s\" => Some(Script::%s),\n" % (longforms[script], longforms[script]))
290+
f.write(" _ => None,\n" )
291+
f.write(""" }
292+
}
293+
""")
254294

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

310+
# Generate impl of `inner_from_short_name`
311+
f.write("""
312+
#[inline]
313+
pub(crate) fn inner_from_short_name(input: &str) -> Option<Self> {
314+
match input {
315+
"Zyyy" => Some(Script::Common),
316+
"Zinh" => Some(Script::Inherited),
317+
""")
318+
for script in script_list:
319+
f.write(" \"%s\" => Some(Script::%s),\n" % (script, longforms[script]))
320+
f.write(""" _ => None,\n""")
321+
f.write(""" }
322+
}
323+
""")
324+
325+
# Generate impl of `for_integer`
326+
f.write("""
267327
#[inline]
268328
pub(crate) fn for_integer(value: u8) -> Self {
269329
match value {
@@ -273,6 +333,10 @@ def emit_enums(f, script_list, extension_list, longforms):
273333
f.write(""" _ => unreachable!(),
274334
}
275335
}
336+
""")
337+
338+
# Close `impl Script` section
339+
f.write("""
276340
}
277341
""")
278342

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

283347

284-
285-
286348
if __name__ == "__main__":
287349
r = "tables.rs"
288350
if os.path.exists(r):
@@ -336,3 +398,5 @@ def extension_name(ext):
336398
is_pub=False , pfun=lambda x: "(%s,%s,%s)" % (escape_char(x[0]), escape_char(x[1]), extension_name(x[2])))
337399

338400
# emit_table(rf, "FOObar", properties)
401+
402+
rf.write(ending)

src/lib.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#![cfg_attr(not(test), no_std)]
55
#![cfg_attr(feature = "bench", feature(test))]
66

7-
#[rustfmt::skip]
87
mod tables;
98

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

1716
impl Script {
18-
/// Get the full name of a script
17+
/// Get the full name of a script.
1918
pub fn full_name(self) -> &'static str {
2019
self.inner_full_name()
2120
}
2221

23-
/// Get the four-character short name of a script
22+
/// Attempts to parse script name from the provided string.
23+
/// Returns `None` if the provided string does not represent a valid
24+
/// script full name.
25+
pub fn from_full_name(input: &str) -> Option<Self> {
26+
Self::inner_from_full_name(input)
27+
}
28+
29+
/// Get the four-character short name of a script.
2430
pub fn short_name(self) -> &'static str {
2531
self.inner_short_name()
2632
}
2733

34+
/// Attempts to parse script name from the provided string.
35+
/// Returns `None` if the provided string does not represent a valid
36+
/// script four-character short name.
37+
pub fn from_short_name(input: &str) -> Option<Self> {
38+
Self::inner_from_short_name(input)
39+
}
40+
2841
/// Is this script "Recommended" according to
2942
/// [UAX #31](www.unicode.org/reports/tr31/#Table_Recommended_Scripts)?
3043
pub fn is_recommended(self) -> bool {

0 commit comments

Comments
 (0)
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