aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ljstype.js54
-rw-r--r--src/lobject.js7
2 files changed, 51 insertions, 10 deletions
diff --git a/src/ljstype.js b/src/ljstype.js
index 2528f74..4d46fa9 100644
--- a/src/ljstype.js
+++ b/src/ljstype.js
@@ -1,27 +1,69 @@
"use strict";
+const luai_ctype_ = [
+ 0x00, /* EOZ */
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */
+ 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */
+ 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
+ 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */
+ 0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
+ 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */
+ 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
+ 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */
+ 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05,
+ 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */
+ 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
+ 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */
+ 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+];
+
+const ALPHABIT = 0;
+const DIGITBIT = 1;
+const PRINTBIT = 2;
+const SPACEBIT = 3;
+const XDIGITBIT = 4;
+
const lisdigit = function(c) {
- return /^\d$/.test(String.fromCharCode(c));
+ return (luai_ctype_[c+1] & (1<<DIGITBIT)) !== 0;
};
const lisxdigit = function(c) {
- return /^[0-9a-fA-F]$/.test(String.fromCharCode(c));
+ return (luai_ctype_[c+1] & (1<<XDIGITBIT)) !== 0;
};
const lisprint = function(c) {
- return /^[\x20-\x7E]$/.test(String.fromCharCode(c));
+ return (luai_ctype_[c+1] & (1<<PRINTBIT)) !== 0;
};
const lisspace = function(c) {
- return /^\s$/.test(String.fromCharCode(c));
+ return (luai_ctype_[c+1] & (1<<SPACEBIT)) !== 0;
};
const lislalpha = function(c) {
- return /^[_a-zA-Z]$/.test(String.fromCharCode(c));
+ return (luai_ctype_[c+1] & (1<<ALPHABIT)) !== 0;
};
const lislalnum = function(c) {
- return /^[_a-zA-Z0-9]$/.test(String.fromCharCode(c));
+ return (luai_ctype_[c+1] & ((1<<ALPHABIT)|(1<<DIGITBIT))) !== 0;
};
module.exports.lisdigit = lisdigit;
diff --git a/src/lobject.js b/src/lobject.js
index 5405b34..4b3d0aa 100644
--- a/src/lobject.js
+++ b/src/lobject.js
@@ -547,13 +547,12 @@ const l_str2int = function(s) {
else if (s[i] === 43 /* ('+').charCodeAt(0) */) i++;
if (s[i] === 48 /* ('0').charCodeAt(0) */ && (s[i+1] === 120 /* ('x').charCodeAt(0) */ || s[i+1] === 88 /* ('X').charCodeAt(0) */)) { /* hex? */
i += 2; /* skip '0x' */
-
- for (; lisxdigit(s[i]); i++) {
+ for (; i < s.length && lisxdigit(s[i]); i++) {
a = (a * 16 + luaO_hexavalue(s[i]))|0;
empty = false;
}
} else { /* decimal */
- for (; lisdigit(s[i]); i++) {
+ for (; i < s.length && lisdigit(s[i]); i++) {
let d = s[i] - 48 /* ('0').charCodeAt(0) */;
if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */
return null; /* do not accept it (as integer) */
@@ -561,7 +560,7 @@ const l_str2int = function(s) {
empty = false;
}
}
- while (lisspace(s[i])) i++; /* skip trailing spaces */
+ while (i < s.length && lisspace(s[i])) i++; /* skip trailing spaces */
if (empty || (i !== s.length && s[i] !== 0)) return null; /* something wrong in the numeral */
else {
return {