1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_6/String/make-normalize-generateddata-input.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,70 @@ 1.4 +#!/usr/bin/python -B 1.5 + 1.6 +""" Usage: make-normalize-generateddata-input.py PATH_TO_MOZILLA_CENTRAL 1.7 + 1.8 + This script generates test input data for String.prototype.normalize 1.9 + from intl/icu/source/data/unidata/NormalizationTest.txt 1.10 + to js/src/tests/ecma_6/String/normalize-generateddata-input.js 1.11 +""" 1.12 + 1.13 +from __future__ import print_function 1.14 +import re, sys 1.15 + 1.16 +sep_pat = re.compile(' +') 1.17 +def to_code_list(codes): 1.18 + return '[' + ', '.join(map(lambda x: '0x{0}'.format(x), re.split(sep_pat, codes))) + ']' 1.19 + 1.20 +def convert(dir): 1.21 + ver_pat = re.compile('NormalizationTest-([0-9\.]+)\.txt') 1.22 + part_pat = re.compile('^@(Part([0-9]+) .+)$') 1.23 + test_pat = re.compile('^([0-9A-Fa-f ]+);([0-9A-Fa-f ]+);([0-9A-Fa-f ]+);([0-9A-Fa-f ]+);([0-9A-Fa-f ]+);$') 1.24 + ignore_pat = re.compile('^#|^$') 1.25 + js_path = 'js/src/tests/ecma_6/String/normalize-generateddata-input.js' 1.26 + txt_path = 'intl/icu/source/data/unidata/NormalizationTest.txt' 1.27 + 1.28 + part_opened = False 1.29 + not_empty = False 1.30 + with open('{dir}/{path}'.format(dir=dir, path=txt_path), 'r') as f: 1.31 + with open('{dir}/{path}'.format(dir=dir, path=js_path), 'w') as outf: 1.32 + for line in f: 1.33 + m = test_pat.search(line) 1.34 + if m: 1.35 + if not_empty: 1.36 + outf.write(',') 1.37 + outf.write('\n') 1.38 + pat = '{{ source: {source}, NFC: {NFC}, NFD: {NFD}, NFKC: {NFKC}, NFKD: {NFKD} }}' 1.39 + outf.write(pat.format(source=to_code_list(m.group(1)), 1.40 + NFC=to_code_list(m.group(2)), 1.41 + NFD=to_code_list(m.group(3)), 1.42 + NFKC=to_code_list(m.group(4)), 1.43 + NFKD=to_code_list(m.group(5)))) 1.44 + not_empty = True 1.45 + continue 1.46 + m = part_pat.search(line) 1.47 + if m: 1.48 + desc = m.group(1) 1.49 + part = m.group(2) 1.50 + if part_opened: 1.51 + outf.write('\n];\n') 1.52 + outf.write('/* {desc} */\n'.format(desc=desc)) 1.53 + outf.write('var tests_part{part} = ['.format(part=part)) 1.54 + part_opened = True 1.55 + not_empty = False 1.56 + continue 1.57 + m = ver_pat.search(line) 1.58 + if m: 1.59 + ver = m.group(1) 1.60 + outf.write('/* created from NormalizationTest-{ver}.txt */\n'.format(ver=ver)) 1.61 + continue 1.62 + m = ignore_pat.search(line) 1.63 + if m: 1.64 + continue 1.65 + print("Unknown line: {0}".format(line), file=sys.stderr) 1.66 + if part_opened: 1.67 + outf.write('\n];\n') 1.68 + 1.69 +if __name__ == '__main__': 1.70 + if len(sys.argv) < 2: 1.71 + print("Usage: make-normalize-generateddata-input.py PATH_TO_MOZILLA_CENTRAL", file=sys.stderr) 1.72 + sys.exit(1) 1.73 + convert(sys.argv[1])