Coverage Report

Created: 2025-10-10 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/testdir/build/lua-master/source/lutf8lib.c
Line
Count
Source
1
/*
2
** $Id: lutf8lib.c $
3
** Standard library for UTF-8 manipulation
4
** See Copyright Notice in lua.h
5
*/
6
7
#define lutf8lib_c
8
#define LUA_LIB
9
10
#include "lprefix.h"
11
12
13
#include <assert.h>
14
#include <limits.h>
15
#include <stdlib.h>
16
#include <string.h>
17
18
#include "lua.h"
19
20
#include "lauxlib.h"
21
#include "lualib.h"
22
#include "llimits.h"
23
24
25
847k
#define MAXUNICODE  0x10FFFFu
26
27
19.3k
#define MAXUTF    0x7FFFFFFFu
28
29
30
148
#define MSGInvalid  "invalid UTF-8 code"
31
32
33
18.8k
#define iscont(c) (((c) & 0xC0) == 0x80)
34
1.83k
#define iscontp(p)  iscont(*(p))
35
36
37
/* from strlib */
38
/* translate a relative string position: negative means back from end */
39
17.3k
static lua_Integer u_posrelat (lua_Integer pos, size_t len) {
40
17.3k
  if (pos >= 0) return pos;
41
8.60k
  else if (0u - (size_t)pos > len) return 0;
42
8.25k
  else return (lua_Integer)len + pos + 1;
43
17.3k
}
44
45
46
/*
47
** Decode one UTF-8 sequence, returning NULL if byte sequence is
48
** invalid.  The array 'limits' stores the minimum value for each
49
** sequence length, to check for overlong representations. Its first
50
** entry forces an error for non-ASCII bytes with no continuation
51
** bytes (count == 0).
52
*/
53
430k
static const char *utf8_decode (const char *s, l_uint32 *val, int strict) {
54
430k
  static const l_uint32 limits[] =
55
430k
        {~(l_uint32)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u};
56
430k
  unsigned int c = (unsigned char)s[0];
57
430k
  l_uint32 res = 0;  /* final result */
58
430k
  if (c < 0x80)  /* ASCII? */
59
417k
    res = c;
60
13.0k
  else {
61
13.0k
    int count = 0;  /* to count number of continuation bytes */
62
24.5k
    for (; c & 0x40; c <<= 1) {  /* while it needs continuation bytes... */
63
14.7k
      unsigned int cc = (unsigned char)s[++count];  /* read next byte */
64
14.7k
      if (!iscont(cc))  /* not a continuation byte? */
65
3.20k
        return NULL;  /* invalid byte sequence */
66
11.5k
      res = (res << 6) | (cc & 0x3F);  /* add lower 6 bits from cont. byte */
67
11.5k
    }
68
9.79k
    res |= ((l_uint32)(c & 0x7F) << (count * 5));  /* add first byte */
69
9.79k
    if (count > 5 || res > MAXUTF || res < limits[count])
70
2.86k
      return NULL;  /* invalid byte sequence */
71
6.92k
    s += count;  /* skip continuation bytes read */
72
6.92k
  }
73
423k
  if (strict) {
74
    /* check for invalid code points; too large or surrogates */
75
423k
    if (res > MAXUNICODE || (0xD800u <= res && res <= 0xDFFFu))
76
230
      return NULL;
77
423k
  }
78
423k
  if (val) *val = res;
79
423k
  return s + 1;  /* +1 to include first byte */
80
423k
}
81
82
83
/*
84
** utf8len(s [, i [, j [, lax]]]) --> number of characters that
85
** start in the range [i,j], or nil + current position if 's' is not
86
** well formed in that interval
87
*/
88
8.60k
static int utflen (lua_State *L) {
89
8.60k
  lua_Integer n = 0;  /* counter for the number of characters */
90
8.60k
  size_t len;  /* string length in bytes */
91
8.60k
  const char *s = luaL_checklstring(L, 1, &len);
92
8.60k
  lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
93
8.60k
  lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len);
94
8.60k
  int lax = lua_toboolean(L, 4);
95
8.60k
  luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2,
96
8.60k
                   "initial position out of bounds");
97
8.60k
  luaL_argcheck(L, --posj < (lua_Integer)len, 3,
98
8.60k
                   "final position out of bounds");
