forked from starwing/luautf8
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.lua
220 lines (185 loc) · 6.5 KB
/
test.lua
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
local utf8 = require 'lua-utf8'
local unpack = unpack or table.unpack
local E = utf8.escape
local function get_codes(s)
return table.concat({utf8.byte(s, 1, -1)}, ' ')
end
local t = { 20985, 20984, 26364, 25171, 23567, 24618, 20861 }
-- test escape & len
assert(get_codes(E"%123%xabc%x{ABC}%d%u{456}") == '123 2748 2748 100 456')
local s = E('%'..table.concat(t, '%'))
assert(utf8.len(s) == 7)
assert(get_codes(s) == table.concat(t, ' '))
-- test offset
local function assert_error(f, msg)
local s,e = pcall(f)
return assert(not s and e:match(msg))
end
assert(utf8.offset("中国", 0) == 1)
assert(utf8.offset("中国", 0,1) == 1)
assert(utf8.offset("中国", 0,2) == 1)
assert(utf8.offset("中国", 0,3) == 1)
assert(utf8.offset("中国", 0,4) == 4)
assert(utf8.offset("中国", 0,5) == 4)
assert(utf8.offset("中国", 1) == 1)
assert_error(function() utf8.offset("中国", 1,2) end,
"initial position is a continuation byte")
assert(utf8.offset("中国", 2) == 4)
assert(utf8.offset("中国", 3) == 7)
assert(utf8.offset("中国", 4) == nil)
assert(utf8.offset("中国", -1,-3) == 1)
assert(utf8.offset("中国", -1,1) == nil)
-- test byte
local function assert_table_equal(t1, t2, i, j)
i = i or 1
j = j or #t2
local len = j-i+1
assert(#t1 == len)
for cur = 1, len do
assert(t1[cur] == t2[cur+i-1])
end
end
assert_table_equal({utf8.byte(s, 2)}, t, 2, 2)
assert_table_equal({utf8.byte(s, 1, -1)}, t)
assert_table_equal({utf8.byte(s, -100)}, {})
assert_table_equal({utf8.byte(s, -100, -200)}, {})
assert_table_equal({utf8.byte(s, -200, -100)}, {})
assert_table_equal({utf8.byte(s, 100)}, {})
assert_table_equal({utf8.byte(s, 100, 200)}, {})
assert_table_equal({utf8.byte(s, 200, 100)}, {})
-- test char
assert(s == utf8.char(unpack(t)))
-- test range
for i = 1, #t do
assert(utf8.byte(s, i) == t[i])
end
-- test sub
assert(get_codes(utf8.sub(s, 2, -2)) == table.concat(t, ' ', 2, #t-1))
assert(get_codes(utf8.sub(s, -100)) == table.concat(t, ' '))
assert(get_codes(utf8.sub(s, -100, -200)) == "")
assert(get_codes(utf8.sub(s, -100, -100)) == "")
assert(get_codes(utf8.sub(s, -100, 0)) == "")
assert(get_codes(utf8.sub(s, -200, -100)) == "")
assert(get_codes(utf8.sub(s, 100, 200)) == "")
assert(get_codes(utf8.sub(s, 200, 100)) == "")
-- test insert/remove
assert(utf8.insert("abcdef", "...") == "abcdef...")
assert(utf8.insert("abcdef", 0, "...") == "abcdef...")
assert(utf8.insert("abcdef", 1, "...") == "...abcdef")
assert(utf8.insert("abcdef", 6, "...") == "abcde...f")
assert(utf8.insert("abcdef", 7, "...") == "abcdef...")
assert(utf8.insert("abcdef", 3, "...") == "ab...cdef")
assert(utf8.insert("abcdef", -3, "...") == "abc...def")
assert(utf8.remove("abcdef", 3, 3) == "abdef")
assert(utf8.remove("abcdef", 3, 4) == "abef")
assert(utf8.remove("abcdef", 4, 3) == "abcdef")
assert(utf8.remove("abcdef", -3, -3) == "abcef")
assert(utf8.remove("abcdef", 100) == "abcdef")
assert(utf8.remove("abcdef", -100) == "")
assert(utf8.remove("abcdef", -100, 0) == "abcdef")
assert(utf8.remove("abcdef", -100, -200) == "abcdef")
assert(utf8.remove("abcdef", -200, -100) == "abcdef")
assert(utf8.remove("abcdef", 100, 200) == "abcdef")
assert(utf8.remove("abcdef", 200, 100) == "abcdef")
do
local s = E"a%255bc"
assert(utf8.len(s, 4))
assert(string.len(s, 6))
assert(utf8.charpos(s) == 1)
assert(utf8.charpos(s, 0) == 1)
assert(utf8.charpos(s, 1) == 1)
assert(utf8.charpos(s, 2) == 2)
assert(utf8.charpos(s, 3) == 4)
assert(utf8.charpos(s, 4) == 5)
assert(utf8.charpos(s, 5) == nil)
assert(utf8.charpos(s, 6) == nil)
assert(utf8.charpos(s, -1) == 5)
assert(utf8.charpos(s, -2) == 4)
assert(utf8.charpos(s, -3) == 2)
assert(utf8.charpos(s, -4) == 1)
assert(utf8.charpos(s, -5) == nil)
assert(utf8.charpos(s, -6) == nil)
assert(utf8.charpos(s, 3, -1) == 2)
assert(utf8.charpos(s, 3, 0) == 2)
assert(utf8.charpos(s, 3, 1) == 4)
assert(utf8.charpos(s, 6, -3) == 2)
assert(utf8.charpos(s, 6, -4) == 1)
assert(utf8.charpos(s, 6, -5) == nil)
end
local idx = 1
for pos, code in utf8.next, s do
assert(t[idx] == code)
idx = idx + 1
end
assert(utf8.ncasecmp("abc", "AbC") == 0)
assert(utf8.ncasecmp("abc", "AbE") == -1)
assert(utf8.ncasecmp("abe", "AbC") == 1)
assert(utf8.ncasecmp("abc", "abcdef") == -1)
assert(utf8.ncasecmp("abcdef", "abc") == 1)
assert(utf8.ncasecmp("abZdef", "abcZef") == 1)
assert(utf8.gsub("x^[]+$", "%p", "%%%0") == "x%^%[%]%+%$")
-- test invalid
-- 1110-1010 10-000000 0110-0001
do
local s = "\234\128\97"
assert(utf8.len(s, nil, nil, true) == 2)
assert_table_equal({utf8.len(s)}, {nil, 1}, 1, 2)
-- 1111-0000 10-000000 10-000000 ...
s = "\240\128\128\128\128"
assert_table_equal({utf8.len(s)}, {nil, 1}, 1, 2)
end
-- test compose
local function assert_fail(f, patt)
local ok, msg = pcall(f)
assert(not ok)
assert(msg:match(patt), msg)
end
do
local s = "नमस्ते"
assert(utf8.len(s) == 6)
assert(utf8.reverse(s) == "तेस्मन")
assert(utf8.reverse(s.." ", true) == " ेत्समन")
assert(utf8.match(s..'\2', "%g+") == s)
assert_fail(function() utf8.reverse(E"%xD800") end, "invalid UTF%-8 code")
end
-- test codepoint
for i = 1, 1000 do
assert(utf8.codepoint(E("%"..i)) == i)
end
assert_fail(function() utf8.codepoint(E"%xD800") end, "invalid UTF%-8 code")
-- test escape
assert_fail(function() E"%{1a1}" end, "invalid escape 'a'")
-- test codes
local result = { [1] = 20985; [4] = 20984; [7] = 26364;
[10] = 25171; [13] = 23567; [16] = 24618; [19] = 20861; }
for p, c in utf8.codes(s) do
assert(result[p] == c)
end
for p, c in utf8.codes(s, true) do
assert(result[p] == c)
end
assert_fail(function()
for p, c in utf8.codes(E"%xD800") do
assert(result[p] == c)
end
end, "invalid UTF%-8 code")
-- test width
assert(utf8.width('नमस्ते\2') == 5)
assert(utf8.width(E'%xA1') == 1)
assert(utf8.width(E'%xA1', 2) == 2)
assert(utf8.width(E'%x61C') == 0)
assert(utf8.width "A" == 1)
assert(utf8.width "A" == 2)
assert(utf8.width(97) == 1)
assert(utf8.width(65313) == 2)
assert_fail(function() utf8.width(true) end, "number/string expected, got boolean")
assert(utf8.widthindex("abcdef", 3) == 3)
assert(utf8.widthindex("abcdef", 7) == 7)
-- test patterns
assert_fail(function() utf8.gsub("a", ".", function() return {} end) end,
"invalid replacement value %(a table%)")
assert_fail(function() utf8.gsub("a", ".", "%z") end,
"invalid use of '%%' in replacement string")
assert(utf8.find("abcabc", "ab", -10) == 1)
print "OK"
-- cc: run='lua -- $input'