lua_std/utf8.rs
1//! Basic support for UTF-8 encoding.
2//!
3//! This library does not provide any support for Unicode other than the
4//! handling of the encoding itself. Any operation that needs the meaning
5//! of a character, such as character classification, is outside its
6//! scope.
7//!
8//! Unless stated otherwise, all functions that expect a byte position as
9//! a parameter assume that the given position is either the start of a
10//! byte sequence or one plus the length of the subject string.
11
12use crate::{boolean, function, number, option, string, Repeatable};
13
14/// The pattern `"[\0-\x7F\xC2-\xFD][\x80-\xBF]*"`, which matches exactly
15/// one UTF-8 byte sequence, assuming that the subject is a valid UTF-8
16/// string.
17///
18/// # Examples
19///
20/// ```lua
21/// for c in ("héllo"):gmatch(utf8.charpattern) do
22/// io.write(c, " ") --> h é l l o
23/// end
24/// ```
25pub const charpattern: string = string;
26
27/// Receives zero or more integers, converts each one to its
28/// corresponding UTF-8 byte sequence, and returns a string with the
29/// concatenation of all these sequences.
30///
31/// # Examples
32///
33/// ```lua
34/// print(utf8.char(72, 105)) --> Hi
35/// print(utf8.char(233, 8364)) --> é€
36/// ```
37pub fn char(codes: Repeatable<number>) -> string {
38 todo!()
39}
40
41/// Returns the code points (as integers) from all characters in `s` that
42/// start between byte positions `i` and `j` (both included). The default
43/// for `i` is 1 and for `j` is `i`. It raises an error if it meets any
44/// invalid byte sequence.
45///
46/// # Examples
47///
48/// ```lua
49/// print(utf8.codepoint("héllo")) --> 104
50/// print(utf8.codepoint("héllo", 1, 3)) --> 104 233
51/// ```
52pub fn codepoint(s: string, i: option<number>, j: option<number>) -> Repeatable<number> {
53 todo!()
54}
55
56/// Returns values so that the construction
57///
58/// ```lua
59/// for p, c in utf8.codes(s) do body end
60/// ```
61///
62/// will iterate over all UTF-8 characters in string `s`, with `p` being
63/// the position (in bytes) and `c` the code point of each character. It
64/// raises an error if it meets any invalid byte sequence.
65pub fn codes(s: string, lax: option<boolean>) -> function {
66 todo!()
67}
68
69/// Returns the number of UTF-8 characters in string `s` that start
70/// between byte positions `i` and `j` (default 1 and −1). If it finds
71/// any invalid byte sequence, returns `nil` plus the position of the
72/// first invalid byte.
73///
74/// # Examples
75///
76/// ```lua
77/// print(utf8.len("héllo")) --> 5
78/// print(#"héllo") --> 6 (é takes two bytes)
79/// print(utf8.len("\xFF")) --> nil 1 (invalid byte at position 1)
80/// ```
81pub fn len(s: string, i: option<number>, j: option<number>) -> option<number> {
82 todo!()
83}
84
85/// Returns the position (in bytes) where the encoding of the `n`-th
86/// character of `s` (counting from byte position `i`) starts, and —
87/// **since Lua 5.5** — also the position of the final byte of that
88/// character. A negative `n` gets characters before position `i`.
89/// `utf8.offset(s, -1)` gets the last character of the string.
90///
91/// # Examples
92///
93/// ```lua
94/// print(utf8.offset("héllo", 2)) --> 2 3 (é spans bytes 2-3)
95/// print(utf8.offset("héllo", 3)) --> 4 4 (l is a single byte)
96/// print(utf8.offset("héllo", -1)) --> 6 6 (the last char)
97/// ```
98pub fn offset(s: string, n: number, i: option<number>) -> option<(number, number)> {
99 todo!()
100}