-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunicode_escapes.mjs
More file actions
175 lines (160 loc) · 4.98 KB
/
unicode_escapes.mjs
File metadata and controls
175 lines (160 loc) · 4.98 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { codepoint_to_utf8, make_surrogate_pair, zeropadhex, lookup_codepoint } from './unicode_utils.mjs'
export const LANGUAGE_ESCAPE_FORMATS = {
codepoint_uplushex: {
name: "Hex Code Point",
format_codepoint: function(codepoint) {
// See https://stackoverflow.com/questions/1273693/why-is-u-used-to-designate-a-unicode-code-point.
return "U+" + zeropadhex(codepoint, 4);
}
},
codepoint_decimal: {
name: "Decimal Code Point",
shortname: "Decimal",
format_codepoint: function(codepoint) {
return codepoint;
}
},
utf16: {
name: "UTF-16 Big Endian",
shortname: "UTF-16BE",
format_codepoint: function(codepoint) {
if (codepoint <= 65535)
return zeropadhex(codepoint, 4);
let sp = make_surrogate_pair(codepoint);
return zeropadhex(sp.high, 4)
+ " " + zeropadhex(sp.low, 4);
},
format_text: function(codepoints) {
if (codepoint <= 65535)
return zeropadhex(codepoint, 4);
let sp = make_surrogate_pair(codepoint);
return zeropadhex(sp.high, 4)
+ " " + zeropadhex(sp.low, 4);
}
},
utf8: {
name: "UTF-8",
format_codepoint: function(codepoint) {
return codepoint_to_utf8(codepoint)
.map(b => zeropadhex(b, 2))
.join(" ");
}
},
javascript: {
name: "Javascript String Literal",
shortname: "Javascript",
format_codepoint: function(codepoint) {
if (codepoint <= 127)
return "\\x" + zeropadhex(codepoint, 2);
if (codepoint <= 65535)
return "\\u" + zeropadhex(codepoint, 4);
// ES6+
// Prior to ES6, a surrogate pair was required.
// That's still possible but we don't recommend it.
return "\\u{" + zeropadhex(codepoint, 0) + "}";
}
},
python: {
name: "Python String Literal",
shortname: "Python",
format_codepoint: function(codepoint) {
// There is also a \U{NAME} escape.
if (codepoint <= 127)
return "\\x" + zeropadhex(codepoint, 2);
if (codepoint <= 65535)
return "\\u" + zeropadhex(codepoint, 4);
return "\\U" + zeropadhex(codepoint, 8);
}
},
cpp: {
name: "C/C++/C# String Literal",
shortname: "C/C++/C#",
format_codepoint: function(codepoint) {
// The \x### escape sequence is also possible for
// any code point, but because it accepts an arbitrary
// number of hex digits and is terminated at the first
// non-hex digit, it may not be valid in context, so
// we don't recommend it. This is true for all three
// languages.
//
// For C, this is since C99.
//
// In C++, since C++23, there is also a \N{NAME} escape.
//
// Code points that require \U probably can also be
// encoded as surrogate pairs.
if (codepoint <= 65535)
return "\\u" + zeropadhex(codepoint, 4);
return "\\U" + zeropadhex(codepoint, 8);
}
},
java: {
// https://docs.oracle.com/javase/specs/jls/se12/html/jls-3.html
name: "Java String Literal",
shortname: "Java",
format_codepoint: function(codepoint) {
if (codepoint <= 65535)
return "\\u" + zeropadhex(codepoint, 4);
let sp = make_surrogate_pair(codepoint);
return "\\u" + zeropadhex(sp.high, 4)
+ "\\u" + zeropadhex(sp.low, 4);
}
},
swift: {
// https://docs.swift.org/swift-book/documentation/the-swift-programming-language/stringsandcharacters/
name: "Swift String Literal",
shortname: "Swift",
format_codepoint: function(codepoint) {
return "\\u{" + zeropadhex(codepoint, 0) + "}";
}
},
php: {
// https://www.php.net/manual/en/language.types.string.php
name: "PHP String Literal",
shortname: "PHP",
format_codepoint: function(codepoint) {
if (codepoint <= 127)
return "\\x" + zeropadhex(codepoint, 2);
return "\\u{" + zeropadhex(codepoint, 4) + "}";
}
},
html_named_entity: {
name: "HTML Named Entity",
format_codepoint: function(codepoint) {
let codePointInfo = lookup_codepoint(codepoint);
if (codePointInfo && 'html5_entity' in codePointInfo)
return codePointInfo.html5_entity;
// Fall back to hex entities.
return "&#x" + zeropadhex(codepoint, 0) + ";";
}
},
xml_entity_hex: {
name: "HTML/XML Hex Entity",
format_codepoint: function(codepoint) {
return "&#x" + zeropadhex(codepoint, 0) + ";";
}
},
xml_entity_dec: {
name: "HTML/XML Decimal Entity",
format_codepoint: function(codepoint) {
return "&#" + codepoint + ";";
}
},
};
export function create_escape(codepoint, language)
{
return LANGUAGE_ESCAPE_FORMATS[language](codepoint);
}
export function create_escapes(codepoint, languages)
{
let escapes = { };
Object.keys(LANGUAGE_ESCAPE_FORMATS)
.map(language => {
escapes[language] = {
key: language,
name: LANGUAGE_ESCAPE_FORMATS[language].name,
escape: LANGUAGE_ESCAPE_FORMATS[language].format_codepoint(codepoint)
}
});
return escapes;
}