Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/lua/blob/master/lutf8lib.c # lua [Ported from github.com/lua/lua] Cast in 'rl_readline_name' Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `lua / lutf8lib.c` 294 lines ยท 8583 bytes Raw Blame History 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 14 #include 15 #include 16 17 #include "lua.h" 18 19 #include "lauxlib.h" 20 #include "lualib.h" 21 #include "llimits.h" 22 23 24 #define MAXUNICODE 0x10FFFFu 25 26 #define MAXUTF 0x7FFFFFFFu 27 28 29 #define MSGInvalid "invalid UTF-8 code" 30 31 32 #define iscont(c) (((c) & 0xC0) == 0x80) 33 #define iscontp(p) iscont(*(p)) 34 35 36 /* from strlib */ 37 /* translate a relative string position: negative means back from end */ 38 static lua_Integer u_posrelat (lua_Integer pos, size_t len) { 39 if (pos >= 0) return pos; 40 else if (0u - (size_t)pos > len) return 0; 41 else return (lua_Integer)len + pos + 1; 42 } 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 static const char *utf8_decode (const char *s, l_uint32 *val, int strict) { 53 static const l_uint32 limits[] = 54 {~(l_uint32)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u}; 55 unsigned int c = (unsigned char)s[0]; 56 l_uint32 res = 0; /* final result */ 57 if (c < 0x80) /* ASCII? */ 58 res = c; 59 else if (c >= 0xfe) /* c >= 1111 1110b ? */ 60 return NULL; /* would need six or more continuation bytes */ 61 else { 62 int count = 0; /* to count number of continuation bytes */ 63 for (; c & 0x40; c <<= 1) { /* while it needs continuation bytes... */ 64 unsigned int cc = (unsigned char)s[++count]; /* read next byte */ 65 if (!iscont(cc)) /* not a continuation byte? */ 66 return NULL; /* invalid byte sequence */ 67 res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */ 68 } 69 lua_assert(count <= 5); 70 res |= ((l_uint32)(c & 0x7F) << (count * 5)); /* add first byte */ 71 if (res > MAXUTF || res < limits[count]) 72 return NULL; /* invalid byte sequence */ 73 s += count; /* skip continuation bytes read */ 74 } 75 if (strict) { 76 /* check for invalid code points; too large or surrogates */ 77 if (res > MAXUNICODE || (0xD800u <= res && res <= 0xDFFFu)) 78 return NULL; 79 } 80 if (val) *val = res; 81 return s + 1; /* +1 to include first byte */ 82 } 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 static int utflen (lua_State *L) { 91 lua_Integer n = 0; /* counter for the number of characters */ 92 size_t len; /* string length in bytes */ 93 const char *s = luaL_checklstring(L, 1, &len); 94 lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); 95 lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len); 96 int lax = lua_toboolean(L, 4); 97 luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2, 98 "initial position out of bounds"); 99 luaL_argcheck(L, --posj < (lua_Integer)len, 3, 100 "final position out of bounds"); 101 while (posi <= posj) { 102 const char *s1 = utf8_decode(s + posi, NULL, !lax); 103 if (s1 == NULL) { /* conversion error? */ 104 luaL_pushfail(L); /* return fail ... */ 105 lua_pushinteger(L, posi + 1); /* ... and current position */ 106 return 2; 107 } 108 posi = ct_diff2S(s1 - s); 109 n++; 110 } 111 lua_pushinteger(L, n); 112 return 1; 113 } 114 115 116 /* 117 ** codepoint(s, [i, [j [, lax]]]) -> returns codepoints for all 118 ** characters that start in the range [i,j] 119 */ 120 static int codepoint (lua_State *L) { 121 size_t len; 122 const char *s = luaL_checklstring(L, 1, &len); 123 lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); 124 lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len); 125 int lax = lua_toboolean(L, 4); 126 int n; 127 const char *se; 128 luaL_argcheck(L, posi >= 1, 2, "out of bounds"); 129 luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of bounds"); 130 if (posi > pose) return 0; /* empty interval; return no values */ 131 if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */ 132 return luaL_error(L, "string slice too long"); 133 n = (int)(pose - posi) + 1; /* upper bound for number of returns */ 134 luaL_checkstack(L, n, "string slice too long"); 135 n = 0; /* count the number of returns */ 136 se = s + pose; /* string end */ 137 for (s += posi - 1; s < se;) { 138 l_uint32 code; 139 s = utf8_decode(s, &code, !lax); 140 if (s == NULL) 141 return luaL_error(L, MSGInvalid); 142 lua_pushinteger(L, l_castU2S(code)); 143 n++; 144 } 145 return n; 146 } 147 148 149 static void pushutfchar (lua_State *L, int arg) { 150 lua_Unsigned code = (lua_Unsigned)luaL_checkinteger(L, arg); 151 luaL_argcheck(L, code <= MAXUTF, arg, "value out of range"); 152 lua_pushfstring(L, "%U", cast(unsigned long, code)); 153 } 154 155 156 /* 157 ** utfchar(n1, n2, ...) -> char(n1)..char(n2)... 158 */ 159 static int utfchar (lua_State *L) { 160 int n = lua_gettop(L); /* number of arguments */ 161 if (n == 1) /* optimize common case of single char */ 162 pushutfchar(L, 1); 163 else { 164 int i; 165 luaL_Buffer b; 166 luaL_buffinit(L, &b); 167 for (i = 1; i <= n; i++) { 168 pushutfchar(L, i); 169 luaL_addvalue(&b); 170 } 171 luaL_pushresult(&b); 172 } 173 return 1; 174 } 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 static int byteoffset (lua_State *L) { 182 size_t len; 183 const char *s = luaL_checklstring(L, 1, &len); 184 lua_Integer n = luaL_checkinteger(L, 2); 185 lua_Integer posi = (n >= 0) ? 1 : cast_st2S(len) + 1; 186 posi = u_posrelat(luaL_optinteger(L, 3, posi), len); 187 luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3, 188 "position out of bounds"); 189 if (n == 0) { 190 /* find beginning of current byte sequence */ 191 while (posi > 0 && iscontp(s + posi)) posi--; 192 } 193 else { 194 if (iscontp(s + posi)) 195 return luaL_error(L, "initial position is a continuation byte"); 196 if (n < 0) { 197 while (n < 0 && posi > 0) { /* move back */ 198 do { /* find beginning of previous character */ 199 posi--; 200 } while (posi > 0 && iscontp(s + posi)); 201 n++; 202 } 203 } 204 else { 205 n--; /* do not move for 1st character */ 206 while (n > 0 && posi < (lua_Integer)len) { 207 do { /* find beginning of next character */ 208 posi++; 209 } while (iscontp(s + posi)); /* (cannot pass final '\0') */ 210 n--; 211 } 212 } 213 } 214 if (n != 0) { /* did not find given character? */ 215 luaL_pushfail(L); 216 return 1; 217 } 218 lua_pushinteger(L, posi + 1); /* initial position */ 219 if ((s[posi] & 0x80) != 0) { /* multi-byte character? */ 220 if (iscont(s[posi])) 221 return luaL_error(L, "initial position is a continuation byte"); 222 while (iscontp(s + posi + 1)) 223 posi++; /* skip to last continuation byte */ 224 } 225 /* else one-byte character: final position is the initial one */ 226 lua_pushinteger(L, posi + 1); /* 'posi' now is the final position */ 227 return 2; 228 } 229 230 231 static int iter_aux (lua_State *L, int strict) { 232 size_t len; 233 const char *s = luaL_checklstring(L, 1, &len); 234 lua_Unsigned n = (lua_Unsigned)lua_tointeger(L, 2); 235 if (n < len) { 236 while (iscontp(s + n)) n++; /* go to next character */ 237 } 238 if (n >= len) /* (also handles original 'n' being negative) */ 239 return 0; /* no more codepoints */ 240 else { 241 l_uint32 code; 242 const char *next = utf8_decode(s + n, &code, strict); 243 if (next == NULL || iscontp(next)) 244 return luaL_error(L, MSGInvalid); 245 lua_pushinteger(L, l_castU2S(n + 1)); 246 lua_pushinteger(L, l_castU2S(code)); 247 return 2; 248 } 249 } 250 251 252 static int iter_auxstrict (lua_State *L) { 253 return iter_aux(L, 1); 254 } 255 256 static int iter_auxlax (lua_State *L) { 257 return iter_aux(L, 0); 258 } 259 260 261 static int iter_codes (lua_State *L) { 262 int lax = lua_toboolean(L, 2); 263 const char *s = luaL_checkstring(L, 1); 264 luaL_argcheck(L, !iscontp(s), 1, MSGInvalid); 265 lua_pushcfunction(L, lax ? iter_auxlax : iter_auxstrict); 266 lua_pushvalue(L, 1); 267 lua_pushinteger(L, 0); 268 return 3; 269 } 270 271 272 /* pattern to match a single UTF-8 character */ 273 #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 LUAMOD_API int luaopen_utf8 (lua_State *L) { 289 luaL_newlib(L, funcs); 290 lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1); 291 lua_setfield(L, -2, "charpattern"); 292 return 1; 293 } 294