michael@0: #!/usr/bin/python -B michael@0: michael@0: """ Usage: make-normalize-generateddata-input.py PATH_TO_MOZILLA_CENTRAL michael@0: michael@0: This script generates test input data for String.prototype.normalize michael@0: from intl/icu/source/data/unidata/NormalizationTest.txt michael@0: to js/src/tests/ecma_6/String/normalize-generateddata-input.js michael@0: """ michael@0: michael@0: from __future__ import print_function michael@0: import re, sys michael@0: michael@0: sep_pat = re.compile(' +') michael@0: def to_code_list(codes): michael@0: return '[' + ', '.join(map(lambda x: '0x{0}'.format(x), re.split(sep_pat, codes))) + ']' michael@0: michael@0: def convert(dir): michael@0: ver_pat = re.compile('NormalizationTest-([0-9\.]+)\.txt') michael@0: part_pat = re.compile('^@(Part([0-9]+) .+)$') michael@0: test_pat = re.compile('^([0-9A-Fa-f ]+);([0-9A-Fa-f ]+);([0-9A-Fa-f ]+);([0-9A-Fa-f ]+);([0-9A-Fa-f ]+);$') michael@0: ignore_pat = re.compile('^#|^$') michael@0: js_path = 'js/src/tests/ecma_6/String/normalize-generateddata-input.js' michael@0: txt_path = 'intl/icu/source/data/unidata/NormalizationTest.txt' michael@0: michael@0: part_opened = False michael@0: not_empty = False michael@0: with open('{dir}/{path}'.format(dir=dir, path=txt_path), 'r') as f: michael@0: with open('{dir}/{path}'.format(dir=dir, path=js_path), 'w') as outf: michael@0: for line in f: michael@0: m = test_pat.search(line) michael@0: if m: michael@0: if not_empty: michael@0: outf.write(',') michael@0: outf.write('\n') michael@0: pat = '{{ source: {source}, NFC: {NFC}, NFD: {NFD}, NFKC: {NFKC}, NFKD: {NFKD} }}' michael@0: outf.write(pat.format(source=to_code_list(m.group(1)), michael@0: NFC=to_code_list(m.group(2)), michael@0: NFD=to_code_list(m.group(3)), michael@0: NFKC=to_code_list(m.group(4)), michael@0: NFKD=to_code_list(m.group(5)))) michael@0: not_empty = True michael@0: continue michael@0: m = part_pat.search(line) michael@0: if m: michael@0: desc = m.group(1) michael@0: part = m.group(2) michael@0: if part_opened: michael@0: outf.write('\n];\n') michael@0: outf.write('/* {desc} */\n'.format(desc=desc)) michael@0: outf.write('var tests_part{part} = ['.format(part=part)) michael@0: part_opened = True michael@0: not_empty = False michael@0: continue michael@0: m = ver_pat.search(line) michael@0: if m: michael@0: ver = m.group(1) michael@0: outf.write('/* created from NormalizationTest-{ver}.txt */\n'.format(ver=ver)) michael@0: continue michael@0: m = ignore_pat.search(line) michael@0: if m: michael@0: continue michael@0: print("Unknown line: {0}".format(line), file=sys.stderr) michael@0: if part_opened: michael@0: outf.write('\n];\n') michael@0: michael@0: if __name__ == '__main__': michael@0: if len(sys.argv) < 2: michael@0: print("Usage: make-normalize-generateddata-input.py PATH_TO_MOZILLA_CENTRAL", file=sys.stderr) michael@0: sys.exit(1) michael@0: convert(sys.argv[1])