raspberryGPIOhtmlserver/node_modules/utf8/tests/generate-test-data.py
node_modules/utf8/tests/generate-test-data.pyRaw
1#!/usr/bin/env python
2
3import re
4import json
5
6# http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
7# http://stackoverflow.com/a/13436167/96656
8def unisymbol(codePoint):
9 if codePoint >= 0x0000 and codePoint <= 0xFFFF:
10 return unichr(codePoint)
11 elif codePoint >= 0x010000 and codePoint <= 0x10FFFF:
12 highSurrogate = int((codePoint - 0x10000) / 0x400) + 0xD800
13 lowSurrogate = int((codePoint - 0x10000) % 0x400) + 0xDC00
14 return unichr(highSurrogate) + unichr(lowSurrogate)
15 else:
16 return 'Error'
17
18def hexify(codePoint):
19 return 'U+' + hex(codePoint)[2:].upper().zfill(6)
20
21def writeFile(filename, contents):
22 print filename
23 with open(filename, 'w') as f:
24 f.write(contents.strip() + '\n')
25
26data = []
27for codePoint in range(0x000000, 0x10FFFF + 1):
28 symbol = unisymbol(codePoint)
29 # http://stackoverflow.com/a/17199950/96656
30 bytes = symbol.encode('utf8').decode('latin1')
31 data.append({
32 'codePoint': codePoint,
33 'decoded': symbol,
34 'encoded': bytes
35 });
36
37jsonData = json.dumps(data, sort_keys=False, indent=2, separators=(',', ': '))
38# Use tabs instead of double spaces for indentation
39jsonData = jsonData.replace(' ', '\t')
40# Escape hexadecimal digits in escape sequences
41jsonData = re.sub(
42 r'\\u([a-fA-F0-9]{4})',
43 lambda match: r'\u{}'.format(match.group(1).upper()),
44 jsonData
45)
46
47writeFile('data.json', jsonData)