Skip to content

Commit bfc4981

Browse files
author
Clar Charr
committed
Remove redundant parameters from functions in unicode.py
1 parent 64787ef commit bfc4981

File tree

1 file changed

+30
-39
lines changed

1 file changed

+30
-39
lines changed

src/libcore/unicode/unicode.py

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# Since this should not require frequent updates, we just store this
2424
# out-of-line and check the unicode.rs file into git.
2525

26-
import fileinput, re, os, sys, operator, math
26+
import re, os, sys, operator, math
2727

2828
preamble = '''// Copyright 2012-2016 The Rust Project Developers. See the COPYRIGHT
2929
// file at the top-level directory of this distribution and at
@@ -69,11 +69,13 @@ def fetch(f):
6969
sys.stderr.write("cannot load %s" % f)
7070
exit(1)
7171

72+
return open(f)
73+
7274
def is_surrogate(n):
7375
return surrogate_codepoints[0] <= n <= surrogate_codepoints[1]
7476

75-
def load_unicode_data(f):
76-
fetch(f)
77+
def load_unicode_data():
78+
uni_data = fetch("UnicodeData.txt")
7779
gencats = {}
7880
to_lower = {}
7981
to_upper = {}
@@ -84,7 +86,7 @@ def load_unicode_data(f):
8486

8587
udict = {}
8688
range_start = -1
87-
for line in fileinput.input(f):
89+
for line in uni_data:
8890
data = line.split(';')
8991
if len(data) != 15:
9092
continue
@@ -154,9 +156,9 @@ def load_unicode_data(f):
154156

155157
return (canon_decomp, compat_decomp, gencats, combines, to_upper, to_lower, to_title)
156158

157-
def load_special_casing(f, to_upper, to_lower, to_title):
158-
fetch(f)
159-
for line in fileinput.input(f):
159+
def load_special_casing(to_upper, to_lower, to_title):
160+
casing_data = fetch("SpecialCasing.txt")
161+
for line in casing_data:
160162
data = line.split('#')[0].split(';')
161163
if len(data) == 5:
162164
code, lower, title, upper, _comment = data
@@ -237,13 +239,13 @@ def format_table_content(f, content, indent):
237239
line = " "*indent + chunk
238240
f.write(line)
239241

240-
def load_properties(f, interestingprops):
241-
fetch(f)
242+
def load_properties(fname, interestingprops):
243+
f = fetch(fname)
242244
props = {}
243245
re1 = re.compile(r"^ *([0-9A-F]+) *; *(\w+)")
244246
re2 = re.compile(r"^ *([0-9A-F]+)\.\.([0-9A-F]+) *; *(\w+)")
245247

246-
for line in fileinput.input(os.path.basename(f)):
248+
for line in f:
247249
prop = None
248250
d_lo = 0
249251
d_hi = 0
@@ -277,19 +279,20 @@ def load_properties(f, interestingprops):
277279
def escape_char(c):
278280
return "'\\u{%x}'" % c if c != 0 else "'\\0'"
279281

280-
def emit_table(f, name, t_data, t_type = "&[(char, char)]", is_pub=True,
281-
pfun=lambda x: "(%s,%s)" % (escape_char(x[0]), escape_char(x[1]))):
282-
pub_string = ""
283-
if is_pub:
284-
pub_string = "pub "
285-
f.write(" %sconst %s: %s = &[\n" % (pub_string, name, t_type))
282+
def emit_table(f, name, t_data):
283+
f.write(" const %s: &[(char, [char; 3])] = &[\n" % (name,))
286284
data = ""
287285
first = True
288286
for dat in t_data:
289287
if not first:
290288
data += ","
291289
first = False
292-
data += pfun(dat)
290+
data += "(%s,[%s,%s,%s])" % (
291+
escape_char(dat[0]),
292+
escape_char(dat[1][0]),
293+
escape_char(dat[1][1]),
294+
escape_char(dat[1][2])
295+
)
293296
format_table_content(f, data, 8)
294297
f.write("\n ];\n\n")
295298

@@ -306,7 +309,7 @@ def compute_trie(rawdata, chunksize):
306309
root.append(childmap[child])
307310
return (root, child_data)
308311

309-
def emit_bool_trie(f, name, t_data, is_pub=True):
312+
def emit_bool_trie(f, name, t_data):
310313
CHUNK = 64
311314
rawdata = [False] * 0x110000
312315
for (lo, hi) in t_data:
@@ -322,10 +325,7 @@ def emit_bool_trie(f, name, t_data, is_pub=True):
322325
chunk |= 1 << j
323326
chunks.append(chunk)
324327

325-
pub_string = ""
326-
if is_pub:
327-
pub_string = "pub "
328-
f.write(" %sconst %s: &super::BoolTrie = &super::BoolTrie {\n" % (pub_string, name))
328+
f.write(" pub const %s: &super::BoolTrie = &super::BoolTrie {\n" % (name,))
329329
f.write(" r1: [\n")
330330
data = ','.join('0x%016x' % chunk for chunk in chunks[0:0x800 // CHUNK])
331331
format_table_content(f, data, 12)
@@ -360,7 +360,7 @@ def emit_bool_trie(f, name, t_data, is_pub=True):
360360

361361
f.write(" };\n\n")
362362

363-
def emit_small_bool_trie(f, name, t_data, is_pub=True):
363+
def emit_small_bool_trie(f, name, t_data):
364364
last_chunk = max(hi // 64 for (lo, hi) in t_data)
365365
n_chunks = last_chunk + 1
366366
chunks = [0] * n_chunks
@@ -370,11 +370,8 @@ def emit_small_bool_trie(f, name, t_data, is_pub=True):
370370
print(cp, cp // 64, len(chunks), lo, hi)
371371
chunks[cp // 64] |= 1 << (cp & 63)
372372

373-
pub_string = ""
374-
if is_pub:
375-
pub_string = "pub "
376-
f.write(" %sconst %s: &super::SmallBoolTrie = &super::SmallBoolTrie {\n"
377-
% (pub_string, name))
373+
f.write(" pub const %s: &super::SmallBoolTrie = &super::SmallBoolTrie {\n"
374+
% (name,))
378375

379376
(r1, r2) = compute_trie(chunks, 1)
380377

@@ -427,15 +424,10 @@ def emit_conversions_module(f, to_upper, to_lower, to_title):
427424
}
428425
429426
""")
430-
t_type = "&[(char, [char; 3])]"
431-
pfun = lambda x: "(%s,[%s,%s,%s])" % (
432-
escape_char(x[0]), escape_char(x[1][0]), escape_char(x[1][1]), escape_char(x[1][2]))
433427
emit_table(f, "to_lowercase_table",
434-
sorted(to_lower.items(), key=operator.itemgetter(0)),
435-
is_pub=False, t_type = t_type, pfun=pfun)
428+
sorted(to_lower.items(), key=operator.itemgetter(0)))
436429
emit_table(f, "to_uppercase_table",
437-
sorted(to_upper.items(), key=operator.itemgetter(0)),
438-
is_pub=False, t_type = t_type, pfun=pfun)
430+
sorted(to_upper.items(), key=operator.itemgetter(0)))
439431
f.write("}\n\n")
440432

441433
def emit_norm_module(f, canon, compat, combine, norm_props):
@@ -464,8 +456,7 @@ def emit_norm_module(f, canon, compat, combine, norm_props):
464456
rf.write(preamble)
465457

466458
# download and parse all the data
467-
fetch("ReadMe.txt")
468-
with open("ReadMe.txt") as readme:
459+
with fetch("ReadMe.txt") as readme:
469460
pattern = r"for Version (\d+)\.(\d+)\.(\d+) of the Unicode"
470461
unicode_version = re.search(pattern, readme.read()).groups()
471462
rf.write("""
@@ -480,8 +471,8 @@ def emit_norm_module(f, canon, compat, combine, norm_props):
480471
};
481472
""" % unicode_version)
482473
(canon_decomp, compat_decomp, gencats, combines,
483-
to_upper, to_lower, to_title) = load_unicode_data("UnicodeData.txt")
484-
load_special_casing("SpecialCasing.txt", to_upper, to_lower, to_title)
474+
to_upper, to_lower, to_title) = load_unicode_data()
475+
load_special_casing(to_upper, to_lower, to_title)
485476
want_derived = ["XID_Start", "XID_Continue", "Alphabetic", "Lowercase", "Uppercase",
486477
"Cased", "Case_Ignorable"]
487478
derived = load_properties("DerivedCoreProperties.txt", want_derived)

0 commit comments

Comments
 (0)