LCOV - code coverage report
Current view: top level - external/lua/src - lutf8lib.c (source / functions) Hit Total Coverage
Test: rapport Lines: 5 148 3.4 %
Date: 2021-12-10 16:22:55 Functions: 1 12 8.3 %
Branches: 0 96 0.0 %

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

Generated by: LCOV version 1.15