summaryrefslogtreecommitdiffstats
path: root/js/src/tests/ecma_6/String/make-normalize-generateddata-input.py
blob: c1983dbd18c0285f12a88e8b08c9a67255f59cd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/python -B

""" Usage: make-normalize-generateddata-input.py PATH_TO_MOZILLA_CENTRAL

    This script generates test input data for String.prototype.normalize
    from intl/icu/source/data/unidata/NormalizationTest.txt
    to js/src/tests/ecma_6/String/normalize-generateddata-input.js
"""

from __future__ import print_function
import re, sys

sep_pat = re.compile(' +')
def to_code_list(codes):
    return '[' + ', '.join(map(lambda x: '0x{0}'.format(x), re.split(sep_pat, codes))) + ']'

def convert(dir):
    ver_pat = re.compile('NormalizationTest-([0-9\.]+)\.txt')
    part_pat = re.compile('^@(Part([0-9]+) .+)$')
    test_pat = re.compile('^([0-9A-Fa-f ]+);([0-9A-Fa-f ]+);([0-9A-Fa-f ]+);([0-9A-Fa-f ]+);([0-9A-Fa-f ]+);$')
    ignore_pat = re.compile('^#|^$')
    js_path = 'js/src/tests/ecma_6/String/normalize-generateddata-input.js'
    txt_path = 'intl/icu/source/data/unidata/NormalizationTest.txt'

    part_opened = False
    not_empty = False
    with open('{dir}/{path}'.format(dir=dir, path=txt_path), 'r') as f:
        with open('{dir}/{path}'.format(dir=dir, path=js_path), 'w') as outf:
            for line in f:
                m = test_pat.search(line)
                if m:
                    if not_empty:
                        outf.write(',')
                    outf.write('\n')
                    pat = '{{ source: {source}, NFC: {NFC}, NFD: {NFD}, NFKC: {NFKC}, NFKD: {NFKD} }}'
                    outf.write(pat.format(source=to_code_list(m.group(1)),
                                          NFC=to_code_list(m.group(2)),
                                          NFD=to_code_list(m.group(3)),
                                          NFKC=to_code_list(m.group(4)),
                                          NFKD=to_code_list(m.group(5))))
                    not_empty = True
                    continue
                m = part_pat.search(line)
                if m:
                    desc = m.group(1)
                    part = m.group(2)
                    if part_opened:
                        outf.write('\n];\n')
                    outf.write('/* {desc} */\n'.format(desc=desc))
                    outf.write('var tests_part{part} = ['.format(part=part))
                    part_opened = True
                    not_empty = False
                    continue
                m = ver_pat.search(line)
                if m:
                    ver = m.group(1)
                    outf.write('/* created from NormalizationTest-{ver}.txt */\n'.format(ver=ver))
                    continue
                m = ignore_pat.search(line)
                if m:
                    continue
                print("Unknown line: {0}".format(line), file=sys.stderr)
            if part_opened:
                outf.write('\n];\n')

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print("Usage: make-normalize-generateddata-input.py PATH_TO_MOZILLA_CENTRAL", file=sys.stderr)
        sys.exit(1)
    convert(sys.argv[1])