tsuki 0.4.8

Lua 5.4 ported to Rust
Documentation
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#![allow(
    dead_code,
    mutable_transmutes,
    non_camel_case_types,
    non_snake_case,
    non_upper_case_globals,
    unused_assignments,
    unused_mut
)]

pub type utfint = libc::c_uint;

unsafe extern "C" fn u_posrelat(mut pos: i64, mut len: usize) -> i64 {
    if pos >= 0 as libc::c_int as i64 {
        return pos;
    } else if (0 as libc::c_uint as usize).wrapping_sub(pos as usize) > len {
        return 0 as libc::c_int as i64;
    } else {
        return len as i64 + pos + 1 as libc::c_int as i64;
    };
}
unsafe extern "C" fn utf8_decode(
    mut s: *const libc::c_char,
    mut val: *mut utfint,
    mut strict: libc::c_int,
) -> *const libc::c_char {
    static mut limits: [utfint; 6] = [
        !(0 as libc::c_int as utfint),
        0x80 as libc::c_int as utfint,
        0x800 as libc::c_int as utfint,
        0x10000 as libc::c_uint,
        0x200000 as libc::c_uint,
        0x4000000 as libc::c_uint,
    ];
    let mut c: libc::c_uint = *s.offset(0 as libc::c_int as isize) as libc::c_uchar as libc::c_uint;
    let mut res: utfint = 0 as libc::c_int as utfint;
    if c < 0x80 as libc::c_int as libc::c_uint {
        res = c;
    } else {
        let mut count: libc::c_int = 0 as libc::c_int;
        while c & 0x40 as libc::c_int as libc::c_uint != 0 {
            count += 1;
            let mut cc: libc::c_uint = *s.offset(count as isize) as libc::c_uchar as libc::c_uint;
            if !(cc & 0xc0 as libc::c_int as libc::c_uint == 0x80 as libc::c_int as libc::c_uint) {
                return 0 as *const libc::c_char;
            }
            res = res << 6 as libc::c_int | cc & 0x3f as libc::c_int as libc::c_uint;
            c <<= 1 as libc::c_int;
        }
        res |= (c & 0x7f as libc::c_int as libc::c_uint) << count * 5 as libc::c_int;
        if count > 5 as libc::c_int
            || res > 0x7fffffff as libc::c_uint
            || res < limits[count as usize]
        {
            return 0 as *const libc::c_char;
        }
        s = s.offset(count as isize);
    }
    if strict != 0 {
        if res > 0x10ffff as libc::c_uint
            || 0xd800 as libc::c_uint <= res && res <= 0xdfff as libc::c_uint
        {
            return 0 as *const libc::c_char;
        }
    }
    if !val.is_null() {
        *val = res;
    }
    return s.offset(1 as libc::c_int as isize);
}
unsafe extern "C" fn utflen(mut L: *mut lua_State) -> libc::c_int {
    let mut n: i64 = 0 as libc::c_int as i64;
    let mut len: usize = 0;
    let mut s: *const libc::c_char = luaL_checklstring(L, 1 as libc::c_int, &mut len);
    let mut posi: i64 = u_posrelat(
        luaL_optinteger(L, 2 as libc::c_int, 1 as libc::c_int as i64),
        len,
    );
    let mut posj: i64 = u_posrelat(
        luaL_optinteger(L, 3 as libc::c_int, -(1 as libc::c_int) as i64),
        len,
    );
    let mut lax: libc::c_int = lua_toboolean(L, 4 as libc::c_int);
    (((1 as libc::c_int as i64 <= posi && {
        posi -= 1;
        posi <= len as i64
    }) as libc::c_int
        != 0 as libc::c_int) as libc::c_int as libc::c_long
        != 0
        || luaL_argerror(
            L,
            2 as libc::c_int,
            b"initial position out of bounds\0" as *const u8 as *const libc::c_char,
        ) != 0) as libc::c_int;
    posj -= 1;
    (((posj < len as i64) as libc::c_int != 0 as libc::c_int) as libc::c_int as libc::c_long != 0
        || luaL_argerror(
            L,
            3 as libc::c_int,
            b"final position out of bounds\0" as *const u8 as *const libc::c_char,
        ) != 0) as libc::c_int;
    while posi <= posj {
        let mut s1: *const libc::c_char = utf8_decode(
            s.offset(posi as isize),
            0 as *mut utfint,
            (lax == 0) as libc::c_int,
        );
        if s1.is_null() {
            lua_pushnil(L);
            lua_pushinteger(L, posi + 1 as libc::c_int as i64);
            return 2 as libc::c_int;
        }
        posi = s1.offset_from(s) as libc::c_long as i64;
        n += 1;
        n;
    }
    lua_pushinteger(L, n);
    return 1 as libc::c_int;
}
unsafe extern "C" fn codepoint(mut L: *mut lua_State) -> libc::c_int {
    let mut len: usize = 0;
    let mut s: *const libc::c_char = luaL_checklstring(L, 1 as libc::c_int, &mut len);
    let mut posi: i64 = u_posrelat(
        luaL_optinteger(L, 2 as libc::c_int, 1 as libc::c_int as i64),
        len,
    );
    let mut pose: i64 = u_posrelat(luaL_optinteger(L, 3 as libc::c_int, posi), len);
    let mut lax: libc::c_int = lua_toboolean(L, 4 as libc::c_int);
    let mut n: libc::c_int = 0;
    let mut se: *const libc::c_char = 0 as *const libc::c_char;
    (((posi >= 1 as libc::c_int as i64) as libc::c_int != 0 as libc::c_int) as libc::c_int
        as libc::c_long
        != 0
        || luaL_argerror(
            L,
            2 as libc::c_int,
            b"out of bounds\0" as *const u8 as *const libc::c_char,
        ) != 0) as libc::c_int;
    (((pose <= len as i64) as libc::c_int != 0 as libc::c_int) as libc::c_int as libc::c_long != 0
        || luaL_argerror(
            L,
            3 as libc::c_int,
            b"out of bounds\0" as *const u8 as *const libc::c_char,
        ) != 0) as libc::c_int;
    if posi > pose {
        return 0 as libc::c_int;
    }
    if pose - posi >= 2147483647 as libc::c_int as i64 {
        return luaL_error(
            L,
            b"string slice too long\0" as *const u8 as *const libc::c_char,
        );
    }
    n = (pose - posi) as libc::c_int + 1 as libc::c_int;
    luaL_checkstack(
        L,
        n,
        b"string slice too long\0" as *const u8 as *const libc::c_char,
    );
    n = 0 as libc::c_int;
    se = s.offset(pose as isize);
    s = s.offset((posi - 1 as libc::c_int as i64) as isize);
    while s < se {
        let mut code: utfint = 0;
        s = utf8_decode(s, &mut code, (lax == 0) as libc::c_int);
        if s.is_null() {
            return luaL_error(
                L,
                b"invalid UTF-8 code\0" as *const u8 as *const libc::c_char,
            );
        }
        lua_pushinteger(L, code as i64);
        n += 1;
        n;
    }
    return n;
}
unsafe extern "C" fn pushutfchar(mut L: *mut lua_State, mut arg: libc::c_int) {
    let mut code: u64 = luaL_checkinteger(L, arg) as u64;
    (((code <= 0x7fffffff as libc::c_uint as u64) as libc::c_int != 0 as libc::c_int) as libc::c_int
        as libc::c_long
        != 0
        || luaL_argerror(
            L,
            arg,
            b"value out of range\0" as *const u8 as *const libc::c_char,
        ) != 0) as libc::c_int;
    lua_pushfstring(
        L,
        b"%U\0" as *const u8 as *const libc::c_char,
        code as libc::c_long,
    );
}
unsafe extern "C" fn utfchar(mut L: *mut lua_State) -> libc::c_int {
    let mut n: libc::c_int = lua_gettop(L);
    if n == 1 as libc::c_int {
        pushutfchar(L, 1 as libc::c_int);
    } else {
        let mut i: libc::c_int = 0;
        let mut b: luaL_Buffer = luaL_Buffer {
            b: 0 as *mut libc::c_char,
            size: 0,
            n: 0,
            L: 0 as *mut lua_State,
            init: C2RustUnnamed { n: 0. },
        };
        luaL_buffinit(L, &mut b);
        i = 1 as libc::c_int;
        while i <= n {
            pushutfchar(L, i);
            luaL_addvalue(&mut b);
            i += 1;
            i;
        }
        luaL_pushresult(&mut b);
    }
    return 1 as libc::c_int;
}
unsafe extern "C" fn byteoffset(mut L: *mut lua_State) -> libc::c_int {
    let mut len: usize = 0;
    let mut s: *const libc::c_char = luaL_checklstring(L, 1 as libc::c_int, &mut len);
    let mut n: i64 = luaL_checkinteger(L, 2 as libc::c_int);
    let mut posi: i64 = (if n >= 0 as libc::c_int as i64 {
        1 as libc::c_int as usize
    } else {
        len.wrapping_add(1 as libc::c_int as usize)
    }) as i64;
    posi = u_posrelat(luaL_optinteger(L, 3 as libc::c_int, posi), len);
    (((1 as libc::c_int as i64 <= posi && {
        posi -= 1;
        posi <= len as i64
    }) as libc::c_int
        != 0 as libc::c_int) as libc::c_int as libc::c_long
        != 0
        || luaL_argerror(
            L,
            3 as libc::c_int,
            b"position out of bounds\0" as *const u8 as *const libc::c_char,
        ) != 0) as libc::c_int;
    if n == 0 as libc::c_int as i64 {
        while posi > 0 as libc::c_int as i64
            && *s.offset(posi as isize) as libc::c_int & 0xc0 as libc::c_int == 0x80 as libc::c_int
        {
            posi -= 1;
            posi;
        }
    } else {
        if *s.offset(posi as isize) as libc::c_int & 0xc0 as libc::c_int == 0x80 as libc::c_int {
            return luaL_error(
                L,
                b"initial position is a continuation byte\0" as *const u8 as *const libc::c_char,
            );
        }
        if n < 0 as libc::c_int as i64 {
            while n < 0 as libc::c_int as i64 && posi > 0 as libc::c_int as i64 {
                loop {
                    posi -= 1;
                    posi;
                    if !(posi > 0 as libc::c_int as i64
                        && *s.offset(posi as isize) as libc::c_int & 0xc0 as libc::c_int
                            == 0x80 as libc::c_int)
                    {
                        break;
                    }
                }
                n += 1;
                n;
            }
        } else {
            n -= 1;
            n;
            while n > 0 as libc::c_int as i64 && posi < len as i64 {
                loop {
                    posi += 1;
                    posi;
                    if !(*s.offset(posi as isize) as libc::c_int & 0xc0 as libc::c_int
                        == 0x80 as libc::c_int)
                    {
                        break;
                    }
                }
                n -= 1;
                n;
            }
        }
    }
    if n == 0 as libc::c_int as i64 {
        lua_pushinteger(L, posi + 1 as libc::c_int as i64);
    } else {
        lua_pushnil(L);
    }
    return 1 as libc::c_int;
}
unsafe extern "C" fn iter_aux(mut L: *mut lua_State, mut strict: libc::c_int) -> libc::c_int {
    let mut len: usize = 0;
    let mut s: *const libc::c_char = luaL_checklstring(L, 1 as libc::c_int, &mut len);
    let mut n: u64 = lua_tointegerx(L, 2 as libc::c_int, 0 as *mut libc::c_int) as u64;
    if n < len as u64 {
        while *s.offset(n as isize) as libc::c_int & 0xc0 as libc::c_int == 0x80 as libc::c_int {
            n = n.wrapping_add(1);
            n;
        }
    }
    if n >= len as u64 {
        return 0 as libc::c_int;
    } else {
        let mut code: utfint = 0;
        let mut next: *const libc::c_char = utf8_decode(s.offset(n as isize), &mut code, strict);
        if next.is_null() || *next as libc::c_int & 0xc0 as libc::c_int == 0x80 as libc::c_int {
            return luaL_error(
                L,
                b"invalid UTF-8 code\0" as *const u8 as *const libc::c_char,
            );
        }
        lua_pushinteger(L, n.wrapping_add(1 as libc::c_int as u64) as i64);
        lua_pushinteger(L, code as i64);
        return 2 as libc::c_int;
    };
}
unsafe extern "C" fn iter_auxstrict(mut L: *mut lua_State) -> libc::c_int {
    return iter_aux(L, 1 as libc::c_int);
}
unsafe extern "C" fn iter_auxlax(mut L: *mut lua_State) -> libc::c_int {
    return iter_aux(L, 0 as libc::c_int);
}
unsafe extern "C" fn iter_codes(mut L: *mut lua_State) -> libc::c_int {
    let mut lax: libc::c_int = lua_toboolean(L, 2 as libc::c_int);
    let mut s: *const libc::c_char = luaL_checklstring(L, 1 as libc::c_int, 0 as *mut usize);
    ((!(*s as libc::c_int & 0xc0 as libc::c_int == 0x80 as libc::c_int) as libc::c_int
        != 0 as libc::c_int) as libc::c_int as libc::c_long
        != 0
        || luaL_argerror(
            L,
            1 as libc::c_int,
            b"invalid UTF-8 code\0" as *const u8 as *const libc::c_char,
        ) != 0) as libc::c_int;
    lua_pushcclosure(
        L,
        if lax != 0 {
            Some(iter_auxlax as unsafe extern "C" fn(*mut lua_State) -> libc::c_int)
        } else {
            Some(iter_auxstrict as unsafe extern "C" fn(*mut lua_State) -> libc::c_int)
        },
        0 as libc::c_int,
    );
    lua_pushvalue(L, 1 as libc::c_int);
    lua_pushinteger(L, 0 as libc::c_int as i64);
    return 3 as libc::c_int;
}
static mut funcs: [luaL_Reg; 7] = unsafe {
    [
        {
            let mut init = luaL_Reg {
                name: b"offset\0" as *const u8 as *const libc::c_char,
                func: Some(byteoffset as unsafe extern "C" fn(*mut lua_State) -> libc::c_int),
            };
            init
        },
        {
            let mut init = luaL_Reg {
                name: b"codepoint\0" as *const u8 as *const libc::c_char,
                func: Some(codepoint as unsafe extern "C" fn(*mut lua_State) -> libc::c_int),
            };
            init
        },
        {
            let mut init = luaL_Reg {
                name: b"char\0" as *const u8 as *const libc::c_char,
                func: Some(utfchar as unsafe extern "C" fn(*mut lua_State) -> libc::c_int),
            };
            init
        },
        {
            let mut init = luaL_Reg {
                name: b"len\0" as *const u8 as *const libc::c_char,
                func: Some(utflen as unsafe extern "C" fn(*mut lua_State) -> libc::c_int),
            };
            init
        },
        {
            let mut init = luaL_Reg {
                name: b"codes\0" as *const u8 as *const libc::c_char,
                func: Some(iter_codes as unsafe extern "C" fn(*mut lua_State) -> libc::c_int),
            };
            init
        },
        {
            let mut init = luaL_Reg {
                name: b"charpattern\0" as *const u8 as *const libc::c_char,
                func: None,
            };
            init
        },
        {
            let mut init = luaL_Reg {
                name: 0 as *const libc::c_char,
                func: None,
            };
            init
        },
    ]
};
#[no_mangle]
pub unsafe extern "C" fn luaopen_utf8(mut L: *mut lua_State) -> libc::c_int {
    lua_createtable(
        L,
        0 as libc::c_int,
        (::core::mem::size_of::<[luaL_Reg; 7]>() as libc::c_ulong)
            .wrapping_div(::core::mem::size_of::<luaL_Reg>() as libc::c_ulong)
            .wrapping_sub(1 as libc::c_int as libc::c_ulong) as libc::c_int,
    );
    luaL_setfuncs(L, funcs.as_ptr(), 0 as libc::c_int);
    lua_pushlstring(
        L,
        b"[\0-\x7F\xC2-\xFD][\x80-\xBF]*\0" as *const u8 as *const libc::c_char,
        (::core::mem::size_of::<[libc::c_char; 15]>() as libc::c_ulong)
            .wrapping_div(::core::mem::size_of::<libc::c_char>() as libc::c_ulong)
            .wrapping_sub(1 as libc::c_int as libc::c_ulong),
    );
    lua_setfield(
        L,
        -(2 as libc::c_int),
        b"charpattern\0" as *const u8 as *const libc::c_char,
    );
    return 1 as libc::c_int;
}