summaryrefslogtreecommitdiff
path: root/src/lutf8lib.js
diff options
context:
space:
mode:
authorBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-03-15 08:14:30 +0100
committerBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-03-15 08:14:30 +0100
commitdbbbb1a029fa3d6ad346426bee3310191e0dd285 (patch)
tree66da462afbc1901e6463d47ecb23f2b997a4207d /src/lutf8lib.js
parent2269fd42e2d44096d368718c314213ff7aafc164 (diff)
downloadfengari-dbbbb1a029fa3d6ad346426bee3310191e0dd285.tar.gz
fengari-dbbbb1a029fa3d6ad346426bee3310191e0dd285.tar.bz2
fengari-dbbbb1a029fa3d6ad346426bee3310191e0dd285.zip
utf8.char
Diffstat (limited to 'src/lutf8lib.js')
-rw-r--r--src/lutf8lib.js28
1 files changed, 27 insertions, 1 deletions
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 */