From dbbbb1a029fa3d6ad346426bee3310191e0dd285 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Wed, 15 Mar 2017 08:14:30 +0100 Subject: utf8.char --- src/lutf8lib.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/lutf8lib.js b/src/lutf8lib.js index f2c7f6b..6ebbec8 100644 --- a/src/lutf8lib.js +++ b/src/lutf8lib.js @@ -50,6 +50,31 @@ const utf8_decode = function(s, val) { }; }; +const pushutfchar = function(L, arg) { + let code = lauxlib.luaL_checkinteger(L, arg); + lauxlib.luaL_argcheck(L, 0 <= code && code <= MAXUNICODE, arg, "value out of range"); + lapi.lua_pushstring(L, `${String.fromCharCode(code)}`); +}; + +/* +** utfchar(n1, n2, ...) -> char(n1)..char(n2)... +*/ +const utfchar = function(L) { + let n = lapi.lua_gettop(L); /* number of arguments */ + if (n === 1) /* optimize common case of single char */ + pushutfchar(L, 1); + else { + let b = new lauxlib.luaL_Buffer(); + lauxlib.luaL_buffinit(L, b); + for (let i = 1; i <= n; i++) { + pushutfchar(L, i); + lauxlib.luaL_addvalue(b); + } + lauxlib.luaL_pushresult(b); + } + return 1; +}; + /* ** offset(s, n, [i]) -> index where n-th character counting from ** position 'i' starts; 0 means character at 'i'. @@ -128,8 +153,9 @@ const codepoint = function(L) { }; const funcs = { + "char": utfchar, "codepoint": codepoint, - "offset": byteoffset + "offset": byteoffset, }; /* pattern to match a single UTF-8 character */ -- cgit v1.2.3-70-g09d2