summaryrefslogtreecommitdiff
path: root/src/lstrlib.js
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2017-12-13 12:30:06 +1100
committerdaurnimator <quae@daurnimator.com>2017-12-13 15:04:04 +1100
commitc05459ce4663efca327eba53d60b1667c126058b (patch)
treeb448af1fbe670efd2f39418069da0ccd9474a831 /src/lstrlib.js
parent13347326dc460bcbb4708e914bcf704d222e36d7 (diff)
downloadfengari-c05459ce4663efca327eba53d60b1667c126058b.tar.gz
fengari-c05459ce4663efca327eba53d60b1667c126058b.tar.bz2
fengari-c05459ce4663efca327eba53d60b1667c126058b.zip
src/lstrlib.js: Replace adddigit function with call to .toString(16)
Diffstat (limited to 'src/lstrlib.js')
-rw-r--r--src/lstrlib.js29
1 files changed, 6 insertions, 23 deletions
diff --git a/src/lstrlib.js b/src/lstrlib.js
index 8b884b7..c4dd1ae 100644
--- a/src/lstrlib.js
+++ b/src/lstrlib.js
@@ -85,15 +85,6 @@ const SIZELENMOD = luaconf.LUA_NUMBER_FRMLEN.length + 1;
const L_NBFD = 1;
-/*
-** Add integer part of 'x' to buffer and return new 'x'
-*/
-const adddigit = function(buff, n, x) {
- let d = Math.floor(x); /* get integer part from 'x' */
- buff[n] = d < 10 ? d + '0'.charCodeAt(0) : d - 10 + 'a'.charCodeAt(0); /* add to buffer */
- return x - d; /* return what is left */
-};
-
const num2straux = function(x) {
/* if 'inf' or 'NaN', format it like '%g' */
if (Object.is(x, Infinity))
@@ -109,27 +100,19 @@ const num2straux = function(x) {
zero = "-" + zero;
return lua.to_luastring(zero);
} else {
- let buff = [];
+ let buff = "";
let fe = luaconf.frexp(x); /* 'x' fraction and exponent */
let m = fe[0];
let e = fe[1];
- let n = 0; /* character count */
if (m < 0) { /* is number negative? */
- buff[n++] = '-'.charCodeAt(0); /* add signal */
+ buff += '-'; /* add signal */
m = -m; /* make it positive */
}
- buff[n++] = '0'.charCodeAt(0);
- buff[n++] = 'x'.charCodeAt(0); /* add "0x" */
- m = adddigit(buff, n++, m * (1 << L_NBFD)); /* add first digit */
+ buff += "0x"; /* add "0x" */
+ buff += (m * (1<<L_NBFD)).toString(16);
e -= L_NBFD; /* this digit goes before the radix point */
- if (m > 0) { /* more digits? */
- buff[n++] = luaconf.lua_getlocaledecpoint().charCodeAt(0); /* add radix point */
- do { /* add as many digits as needed */
- m = adddigit(buff, n++, m * 16);
- } while (m > 0);
- }
- let exp = lua.to_luastring(sprintf("p%+d", e));
- return buff.concat(exp);
+ buff += sprintf("p%+d", e); /* add exponent */
+ return lua.to_luastring(buff);
}
};