-
Notifications
You must be signed in to change notification settings - Fork 836
Expand file tree
/
Copy pathutf8.lua
More file actions
377 lines (262 loc) · 7.45 KB
/
utf8.lua
File metadata and controls
377 lines (262 loc) · 7.45 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
local bit = bit
local error = error
local ipairs = ipairs
local string = string
local table = table
local unpack = unpack
local math = math
module( "utf8" )
--
-- Pattern that can be used with the string library to match a single UTF-8 byte-sequence.
-- This expects the string to contain valid UTF-8 data.
--
charpattern = "[%z\x01-\x7F\xC2-\xF4][\x80-\xBF]*"
--
-- Transforms indexes of a string to be positive.
-- Negative indices will wrap around like the string library's functions.
--
local function strRelToAbs( str, ... )
local args = { ... }
for k, v in ipairs( args ) do
v = v > 0 and v or math.max( #str + v + 1, 1 )
if v < 1 or v > #str + 1 then
error( "bad index to string (out of range)", 3 )
end
args[ k ] = v
end
return unpack( args )
end
-- Decodes a single UTF-8 byte-sequence from a string, ensuring it is valid
-- Returns the index of the first/last chars of a sequence and its codepoint
--
local function decode( str, startPos )
startPos = strRelToAbs( str, startPos or 1 )
local b1 = str:byte( startPos, startPos )
-- End of string
if not b1 then
return nil
end
-- Single-byte sequence
if b1 < 0x80 then
return startPos, startPos, b1
end
-- Validate first byte of multi-byte sequence
if b1 > 0xF4 or b1 < 0xC2 then
return nil
end
-- Get 'supposed' amount of continuation bytes from primary byte
local contByteCount = b1 >= 0xF0 and 3 or
b1 >= 0xE0 and 2 or
b1 >= 0xC0 and 1
local endPos = startPos + contByteCount
local codePoint = 0
-- The string doesn't have enough data for this many continutation bytes
if #str < endPos then
return nil
end
-- Validate our continuation bytes
for _, bX in ipairs { str:byte( startPos + 1, endPos ) } do
-- Invalid continuation byte hit
if bit.band( bX, 0xC0 ) ~= 0x80 then
return nil
end
codePoint = bit.bor( bit.lshift( codePoint, 6 ), bit.band( bX, 0x3F ) )
b1 = bit.lshift( b1, 1 )
end
codePoint = bit.bor( codePoint, bit.lshift( bit.band( b1, 0x7F ), contByteCount * 5 ) )
return startPos, endPos, codePoint
end
--
-- Takes zero or more integers and returns a string containing the UTF-8 representation of each
--
function char( ... )
local buf = {}
for k, v in ipairs { ... } do
if v < 0 or v > 0x10FFFF then
error( "bad argument #" .. k .. " to char (out of range)", 2 )
end
local b1, b2, b3, b4 = nil, nil, nil, nil
if v < 0x80 then -- Single-byte sequence
table.insert( buf, string.char( v ) )
elseif v < 0x800 then -- Two-byte sequence
b1 = bit.bor( 0xC0, bit.band( bit.rshift( v, 6 ), 0x1F ) )
b2 = bit.bor( 0x80, bit.band( v, 0x3F ) )
table.insert( buf, string.char( b1, b2 ) )
elseif v < 0x10000 then -- Three-byte sequence
b1 = bit.bor( 0xE0, bit.band( bit.rshift( v, 12 ), 0x0F ) )
b2 = bit.bor( 0x80, bit.band( bit.rshift( v, 6 ), 0x3F ) )
b3 = bit.bor( 0x80, bit.band( v, 0x3F ) )
table.insert( buf, string.char( b1, b2, b3 ) )
else -- Four-byte sequence
b1 = bit.bor( 0xF0, bit.band( bit.rshift( v, 18 ), 0x07 ) )
b2 = bit.bor( 0x80, bit.band( bit.rshift( v, 12 ), 0x3F ) )
b3 = bit.bor( 0x80, bit.band( bit.rshift( v, 6 ), 0x3F ) )
b4 = bit.bor( 0x80, bit.band( v, 0x3F ) )
table.insert( buf, string.char( b1, b2, b3, b4 ) )
end
end
return table.concat( buf, "" )
end
--
-- Iterates over a UTF-8 string similarly to pairs
-- k = index of sequence, v = string value of sequence
--
function codes( str )
local i = 1
return function()
-- Have we hit the end of the iteration set?
if i > #str then
return nil
end
local startPos, endPos, codePoint = decode( str, i )
if not startPos then
error( "invalid UTF-8 code", 2 )
end
i = endPos + 1
return startPos, codePoint
end
end
--
-- Returns an integer-representation of the UTF-8 sequence(s) in a string
-- startPos defaults to 1, endPos defaults to startPos
--
function codepoint( str, startPos, endPos )
startPos, endPos = strRelToAbs( str, startPos or 1, endPos or startPos or 1 )
local ret = {}
repeat
local seqStartPos, seqEndPos, codePoint = decode( str, startPos )
if not seqStartPos then
error( "invalid UTF-8 code", 2 )
end
-- Increment current string index
startPos = seqEndPos + 1
table.insert( ret, codePoint )
until seqEndPos >= endPos
return unpack( ret )
end
--
-- Returns the length of a UTF-8 string. false, index is returned if an invalid sequence is hit
-- startPos defaults to 1, endPos defaults to -1
--
function len( str, startPos, endPos )
startPos, endPos = strRelToAbs( str, startPos or 1, endPos or -1 )
local len = 0
while endPos >= startPos and startPos <= #str do
local seqStartPos, seqEndPos = decode( str, startPos )
-- Hit an invalid sequence?
if not seqStartPos then
return false, startPos
end
-- Increment current string pointer
startPos = seqEndPos + 1
-- Increment length
len = len + 1
end
return len
end
--
-- Returns the byte-index of the n'th UTF-8-character after the given byte-index (nil if none)
-- startPos defaults to 1 when n is positive and -1 when n is negative
-- If 0 is zero, this function instead returns the byte-index of the UTF-8-character startPos lies within.
--
function offset( str, n, startPos )
if startPos and ( startPos > #str or -startPos > #str or startPos == 0 ) then
error( "bad index to string (out of range)", 2 )
end
local pos = ( n >= 0 ) and 1 or #str
pos = strRelToAbs( str, startPos or pos )
-- Back up to the start of this byte sequence
if n == 0 then
while pos > 0 and not decode( str, pos ) do
pos = pos - 1
end
return pos
end
--
-- Make sure we're on a valid sequence
--
if not decode( str, pos ) then
error( "initial position is a continuation byte", 2 )
end
-- Back up to (-n) byte sequences
if n < 0 then
for i = 1, -n do
pos = pos - 1
while pos > 0 and not decode( str, pos ) do
pos = pos - 1
end
end
if pos < 1 then
return nil
end
return pos
end
-- Jump forward (n) byte sequences
if n > 0 then
for i = 1, n do
pos = pos + 1
while pos <= #str and not decode( str, pos ) do
pos = pos + 1
end
end
if pos > #str then
return nil
end
return pos
end
end
--
-- Forces a string to contain only valid UTF-8 data.
-- Invalid sequences are replaced with U+FFFD.
--
function force( str )
local buf = {}
local curPos, endPos = 1, #str
-- Empty string?
if endPos == 0 then
return str
end
repeat
local seqStartPos, seqEndPos = decode( str, curPos )
if not seqStartPos then
table.insert( buf, char( 0xFFFD ) )
curPos = curPos + 1
else
table.insert( buf, str:sub( seqStartPos, seqEndPos ) )
curPos = seqEndPos + 1
end
until curPos > endPos
return table.concat( buf, "" )
end
--
-- Converts a relative index to an absolute
-- This is different from the above in that it cares about characters and not bytes
--
local function strRelToAbsChar( str, pos )
if pos < 0 then
pos = math.max( pos + len( str ) + 1, 0 )
end
return pos
end
--
-- UTF-8 compilant version of str[idx]
--
function GetChar( str, idx )
idx = strRelToAbsChar( str, idx )
if idx == 0 then return "" end
if idx > len( str ) then return "" end
local off = offset( str, idx - 1 )
return char( codepoint( str, off ) )
end
--
-- UTF-8 compilant version of string.sub
--
function sub( str, charstart, charend )
charstart = strRelToAbsChar( str, charstart )
charend = strRelToAbsChar( str, charend or -1 )
local buf = {}
for i = charstart, charend do
buf[#buf + 1] = GetChar( str, i )
end
return table.concat( buf )
end