Repository navigation
Expand file tree
/
Copy pathlua-utf8.d.lua
More file actions
259 lines (220 loc) · 8.42 KB
/
Copy pathlua-utf8.d.lua
File metadata and controls
259 lines (220 loc) · 8.42 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
--- @meta lua-utf8
-- Type definitions for luautf8 (https://github.com/starwing/luautf8).
-- API reference: README.md. Compatible with Lua 5.3 utf8 library.
local utf8 = {}
-- ================================ Constants ================================
--- Pattern matching the exact location of a UTF-8 byte sequence in a string.
utf8.charpattern = "[\0-\x7F\xC2-\xF4][\x80-\xBF]*"
-- ==================== Lua 5.3 utf8 compatibility ====================
--- Returns the position (in bytes) where the encoding of the n-th character
--- of s (counting from position 1) starts, or the position after the i-th
--- character. Returns nil if n is 0 or out of range.
--- @param s string
--- @param n integer
--- @param i? integer
--- @return integer?
function utf8.offset(s, n, i) end
--- Returns the code points of the substring starting at position i and ending
--- at j (both inclusive, default 1 and #s). When lax is true, invalid code
--- points such as surrogates are returned instead of raising an error.
--- @param s string
--- @param i? integer
--- @param j? integer
--- @param lax? boolean
--- @return integer ...
function utf8.codepoint(s, i, j, lax) end
--- Returns an iterator over the UTF-8 characters of s. When lax is true,
--- invalid sequences are skipped instead of raising an error.
--- @param s string
--- @param lax? boolean
--- @return fun(): integer, integer
function utf8.codes(s, lax) end
-- ==================== string module compatibility ====================
--- Returns the internal numeric codes of the characters of s.
--- @param s string
--- @param i? integer
--- @param j? integer
--- @return integer ...
function utf8.byte(s, i, j) end
--- Returns a string with each argument converted to a UTF-8 byte sequence.
--- @param ... integer
--- @return string
function utf8.char(...) end
--- Finds the first occurrence of pattern in s. Returns nil if not found.
--- @param s string
--- @param pattern string
--- @param init? integer
--- @param plain? boolean
--- @return integer? start
--- @return integer? end
function utf8.find(s, pattern, init, plain) end
--- Returns an iterator over all non-overlapping matches of pattern in s.
--- @param s string
--- @param pattern string
--- @return fun(): string ...
function utf8.gmatch(s, pattern) end
--- Replaces every occurrence of pattern with repl, returning the new string
--- and the number of substitutions.
--- @param s string
--- @param pattern string
--- @param repl string|table|function
--- @param n? integer
--- @return string
--- @return integer
function utf8.gsub(s, pattern, repl, n) end
--- Returns the number of UTF-8 characters in s, or nil plus an error message
--- if s is not a valid UTF-8 string. When lax is true, invalid sequences are
--- counted leniently instead of failing.
--- @param s string
--- @param i? integer
--- @param j? integer
--- @param lax? boolean
--- @return integer? len
--- @return string? err
function utf8.len(s, i, j, lax) end
--- Converts s to lowercase. With an integer argument, converts a code point.
--- @overload fun(s: string): string
--- @overload fun(cp: integer): integer
--- @param s string|integer
--- @return string|integer
function utf8.lower(s) end
--- Matches pattern in s, returning the captures (or the whole match).
--- @param s string
--- @param pattern string
--- @param init? integer
--- @return string ...
function utf8.match(s, pattern, init) end
--- Returns the reverse of s. Reverses by character, not by byte. When lax is
--- true, invalid sequences are reversed leniently without raising an error.
--- @param s string
--- @param lax? boolean
--- @return string
function utf8.reverse(s, lax) end
--- Returns the substring of s starting at i and ending at j.
--- @param s string
--- @param i? integer
--- @param j? integer
--- @return string
function utf8.sub(s, i, j) end
--- Converts s to uppercase. With an integer argument, converts a code point.
--- @overload fun(s: string): string
--- @overload fun(cp: integer): integer
--- @param s string|integer
--- @return string|integer
function utf8.upper(s) end
-- ======================== Unicode-specific ========================
--- Escapes s to UTF-8 format (supports %ddd, %{ddd}, %uddd, %u{ddd},
--- %xhhh, %x{hhh}, and %? for any other character).
--- @param s string
--- @return string
function utf8.escape(s) end
--- Converts UTF-8 character position n to byte position, also returning the
--- code point at the resulting position. Assumes s is valid UTF-8.
--- With one optional argument it is n; with two optional arguments they are
--- i (byte position) and n (character count after i).
--- @param s string
--- @param i? integer
--- @param n? integer
--- @return integer position
--- @return integer codepoint
function utf8.charpos(s, i, n) end
--- Iterates through s: returns the byte position of the next character after
--- i (or the n-th character), plus its code point. With only s given, usable
--- directly as an iterator.
--- @overload fun(s: string): fun(): integer?, integer?
--- @overload fun(s: string, i: integer, n?: integer): integer?, integer?
--- @return integer? position
--- @return integer? codepoint
function utf8.next(s, i, n) end
--- Inserts substring into s: before the n-th character (default: append).
--- @param s string
--- @param ... string|integer
--- @return string
function utf8.insert(s, ...) end
--- Deletes a substring from s: from i to j (inclusive), or the last character
--- when neither is given.
--- @param s string
--- @param i? integer
--- @param j? integer
--- @return string
function utf8.remove(s, i, j) end
--- Calculates the display width of s (or of a substring) in columns, or of a
--- single code point when given a number.
--- @overload fun(s: string, i?: integer, j?: integer, ambiwidth?: 1|2, default_width?: integer): integer
--- @overload fun(cp: integer, ambiwidth?: 1|2, default_width?: integer): integer
--- @param s string|integer
--- @return integer
function utf8.width(s, i, j, ambiwidth, default_width) end
--- Returns the character index idx at display width in s, plus offset (which
--- column within idx-th character) and the character's width.
--- @param s string
--- @param width integer
--- @param i? integer
--- @param j? integer
--- @param ambiwidth? 1|2
--- @param default_width? integer
--- @return integer idx
--- @return integer offset
--- @return integer width
function utf8.widthindex(s, width, i, j, ambiwidth, default_width) end
--- Finds the byte position where truncation should occur to fit within a
--- display width limit (negative limit truncates from the back).
--- @param s string
--- @param limit integer
--- @param i? integer
--- @param j? integer
--- @param ambiwidth? 1|2
--- @param default_width? integer
--- @return integer position
--- @return integer remain
function utf8.widthlimit(s, limit, i, j, ambiwidth, default_width) end
--- Converts s to title-case. With an integer argument, converts a code point.
--- @overload fun(s: string): string
--- @overload fun(cp: integer): integer
--- @param s string|integer
--- @return string|integer
function utf8.title(s) end
--- Converts s to folded case (for case-insensitive comparison).
--- With an integer argument, converts a code point.
--- @overload fun(s: string): string
--- @overload fun(cp: integer): integer
--- @param s string|integer
--- @return string|integer
function utf8.fold(s) end
--- Compares a and b without case: -1 if a < b, 0 if equal, 1 if a > b.
--- @param a string
--- @param b string
--- @return -1|0|1
function utf8.ncasecmp(a, b) end
--- Returns true if s is a valid UTF-8 string.
--- @param s string
--- @return boolean
function utf8.isvalid(s) end
--- Replaces invalid UTF-8 byte sequences in s with replacement (default: U+FFFD).
--- @param s string
--- @param replacement? string
--- @return string result
--- @return boolean was_valid
function utf8.clean(s, replacement) end
--- Returns the byte position of the first invalid byte sequence, or nil.
--- @param s string
--- @param i? integer
--- @return integer?
function utf8.invalidoffset(s, i) end
--- Returns true if s is in Normal Form C. Raises an error for invalid UTF-8.
--- @param s string
--- @return boolean
function utf8.isnfc(s) end
--- Converts s to Normal Form C.
--- @param s string
--- @return string result
--- @return boolean was_nfc
function utf8.normalize_nfc(s) end
--- Returns an iterator over grapheme clusters of s, yielding the inclusive
--- byte range [from, to] of each cluster.
--- @param s string
--- @param i? integer
--- @param j? integer
--- @return fun(): integer, integer
function utf8.grapheme_indices(s, i, j) end
return utf8