diff options
author | daurnimator <quae@daurnimator.com> | 2018-01-29 23:28:38 +1100 |
---|---|---|
committer | daurnimator <quae@daurnimator.com> | 2018-01-29 23:28:38 +1100 |
commit | 7ac9666b8d9c840dfed8105768becaf5e228f84a (patch) | |
tree | aa7f6f9dd6575e866867348a0e76609ef043cc1d /src | |
parent | c248ea3fc49cc75bdd68fcc23b1c96be8345b451 (diff) | |
download | fengari-7ac9666b8d9c840dfed8105768becaf5e228f84a.tar.gz fengari-7ac9666b8d9c840dfed8105768becaf5e228f84a.tar.bz2 fengari-7ac9666b8d9c840dfed8105768becaf5e228f84a.zip |
src/lstrlib.js: Add binary safe string.lower and string.upper
Diffstat (limited to 'src')
-rw-r--r-- | src/lstrlib.js | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/src/lstrlib.js b/src/lstrlib.js index b2c2818..fcaea79 100644 --- a/src/lstrlib.js +++ b/src/lstrlib.js @@ -673,15 +673,29 @@ const str_reverse = function(L) { const str_lower = function(L) { let s = luaL_checkstring(L, 1); - // TODO: will fail on invalid UTF-8 - lua_pushstring(L, to_luastring(to_jsstring(s).toLowerCase())); + let l = s.length; + let r = new Uint8Array(l); + for (let i=0; i<l; i++) { + let c = s[i]; + if (isupper(c)) + c = c | 0x20; + r[i] = c; + } + lua_pushstring(L, r); return 1; }; const str_upper = function(L) { let s = luaL_checkstring(L, 1); - // TODO: will fail on invalid UTF-8 - lua_pushstring(L, to_luastring(to_jsstring(s).toUpperCase())); + let l = s.length; + let r = new Uint8Array(l); + for (let i=0; i<l; i++) { + let c = s[i]; + if (islower(c)) + c = c & 0xdf; + r[i] = c; + } + lua_pushstring(L, r); return 1; }; |