aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2017-06-08 16:41:52 +1000
committerdaurnimator <quae@daurnimator.com>2017-06-08 16:41:52 +1000
commitc129a1d95b44be63c840d95f94409d7333d440cc (patch)
tree6f187b07539a3462a045c701b1a8a20dadb12f06
parent5975411b8bed04b6bddf638b28af80a5932e79e5 (diff)
downloadfengari-c129a1d95b44be63c840d95f94409d7333d440cc.tar.gz
fengari-c129a1d95b44be63c840d95f94409d7333d440cc.tar.bz2
fengari-c129a1d95b44be63c840d95f94409d7333d440cc.zip
src/lvm.js: Simplify l_strcmp by using string hash for comparison
-rw-r--r--src/lvm.js37
1 files changed, 9 insertions, 28 deletions
diff --git a/src/lvm.js b/src/lvm.js
index 8256557..9d702fd 100644
--- a/src/lvm.js
+++ b/src/lvm.js
@@ -818,35 +818,17 @@ const LEnum = function(l, r) {
/*
** Compare two strings 'ls' x 'rs', returning an integer smaller-equal-
** -larger than zero if 'ls' is smaller-equal-larger than 'rs'.
-** The code is a little tricky because it allows '\0' in the strings.
*/
const l_strcmp = function(ls, rs) {
- let l = ls.getstr();
- let ll = ls.tsslen();
- let jl = defs.to_jsstring(l);
- let r = rs.getstr();
- let lr = rs.tsslen();
- let jr = defs.to_jsstring(r);
- for (;;) {
- let temp = jl === jr; // TODO: strcoll ?
- if (!temp) /* not equal? */
- return jl < jr ? -1 : 1; /* done */
- else { /* strings are equal up to a '\0' */
- let len = jl.length; /* index of first '\0' in both strings */
- if (len === lr) /* 'rs' is finished? */
- return len === ll ? 0 : 1; /* check 'ls' */
- else if (len === ll) /* 'ls' is finished? */
- return -1; /* 'ls' is smaller than 'rs' ('rs' is not finished) */
- /* both strings longer than 'len'; go on comparing after the '\0' */
- len++;
- l = l.slice(len);
- ll -= len;
- r = r.slice(len);
- lr -= len;
- jl = defs.to_jsstring(l);
- jr = defs.to_jsstring(r);
- }
- }
+ let l = lstring.luaS_hashlongstr(ls);
+ let r = lstring.luaS_hashlongstr(rs);
+ /* In fengari we assume string hash has same collation as byte values */
+ if (l === r)
+ return 0;
+ else if (l < r)
+ return -1;
+ else
+ return 1;
};
/*
@@ -1081,7 +1063,6 @@ module.exports.dojump = dojump;
module.exports.donextjump = donextjump;
module.exports.forlimit = forlimit;
module.exports.gettable = gettable;
-module.exports.l_strcmp = l_strcmp;
module.exports.luaV_concat = luaV_concat;
module.exports.luaV_div = luaV_div;
module.exports.luaV_equalobj = luaV_equalobj;