/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 | 864k | #define MAXUNICODE  0x10FFFFu | 
| 26 |  |  | 
| 27 | 20.3k | #define MAXUTF    0x7FFFFFFFu | 
| 28 |  |  | 
| 29 |  |  | 
| 30 | 137 | #define MSGInvalid  "invalid UTF-8 code" | 
| 31 |  |  | 
| 32 |  |  | 
| 33 | 18.4k | #define iscont(c) (((c) & 0xC0) == 0x80) | 
| 34 | 1.18k | #define iscontp(p)  iscont(*(p)) | 
| 35 |  |  | 
| 36 |  |  | 
| 37 |  | /* from strlib */ | 
| 38 |  | /* translate a relative string position: negative means back from end */ | 
| 39 | 30.4k | static lua_Integer u_posrelat (lua_Integer pos, size_t len) { | 
| 40 | 30.4k |   if (pos >= 0) return pos; | 
| 41 | 9.22k |   else if (0u - (size_t)pos > len) return 0; | 
| 42 | 8.85k |   else return (lua_Integer)len + pos + 1; | 
| 43 | 30.4k | } | 
| 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 | 439k | static const char *utf8_decode (const char *s, l_uint32 *val, int strict) { | 
| 54 | 439k |   static const l_uint32 limits[] = | 
| 55 | 439k |         {~(l_uint32)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u}; | 
| 56 | 439k |   unsigned int c = (unsigned char)s[0]; | 
| 57 | 439k |   l_uint32 res = 0;  /* final result */ | 
| 58 | 439k |   if (c < 0x80)  /* ASCII? */ | 
| 59 | 425k |     res = c; | 
| 60 | 13.7k |   else { | 
| 61 | 13.7k |     int count = 0;  /* to count number of continuation bytes */ | 
| 62 | 25.9k |     for (; c & 0x40; c <<= 1) {  /* while it needs continuation bytes... */ | 
| 63 | 15.6k |       unsigned int cc = (unsigned char)s[++count];  /* read next byte */ | 
| 64 | 15.6k |       if (!iscont(cc))  /* not a continuation byte? */ | 
| 65 | 3.47k |         return NULL;  /* invalid byte sequence */ | 
| 66 | 12.1k |       res = (res << 6) | (cc & 0x3F);  /* add lower 6 bits from cont. byte */ | 
| 67 | 12.1k |     } | 
| 68 | 10.3k |     res |= ((l_uint32)(c & 0x7F) << (count * 5));  /* add first byte */ | 
| 69 | 10.3k |     if (count > 5 || res > MAXUTF || res < limits[count]) | 
| 70 | 2.98k |       return NULL;  /* invalid byte sequence */ | 
| 71 | 7.31k |     s += count;  /* skip continuation bytes read */ | 
| 72 | 7.31k |   } | 
| 73 | 432k |   if (strict) { | 
| 74 |  |     /* check for invalid code points; too large or surrogates */ | 
| 75 | 432k |     if (res > MAXUNICODE || (0xD800u <= res && res <= 0xDFFFu)) | 
| 76 | 265 |       return NULL; | 
| 77 | 432k |   } | 
| 78 | 432k |   if (val) *val = res; | 
| 79 | 432k |   return s + 1;  /* +1 to include first byte */ | 
| 80 | 432k | } | 
| 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 | 15.1k | static int utflen (lua_State *L) { | 
| 89 | 15.1k |   lua_Integer n = 0;  /* counter for the number of characters */ | 
| 90 | 15.1k |   size_t len;  /* string length in bytes */ | 
| 91 | 15.1k |   const char *s = luaL_checklstring(L, 1, &len); | 
| 92 | 15.1k |   lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); | 
| 93 | 15.1k |   lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len); | 
| 94 | 15.1k |   int lax = lua_toboolean(L, 4); | 
| 95 | 15.1k |   luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2, | 
| 96 | 15.1k |                    "initial position out of bounds"); | 
| 97 | 15.1k |   luaL_argcheck(L, --posj < (lua_Integer)len, 3, | 
| 98 | 15.1k |                    "final position out of bounds"); | 
| 99 | 446k |   while (posi <= posj) { | 
| 100 | 437k |     const char *s1 = utf8_decode(s + posi, NULL, !lax); | 
| 101 | 437k |     if (s1 == NULL) {  /* conversion error? */ | 
| 102 | 6.62k |       luaL_pushfail(L);  /* return fail ... */ | 
| 103 | 6.62k |       lua_pushinteger(L, posi + 1);  /* ... and current position */ | 
| 104 | 6.62k |       return 2; | 
| 105 | 6.62k |     } | 
| 106 | 431k |     posi = ct_diff2S(s1 - s); | 
| 107 | 431k |     n++; | 
| 108 | 431k |   } | 
| 109 | 8.51k |   lua_pushinteger(L, n); | 
| 110 | 8.51k |   return 1; | 
| 111 | 15.1k | } | 
| 112 |  |  | 
| 113 |  |  | 
| 114 |  | /* | 
| 115 |  | ** codepoint(s, [i, [j [, lax]]]) -> returns codepoints for all | 
| 116 |  | ** characters that start in the range [i,j] | 
| 117 |  | */ | 
| 118 | 82 | static int codepoint (lua_State *L) { | 
| 119 | 82 |   size_t len; | 
| 120 | 82 |   const char *s = luaL_checklstring(L, 1, &len); | 
| 121 | 82 |   lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); | 
| 122 | 82 |   lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len); | 
| 123 | 82 |   int lax = lua_toboolean(L, 4); | 
| 124 | 82 |   int n; | 
| 125 | 82 |   const char *se; | 
| 126 | 82 |   luaL_argcheck(L, posi >= 1, 2, "out of bounds"); | 
| 127 | 82 |   luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of bounds"); | 
| 128 | 82 |   if (posi > pose) return 0;  /* empty interval; return no values */ | 
| 129 | 82 |   if (pose - posi >= INT_MAX)  /* (lua_Integer -> int) overflow? */ | 
| 130 | 0 |     return luaL_error(L, "string slice too long"); | 
| 131 | 82 |   n = (int)(pose -  posi) + 1;  /* upper bound for number of returns */ | 
| 132 | 82 |   luaL_checkstack(L, n, "string slice too long"); | 
| 133 | 82 |   n = 0;  /* count the number of returns */ | 
| 134 | 82 |   se = s + pose;  /* string end */ | 
| 135 | 160 |   for (s += posi - 1; s < se;) { | 
| 136 | 81 |     l_uint32 code; | 
| 137 | 81 |     s = utf8_decode(s, &code, !lax); | 
| 138 | 81 |     if (s == NULL) | 
| 139 | 3 |       return luaL_error(L, MSGInvalid); | 
| 140 | 78 |     lua_pushinteger(L, l_castU2S(code)); | 
| 141 | 78 |     n++; | 
| 142 | 78 |   } | 
| 143 | 79 |   return n; | 
| 144 | 82 | } | 
| 145 |  |  | 
| 146 |  |  | 
| 147 | 1.43k | static void pushutfchar (lua_State *L, int arg) { | 
| 148 | 1.43k |   lua_Unsigned code = (lua_Unsigned)luaL_checkinteger(L, arg); | 
| 149 | 1.43k |   luaL_argcheck(L, code <= MAXUTF, arg, "value out of range"); | 
| 150 | 1.43k |   lua_pushfstring(L, "%U", (long)code); | 
| 151 | 1.43k | } | 
| 152 |  |  | 
| 153 |  |  | 
| 154 |  | /* | 
| 155 |  | ** utfchar(n1, n2, ...)  -> char(n1)..char(n2)... | 
| 156 |  | */ | 
| 157 | 3.40k | static int utfchar (lua_State *L) { | 
| 158 | 3.40k |   int n = lua_gettop(L);  /* number of arguments */ | 
| 159 | 3.40k |   if (n == 1)  /* optimize common case of single char */ | 
| 160 | 786 |     pushutfchar(L, 1); | 
| 161 | 2.61k |   else { | 
| 162 | 2.61k |     int i; | 
| 163 | 2.61k |     luaL_Buffer b; | 
| 164 | 2.61k |     luaL_buffinit(L, &b); | 
| 165 | 3.26k |     for (i = 1; i <= n; i++) { | 
| 166 | 645 |       pushutfchar(L, i); | 
| 167 | 645 |       luaL_addvalue(&b); | 
| 168 | 645 |     } | 
| 169 | 2.61k |     luaL_pushresult(&b); | 
| 170 | 2.61k |   } | 
| 171 | 3.40k |   return 1; | 
| 172 | 3.40k | } | 
| 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 | 22 | static int byteoffset (lua_State *L) { | 
| 180 | 22 |   size_t len; | 
| 181 | 22 |   const char *s = luaL_checklstring(L, 1, &len); | 
| 182 | 22 |   lua_Integer n  = luaL_checkinteger(L, 2); | 
| 183 | 22 |   lua_Integer posi = (n >= 0) ? 1 : cast_st2S(len) + 1; | 
| 184 | 22 |   posi = u_posrelat(luaL_optinteger(L, 3, posi), len); | 
| 185 | 22 |   luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3, | 
| 186 | 22 |                    "position out of bounds"); | 
| 187 | 22 |   if (n == 0) { | 
| 188 |  |     /* find beginning of current byte sequence */ | 
| 189 | 0 |     while (posi > 0 && iscontp(s + posi)) posi--; | 
| 190 | 0 |   } | 
| 191 | 22 |   else { | 
| 192 | 22 |     if (iscontp(s + posi)) | 
| 193 | 0 |       return luaL_error(L, "initial position is a continuation byte"); | 
| 194 | 22 |     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 | 22 |     else { | 
| 203 | 22 |       n--;  /* do not move for 1st character */ | 
| 204 | 22 |       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 | 22 |     } | 
| 211 | 22 |   } | 
| 212 | 22 |   if (n != 0) {  /* did not find given character? */ | 
| 213 | 0 |     luaL_pushfail(L); | 
| 214 | 0 |     return 1; | 
| 215 | 0 |   } | 
| 216 | 22 |   lua_pushinteger(L, posi + 1);  /* initial position */ | 
| 217 | 22 |   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 | 22 |   lua_pushinteger(L, posi + 1);  /* 'posi' now is the final position */ | 
| 225 | 22 |   return 2; | 
| 226 | 22 | } | 
| 227 |  |  | 
| 228 |  |  | 
| 229 | 3.31k | static int iter_aux (lua_State *L, int strict) { | 
| 230 | 3.31k |   size_t len; | 
| 231 | 3.31k |   const char *s = luaL_checklstring(L, 1, &len); | 
| 232 | 3.31k |   lua_Unsigned n = (lua_Unsigned)lua_tointeger(L, 2); | 
| 233 | 3.31k |   if (n < len) { | 
| 234 | 1.30k |     while (iscontp(s + n)) n++;  /* go to next character */ | 
| 235 | 1.30k |   } | 
| 236 | 3.31k |   if (n >= len)  /* (also handles original 'n' being negative) */ | 
| 237 | 2.03k |     return 0;  /* no more codepoints */ | 
| 238 | 1.27k |   else { | 
| 239 | 1.27k |     l_uint32 code; | 
| 240 | 1.27k |     const char *next = utf8_decode(s + n, &code, strict); | 
| 241 | 1.27k |     if (next == NULL || iscontp(next)) | 
| 242 | 134 |       return luaL_error(L, MSGInvalid); | 
| 243 | 1.14k |     lua_pushinteger(L, l_castU2S(n + 1)); | 
| 244 | 1.14k |     lua_pushinteger(L, l_castU2S(code)); | 
| 245 | 1.14k |     return 2; | 
| 246 | 1.27k |   } | 
| 247 | 3.31k | } | 
| 248 |  |  | 
| 249 |  |  | 
| 250 | 3.31k | static int iter_auxstrict (lua_State *L) { | 
| 251 | 3.31k |   return iter_aux(L, 1); | 
| 252 | 3.31k | } | 
| 253 |  |  | 
| 254 | 0 | static int iter_auxlax (lua_State *L) { | 
| 255 | 0 |   return iter_aux(L, 0); | 
| 256 | 0 | } | 
| 257 |  |  | 
| 258 |  |  | 
| 259 | 3.75k | static int iter_codes (lua_State *L) { | 
| 260 | 3.75k |   int lax = lua_toboolean(L, 2); | 
| 261 | 3.75k |   const char *s = luaL_checkstring(L, 1); | 
| 262 | 3.75k |   luaL_argcheck(L, !iscontp(s), 1, MSGInvalid); | 
| 263 | 3.75k |   lua_pushcfunction(L, lax ? iter_auxlax : iter_auxstrict); | 
| 264 | 3.75k |   lua_pushvalue(L, 1); | 
| 265 | 3.75k |   lua_pushinteger(L, 0); | 
| 266 | 3.75k |   return 3; | 
| 267 | 3.75k | } | 
| 268 |  |  | 
| 269 |  |  | 
| 270 |  | /* pattern to match a single UTF-8 character */ | 
| 271 | 59.3k | #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 | 29.6k | LUAMOD_API int luaopen_utf8 (lua_State *L) { | 
| 287 | 29.6k |   luaL_newlib(L, funcs); | 
| 288 | 29.6k |   lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1); | 
| 289 | 29.6k |   lua_setfield(L, -2, "charpattern"); | 
| 290 | 29.6k |   return 1; | 
| 291 | 29.6k | } | 
| 292 |  |  |