Coverage Report

Created: 2026-04-11 06:45

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