99
430k
  while (posi <= posj) {
100
428k
    const char *s1 = utf8_decode(s + posi, NULL, !lax);
101
428k
    if (s1 == NULL) {  /* conversion error? */
102
6.20k
      luaL_pushfail(L);  /* return fail ... */
103
6.20k
      lua_pushinteger(L, posi + 1);  /* ... and current position */
104
6.20k
      return 2;
105
6.20k
    }
106
421k
    posi = ct_diff2S(s1 - s);
107
421k
    n++;
108
421k
  }
109
2.40k
  lua_pushinteger(L, n);
110
2.40k
  return 1;
111
8.60k
}
112
113
114
/*
115
** codepoint(s, [i, [j [, lax]]]) -> returns codepoints for all
116
** characters that start in the range [i,j]
117
*/
118
91
static int codepoint (lua_State *L) {
119
91
  size_t len;
120
91
  const char *s = luaL_checklstring(L, 1, &len);
121
91
  lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
122
91
  lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len);
123
91
  int lax = lua_toboolean(L, 4);
124
91
  int n;
125
91
  const char *se;
126
91
  luaL_argcheck(L, posi >= 1, 2, "out of bounds");
127
91
  luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of bounds");
128
91
  if (posi > pose) return 0;  /* empty interval; return no values */
129
91
  if (pose - posi >= INT_MAX)  /* (lua_Integer -> int) overflow? */
130
0
    return luaL_error(L, "string slice too long");
131
91
  n = (int)(pose -  posi) + 1;  /* upper bound for number of returns */
132
91
  luaL_checkstack(L, n, "string slice too long");
133
91
  n = 0;  /* count the number of returns */
134
91
  se = s + pose;  /* string end */
135
179
  for (s += posi - 1; s < se;) {
136
90
    l_uint32 code;
137
90
    s = utf8_decode(s, &code, !lax);
138
90
    if (s == NULL)
139
2
      return luaL_error(L, MSGInvalid);
140
88
    lua_pushinteger(L, l_castU2S(code));
141
88
    n++;
142
88
  }
143
89
  return n;
144
91
}
145
146
147
819
static void pushutfchar (lua_State *L, int arg) {
148
819
  lua_Unsigned code = (lua_Unsigned)luaL_checkinteger(L, arg);
149
819
  luaL_argcheck(L, code <= MAXUTF, arg, "value out of range");
150
819
  lua_pushfstring(L, "%U", (long)code);
151
819
}
152
153
154
/*
155
** utfchar(n1, n2, ...)  -> char(n1)..char(n2)...
156
*/
157
1.62k
static int utfchar (lua_State *L) {
158
1.62k
  int n = lua_gettop(L);  /* number of arguments */
159
1.62k
  if (n == 1)  /* optimize common case of single char */
160
429
    pushutfchar(L, 1);
161
1.19k
  else {
162
1.19k
    int i;
163
1.19k
    luaL_Buffer b;
164
1.19k
    luaL_buffinit(L, &b);
165
1.58k
    for (i = 1; i <= n; i++) {
166
390
      pushutfchar(L, i);
167
390
      luaL_addvalue(&b);
168
390
    }
169
1.19k
    luaL_pushresult(&b);
170
1.19k
  }
171
1.62k
  return 1;
172
1.62k
}
173
174
175
/*
176
** offset(s, n, [i])  -> indices where n-th character counting from
177
**   position 'i' starts and ends; 0 means character at 'i'.
178
*/
179
27
static int byteoffset (lua_State *L) {
180
27
  size_t len;
181
27
  const char *s = luaL_checklstring(L, 1, &len);
182
27
  lua_Integer n  = luaL_checkinteger(L, 2);
183
27
  lua_Integer posi = (n >= 0) ? 1 : cast_st2S(len) + 1;
184
27
  posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
185
27
  luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
186
27
                   "position out of bounds");
187
27
  if (n == 0) {
188
    /* find beginning of current byte sequence */
189
0
    while (posi > 0 && iscontp(s + posi)) posi--;
190
0
  }
