File: sse_convert_utf32_to_utf16.py

package info (click to toggle)
simdutf 7.7.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,244 kB
  • sloc: cpp: 60,074; ansic: 14,226; python: 3,364; sh: 321; makefile: 12
file content (105 lines) | stat: -rw-r--r-- 2,354 bytes parent folder | download
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python3

from contextlib import redirect_stdout


def format_array(array):
    result = []
    for value in array:
        if value < 0 or value == 0x80:
            result.append('0x80')
        else:
            result.append(str(value))

    return ', '.join(result)


def assure_array_length(array, size, value=0x80):
    while len(array) < size:
        array.append(value)


def utf32_to_utf16le():
    for mask in range(16):
        src = 0
        arr = []
        for bit in [0x01, 0x02, 0x04, 0x08]:
            if mask & bit:
                arr.append(src + 0)
                arr.append(src + 1)
                arr.append(src + 2)
                arr.append(src + 3)
            else:
                arr.append(src + 0)
                arr.append(src + 1)

            src += 4

        assure_array_length(arr, 16)

        yield arr


def utf32_to_utf16be():
    for mask in range(16):
        src = 0
        arr = []
        for bit in [0x01, 0x02, 0x04, 0x08]:
            if mask & bit:
                arr.append(src + 1)
                arr.append(src + 0)
                arr.append(src + 3)
                arr.append(src + 2)
            else:
                arr.append(src + 1)
                arr.append(src + 0)

            src += 4

        assure_array_length(arr, 16)

        yield arr


CPP_HEADER = """// file generated by scripts/sse_convert_utf32_to_utf16.py
#ifndef SIMDUTF_UTF32_TO_UTF16_TABLES_H
#define SIMDUTF_UTF32_TO_UTF16_TABLES_H

namespace simdutf {
namespace {
namespace tables {
namespace utf32_to_utf16 {
"""

CPP_FOOTER = """} // utf16_to_utf8 namespace
} // tables namespace
} // unnamed namespace
} // namespace simdutf

#endif // SIMDUTF_UTF16_TO_UTF8_TABLES_H
"""


def main():
    with open('utf32_to_utf16_tables.h', 'wt') as f:
        with redirect_stdout(f):
            print(CPP_HEADER)

            print("const uint8_t pack_utf32_to_utf16le[16][16] = {")
            for row in utf32_to_utf16le():
                print("{%s}," % format_array(row))
            print("};")

            print()

            print("const uint8_t pack_utf32_to_utf16be[16][16] = {")
            for row in utf32_to_utf16be():
                print("{%s}," % format_array(row))
            print("};")

            print()
            print(CPP_FOOTER)


if __name__ == '__main__':
    main()