aboutsummaryrefslogtreecommitdiff
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
parent2269fd42e2d44096d368718c314213ff7aafc164 (diff)
downloadfengari-dbbbb1a029fa3d6ad346426bee3310191e0dd285.tar.gz
fengari-dbbbb1a029fa3d6ad346426bee3310191e0dd285.tar.bz2
fengari-dbbbb1a029fa3d6ad346426bee3310191e0dd285.zip
utf8.char
-rw-r--r--README.md6
-rw-r--r--src/lutf8lib.js28
-rw-r--r--tests/lutf8lib.js32
3 files changed, 65 insertions, 1 deletions
diff --git a/README.md b/README.md
index 21f92fe..573288e 100644
--- a/README.md
+++ b/README.md
@@ -234,6 +234,12 @@
- [ ] io
- [ ] Debug
- [ ] utf8
+ - [x] utf8.char
+ - [x] utf8.codepoint
+ - [x] utf8.offset
+ - [ ] utf8.charpattern
+ - [ ] utf8.codes
+ - [ ] utf8.len
- [ ] Run [Lua test suite](https://github.com/lua/tests)
- [ ] DOM API binding
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 */
diff --git a/tests/lutf8lib.js b/tests/lutf8lib.js
index edfae56..7033a57 100644
--- a/tests/lutf8lib.js
+++ b/tests/lutf8lib.js
@@ -80,4 +80,36 @@ test('utf8.codepoint', function (t) {
"Correct element(s) on the stack"
);
+});
+
+
+test('utf8.char', function (t) {
+ let luaCode = `
+ return utf8.char(40, 32, 865, 176, 32, 860, 662, 32, 865, 176, 32, 41)
+ `, L;
+
+ t.plan(3);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ linit.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, luaCode);
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lapi.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+ t.strictEqual(
+ lapi.lua_tostring(L, -1),
+ "( ͡° ͜ʖ ͡° )",
+ "Correct element(s) on the stack"
+ );
+
}); \ No newline at end of file