191
27
  else {
192
27
    if (iscontp(s + posi))
193
0
      return luaL_error(L, "initial position is a continuation byte");
194
27
    if (n < 0) {
195
0
      while (n < 0 && posi > 0) {  /* move back */
196
0
        do {  /* find beginning of previous character */
197
0
          posi--;
198
0
        } while (posi > 0 && iscontp(s + posi));
199
0
        n++;
200
0
      }
201
0
    }
202
27
    else {
203
27
      n--;  /* do not move for 1st character */
204
27
      while (n > 0 && posi < (lua_Integer)len) {
205
0
        do {  /* find beginning of next character */
206
0
          posi++;
207
0
        } while (iscontp(s + posi));  /* (cannot pass final '\0') */
208
0
        n--;
209
0
      }
210
27
    }
211
27
  }
212
27
  if (n != 0) {  /* did not find given character? */
213
0
    luaL_pushfail(L);
214
0
    return 1;
215
0
  }
216
27
  lua_pushinteger(L, posi + 1);  /* initial position */
217
27
  if ((s[posi] & 0x80) != 0) {  /* multi-byte character? */
218
0
    if (iscont(s[posi]))
219
0
      return luaL_error(L, "initial position is a continuation byte");
220
0
    while (iscontp(s + posi + 1))
221
0
      posi++;  /* skip to last continuation byte */
222
0
  }
223
  /* else one-byte character: final position is the initial one */
224
27
  lua_pushinteger(L, posi + 1);  /* 'posi' now is the final position */
225
27
  return 2;
226
27
}
227
228
229
3.98k
static int iter_aux (lua_State *L, int strict) {
230
3.98k
  size_t len;
231
3.98k
  const char *s = luaL_checklstring(L, 1, &len);
232
3.98k
  lua_Unsigned n = (lua_Unsigned)lua_tointeger(L, 2);
233
3.98k
  if (n < len) {
234
1.95k
    while (iscontp(s + n)) n++;  /* go to next character */
235
1.95k
  }
236
3.98k
  if (n >= len)  /* (also handles original 'n' being negative) */
237
2.04k
    return 0;  /* no more codepoints */
238
1.93k
  else {
239
1.93k
    l_uint32 code;
240
1.93k
    const char *next = utf8_decode(s + n, &code, strict);
241
1.93k
    if (next == NULL || iscontp(next))
242
146
      return luaL_error(L, MSGInvalid);
243
1.78k
    lua_pushinteger(L, l_castU2S(n + 1));
244
1.78k
    lua_pushinteger(L, l_castU2S(code));
245
1.78k
    return 2;
246
1.93k
  }
247
3.98k
}
248
249
250
3.98k
static int iter_auxstrict (lua_State *L) {
251
3.98k
  return iter_aux(L, 1);
252
3.98k
}
253
254
0
static int iter_auxlax (lua_State *L) {
255
0
  return iter_aux(L, 0);
256
0
}
257
258
259
4.40k
static int iter_codes (lua_State *L) {
260
4.40k
  int lax = lua_toboolean(L, 2);
261
4.40k
  const char *s = luaL_checkstring(L, 1);
262
4.40k
  luaL_argcheck(L, !iscontp(s), 1, MSGInvalid);
263
4.40k
  lua_pushcfunction(L, lax ? iter_auxlax : iter_auxstrict);
264
4.40k
  lua_pushvalue(L, 1);
265
4.40k
  lua_pushinteger(L, 0);
266
4.40k
  return 3;
267
4.40k
}
268
269
270
/* pattern to match a single UTF-8 character */
271
53.6k
#define UTF8PATT  "[\0-\x7F\xC2-\xFD][\x80-\xBF]*"
272
273
274
static const luaL_Reg funcs[] = {
275
  {"offset", byteoffset},
276
  {"codepoint", codepoint},
277
  {"char", utfchar},
278
  {"len", utflen},
279
  {"codes", iter_codes},
280
  /* placeholders */
281
  {"charpattern", NULL},
282
  {NULL, NULL}
283
};
284
285
286
26.8k
LUAMOD_API int luaopen_utf8 (lua_State *L) {
287
26.8k
  luaL_newlib(L, funcs);
288
26.8k
  lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1);
289
26.8k
  lua_setfield(L, -2, "charpattern");
290
26.8k
  return 1;
291
26.8k
}
292