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

Commitd99f11a

Browse files
committed
Add Script::from_full_name and Script::from_short_name
1 parent8cd55c9 commitd99f11a

File tree

3 files changed

+500
-85
lines changed

3 files changed

+500
-85
lines changed

‎scripts/unicode.py

Lines changed: 69 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+
defgenerate_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+
""")
254278

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+
forscriptinscript_list:
289+
f.write("\"%s\" => Some(Script::%s),\n"% (longforms[script],longforms[script]))
290+
f.write(" _ => None,\n" )
291+
f.write(""" }
292+
}
293+
""")
294+
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,26 @@ 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+
"" => Some(Script::Unknown),
316+
"Zyyy" => Some(Script::Common),
317+
"Zinh" => Some(Script::Inherited),
318+
""")
319+
forscriptinscript_list:
320+
f.write("\"%s\" => Some(Script::%s),\n"% (script,longforms[script]))
321+
f.write(""" _ => None,\n""")
322+
f.write(""" }
323+
}
324+
""")
325+
326+
# Generate impl of `for_integer`
327+
f.write("""
267328
#[inline]
268329
pub(crate) fn for_integer(value: u8) -> Self {
269330
match value {
@@ -273,6 +334,10 @@ def emit_enums(f, script_list, extension_list, longforms):
273334
f.write(""" _ => unreachable!(),
274335
}
275336
}
337+
""")
338+
339+
# Close `impl Script` section
340+
f.write("""
276341
}
277342
""")
278343

@@ -281,8 +346,6 @@ def extension_name(ext):
281346
return"script_extensions::%s"%"_".join([e.upper()foreinext])
282347

283348

284-
285-
286349
if__name__=="__main__":
287350
r="tables.rs"
288351
ifos.path.exists(r):
@@ -336,3 +399,5 @@ def extension_name(ext):
336399
is_pub=False ,pfun=lambdax:"(%s,%s,%s)"% (escape_char(x[0]),escape_char(x[1]),extension_name(x[2])))
337400

338401
# emit_table(rf, "FOObar", properties)
402+
403+
rf.write(ending)

‎src/lib.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,30 @@ use tables::{get_script, get_script_extension, NEXT_SCRIPT};
1515
pubuse tables::{Script,UNICODE_VERSION};
1616

1717
implScript{
18-
/// Get the full name of a script
18+
/// Get the full name of a script.
1919
pubfnfull_name(self) ->&'staticstr{
2020
self.inner_full_name()
2121
}
2222

23-
/// Get the four-character short name of a script
23+
/// Attempts to parse script name from the provided string.
24+
/// Returns `None` if the provided string does not represent a valid
25+
/// script full name.
26+
pubfnfrom_full_name(input:&str) ->Option<Self>{
27+
Self::inner_from_full_name(input)
28+
}
29+
30+
/// Get the four-character short name of a script.
2431
pubfnshort_name(self) ->&'staticstr{
2532
self.inner_short_name()
2633
}
2734

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp