Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
lua/testes/utf8.lua at rc · lua/lua · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
lua
lua
Repository navigation
Code
Actions
Security and quality
Insights
More
items
Files
Expand file tree
rc
Breadcrumbs
lua
/
testes
/
utf8.lua
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
280 lines (223 loc) · 7.67 KB
rc
Breadcrumbs
lua
/
testes
/
utf8.lua
Copy path
Top
File metadata and controls
Code
Blame
280 lines (223 loc) · 7.67 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
--
$Id: testes/utf8.lua $
--
See Copyright Notice in file lua.h
--
UTF-8 file
global
<const>
*
print
"
testing UTF-8 library
"
local
utf8
=
require
'
utf8
'
local
function
checkerror
(
msg
,
f
, ...)
local
s
,
err
=
pcall
(
f
,
...
)
assert
(
not
s
and
string.find
(
err
,
msg
))
end
local
function
len
(
s
)
return
#
string.gsub
(
s
,
"
[
\x80
-
\xBF
]
"
,
"
"
)
end
local
justone
=
"
^
"
..
utf8.charpattern
..
"
$
"
--
't' is the list of codepoints of 's'
local
function
checksyntax
(
s
,
t
)
--
creates a string "return '\u{t[1]}...\u{t[n]}'"
local
ts
=
{
"
return '
"
}
for
i
=
1
,
#
t
do
ts
[
i
+
1
]
=
string.format
(
"
\\
u{%x}
"
,
t
[
i
])
end
ts
[
#
t
+
2
]
=
"
'
"
ts
=
table.concat
(
ts
)
--
its execution should result in 's'
assert
(
assert
(
load
(
ts
))()
==
s
)
end
assert
(
not
utf8.offset
(
"
alo
"
,
5
))
assert
(
not
utf8.offset
(
"
alo
"
,
-
4
))
--
'check' makes several tests over the validity of string 's'.
--
't' is the list of codepoints of 's'.
local
function
check
(
s
,
t
,
nonstrict
)
local
l
=
utf8.len
(
s
,
1
,
-
1
,
nonstrict
)
assert
(
#
t
==
l
and
len
(
s
)
==
l
)
assert
(
utf8.char
(
table.unpack
(
t
))
==
s
)
--
't' and 's' are equivalent
assert
(
utf8.offset
(
s
,
0
)
==
1
)
checksyntax
(
s
,
t
)
--
creates new table with all codepoints of 's'
local
t1
=
{
utf8.codepoint
(
s
,
1
,
-
1
,
nonstrict
)}
assert
(
#
t
==
#
t1
)
for
i
=
1
,
#
t
do
assert
(
t
[
i
]
==
t1
[
i
])
end
--
't' is equal to 't1'
for
i
=
1
,
l
do
--
for all codepoints
local
pi
,
pie
=
utf8.offset
(
s
,
i
)
--
position of i-th char
local
pi1
=
utf8.offset
(
s
,
2
,
pi
)
--
position of next char
assert
(
pi1
==
pie
+
1
)
assert
(
string.find
(
string.sub
(
s
,
pi
,
pi1
-
1
),
justone
))
assert
(
utf8.offset
(
s
,
-
1
,
pi1
)
==
pi
)
assert
(
utf8.offset
(
s
,
i
-
l
-
1
)
==
pi
)
assert
(
pi1
-
pi
==
#
utf8.char
(
utf8.codepoint
(
s
,
pi
,
pi
,
nonstrict
)))
for
j
=
pi
,
pi1
-
1
do
local
off1
,
off2
=
utf8.offset
(
s
,
0
,
j
)
assert
(
off1
==
pi
and
off2
==
pi1
-
1
)
end
for
j
=
pi
+
1
,
pi1
-
1
do
assert
(
not
utf8.len
(
s
,
j
))
end
assert
(
utf8.len
(
s
,
pi
,
pi
,
nonstrict
)
==
1
)
assert
(
utf8.len
(
s
,
pi
,
pi1
-
1
,
nonstrict
)
==
1
)
assert
(
utf8.len
(
s
,
pi
,
-
1
,
nonstrict
)
==
l
-
i
+
1
)
assert
(
utf8.len
(
s
,
pi1
,
-
1
,
nonstrict
)
==
l
-
i
)
assert
(
utf8.len
(
s
,
1
,
pi
,
nonstrict
)
==
i
)
end
local
expected
=
1
--
expected position of "current" character
for
i
=
1
,
l
+
1
do
local
p
,
e
=
utf8.offset
(
s
,
i
)
assert
(
p
==
expected
)
expected
=
e
+
1
end
assert
(
expected
-
1
==
#
s
+
1
)
local
i
=
0
for
p
,
c
in
utf8.codes
(
s
,
nonstrict
)
do
i
=
i
+
1
assert
(
c
==
t
[
i
]
and
p
==
utf8.offset
(
s
,
i
))
assert
(
utf8.codepoint
(
s
,
p
,
p
,
nonstrict
)
==
c
)
end
assert
(
i
==
#
t
)
i
=
0
for
c
in
string.gmatch
(
s
,
utf8.charpattern
)
do
i
=
i
+
1
assert
(
c
==
utf8.char
(
t
[
i
]))
end
assert
(
i
==
#
t
)
for
i
=
1
,
l
do
assert
(
utf8.offset
(
s
,
i
)
==
utf8.offset
(
s
,
i
-
l
-
1
,
#
s
+
1
))
end
end
do
--
error indication in utf8.len
local
function
checklen
(
s
,
p
)
local
a
,
b
=
utf8.len
(
s
)
assert
(
not
a
and
b
==
p
)
end
checklen
(
"
abc
\xE3
def
"
,
4
)
checklen
(
"
\xF4\x9F\xBF
"
,
1
)
checklen
(
"
\xF4\x9F\xBF\xBF
"
,
1
)
--
spurious continuation bytes
checklen
(
"
汉字
\x80
"
,
#
(
"
汉字
"
)
+
1
)
checklen
(
"
\x80
hello
"
,
1
)
checklen
(
"
hel
\x80
lo
"
,
4
)
checklen
(
"
汉字
\xBF
"
,
#
(
"
汉字
"
)
+
1
)
checklen
(
"
\xBF
hello
"
,
1
)
checklen
(
"
hel
\xBF
lo
"
,
4
)
end
--
errors in utf8.codes
do
local
function
errorcodes
(
s
)
checkerror
(
"
invalid UTF%-8 code
"
,
function
()
for
c
in
utf8.codes
(
s
)
do
assert
(
c
)
end
end
)
end
errorcodes
(
"
ab
\xff
"
)
errorcodes
(
"
\u{110000}
"
)
errorcodes
(
"
in
\x80
valid
"
)
errorcodes
(
"
\xbf
invalid
"
)
errorcodes
(
"
αλφ
\xBF
α
"
)
--
calling iteration function with invalid arguments
local
f
=
utf8.codes
(
"
"
)
assert
(
f
(
"
"
,
2
)
==
nil
)
assert
(
f
(
"
"
,
-
1
)
==
nil
)
assert
(
f
(
"
"
,
math.mininteger
)
==
nil
)
end
--
error in initial position for offset
checkerror
(
"
position out of bounds
"
,
utf8.offset
,
"
abc
"
,
1
,
5
)
checkerror
(
"
position out of bounds
"
,
utf8.offset
,
"
abc
"
,
1
,
-
4
)
checkerror
(
"
position out of bounds
"
,
utf8.offset
,
"
"
,
1
,
2
)
checkerror
(
"
position out of bounds
"
,
utf8.offset
,
"
"
,
1
,
-
1
)
checkerror
(
"
continuation byte
"
,
utf8.offset
,
"
𦧺
"
,
1
,
2
)
checkerror
(
"
continuation byte
"
,
utf8.offset
,
"
𦧺
"
,
1
,
2
)
checkerror
(
"
continuation byte
"
,
utf8.offset
,
"
\x80
"
,
1
)
checkerror
(
"
continuation byte
"
,
utf8.offset
,
"
\x9c
"
,
-
1
)
--
error in indices for len
checkerror
(
"
out of bounds
"
,
utf8.len
,
"
abc
"
,
0
,
2
)
checkerror
(
"
out of bounds
"
,
utf8.len
,
"
abc
"
,
1
,
4
)
do
--
missing continuation bytes
--
get what is available
local
p
,
e
=
utf8.offset
(
"
\xE0
"
,
1
)
assert
(
p
==
1
and
e
==
1
)
local
p
,
e
=
utf8.offset
(
"
\xE0\x9e
"
,
-
1
)
assert
(
p
==
1
and
e
==
2
)
end
local
s
=
"
hello World
"
local
t
=
{
string.byte
(
s
,
1
,
-
1
)}
for
i
=
1
,
utf8.len
(
s
)
do
assert
(
t
[
i
]
==
string.byte
(
s
,
i
))
end
check
(
s
,
t
)
check
(
"
汉字/漢字
"
, {
27721
,
23383
,
47
,
28450
,
23383
,})
do
local
s
=
"
áéí
\128
"
local
t
=
{
utf8.codepoint
(
s
,
1
,
#
s
-
1
)}
assert
(
#
t
==
3
and
t
[
1
]
==
225
and
t
[
2
]
==
233
and
t
[
3
]
==
237
)
checkerror
(
"
invalid UTF%-8 code
"
,
utf8.codepoint
,
s
,
1
,
#
s
)
checkerror
(
"
out of bounds
"
,
utf8.codepoint
,
s
,
#
s
+
1
)
t
=
{
utf8.codepoint
(
s
,
4
,
3
)}
assert
(
#
t
==
0
)
checkerror
(
"
out of bounds
"
,
utf8.codepoint
,
s
,
-
(
#
s
+
1
),
1
)
checkerror
(
"
out of bounds
"
,
utf8.codepoint
,
s
,
1
,
#
s
+
1
)
--
surrogates
assert
(
utf8.codepoint
(
"
\u{D7FF}
"
)
==
0xD800
-
1
)
assert
(
utf8.codepoint
(
"
\u{E000}
"
)
==
0xDFFF
+
1
)
assert
(
utf8.codepoint
(
"
\u{D800}
"
,
1
,
1
,
true
)
==
0xD800
)
assert
(
utf8.codepoint
(
"
\u{DFFF}
"
,
1
,
1
,
true
)
==
0xDFFF
)
assert
(
utf8.codepoint
(
"
\u{7FFFFFFF}
"
,
1
,
1
,
true
)
==
0x7FFFFFFF
)
end
assert
(
utf8.char
()
==
"
"
)
assert
(
utf8.char
(
0
,
97
,
98
,
99
,
1
)
==
"
\0
abc
\1
"
)
assert
(
utf8.codepoint
(
utf8.char
(
0x10FFFF
))
==
0x10FFFF
)
assert
(
utf8.codepoint
(
utf8.char
(
0x7FFFFFFF
),
1
,
1
,
true
)
==
(
1
<<
31
)
-
1
)
checkerror
(
"
value out of range
"
,
utf8.char
,
0x7FFFFFFF
+
1
)
checkerror
(
"
value out of range
"
,
utf8.char
,
-
1
)
local
function
invalid
(
s
)
checkerror
(
"
invalid UTF%-8 code
"
,
utf8.codepoint
,
s
)
assert
(
not
utf8.len
(
s
))
end
--
UTF-8 representation for 0x11ffff (value out of valid range)
invalid
(
"
\xF4\x9F\xBF\xBF
"
)
--
surrogates
invalid
(
"
\u{D800}
"
)
invalid
(
"
\u{DFFF}
"
)
--
overlong sequences
invalid
(
"
\xC0\x80
"
)
--
zero
invalid
(
"
\xC1\xBF
"
)
--
0x7F (should be coded in 1 byte)
invalid
(
"
\xE0\x9F\xBF
"
)
--
0x7FF (should be coded in 2 bytes)
invalid
(
"
\xF0\x8F\xBF\xBF
"
)
--
0xFFFF (should be coded in 3 bytes)
--
invalid bytes
invalid
(
"
\x80
"
)
--
continuation byte
invalid
(
"
\xBF
"
)
--
continuation byte
invalid
(
"
\xFE
"
)
--
invalid byte
invalid
(
"
\xFF
"
)
--
invalid byte
--
empty string
check
(
"
"
, {})
--
minimum and maximum values for each sequence size
s
=
"
\0
\x7F
\z
\xC2\x80
\xDF\xBF
\z
\xE0\xA0\x80
\xEF\xBF\xBF
\z
\xF0\x90\x80\x80
\xF4\x8F\xBF\xBF
"
s
=
string.gsub
(
s
,
"
"
,
"
"
)
check
(
s
, {
0
,
0x7F
,
0x80
,
0x7FF
,
0x800
,
0xFFFF
,
0x10000
,
0x10FFFF
})
do
--
original UTF-8 values
local
s
=
"
\u{4000000}\u{7FFFFFFF}
"
assert
(
#
s
==
12
)
check
(
s
, {
0x4000000
,
0x7FFFFFFF
},
true
)
s
=
"
\u{200000}\u{3FFFFFF}
"
assert
(
#
s
==
10
)
check
(
s
, {
0x200000
,
0x3FFFFFF
},
true
)
s
=
"
\u{10000}\u{1fffff}
"
assert
(
#
s
==
8
)
check
(
s
, {
0x10000
,
0x1FFFFF
},
true
)
end
local
x
=
"
日本語a-4
\0
éó
"
check
(
x
, {
26085
,
26412
,
35486
,
97
,
45
,
52
,
0
,
233
,
243
})
--
Supplementary Characters
check
(
"
𣲷𠜎𠱓𡁻𠵼ab𠺢
"
,
{
0x23CB7
,
0x2070E
,
0x20C53
,
0x2107B
,
0x20D7C
,
0x61
,
0x62
,
0x20EA2
,})
check
(
"
𨳊𩶘𦧺𨳒𥄫𤓓
\xF4\x8F\xBF\xBF
"
,
{
0x28CCA
,
0x29D98
,
0x269FA
,
0x28CD2
,
0x2512B
,
0x244D3
,
0x10ffff
})
local
i
=
0
for
p
,
c
in
string.gmatch
(
x
,
"
()(
"
..
utf8.charpattern
..
"
)
"
)
do
i
=
i
+
1
assert
(
utf8.offset
(
x
,
i
)
==
p
)
assert
(
utf8.len
(
x
,
p
)
==
utf8.len
(
x
)
-
i
+
1
)
assert
(
utf8.len
(
c
)
==
1
)
for
j
=
1
,
#
c
-
1
do
assert
(
utf8.offset
(
x
,
0
,
p
+
j
-
1
)
==
p
)
end
end
print
'
ok
'
You can’t perform that action at this time.