summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLua Team <team@lua.org>2015-11-30 12:00:00 +0000
committerrepogen <>2015-11-30 12:00:00 +0000
commitef91579f913d6e1c4666d10b3dc08e1da247328a (patch)
tree337b92eb258fb71bb4c7a382eaef1415b8cb92a8
parent7ccf44e218364b579c842aa7aea3ea83026abf38 (diff)
downloadlua-github-ef91579f913d6e1c4666d10b3dc08e1da247328a.tar.gz
-rw-r--r--README2
-rw-r--r--doc/manual.html10
-rw-r--r--src/lauxlib.c6
-rw-r--r--src/lauxlib.h4
-rw-r--r--src/lcode.c4
-rw-r--r--src/ldblib.c6
-rw-r--r--src/ldo.c6
-rw-r--r--src/ldo.h4
-rw-r--r--src/liolib.c20
-rw-r--r--src/llex.c4
-rw-r--r--src/llimits.h4
-rw-r--r--src/loadlib.c4
-rw-r--r--src/loslib.c4
-rw-r--r--src/lstring.c7
-rw-r--r--src/lstrlib.c20
-rw-r--r--src/ltable.c4
-rw-r--r--src/ltablib.c144
-rw-r--r--src/lvm.c6
18 files changed, 162 insertions, 97 deletions
diff --git a/README b/README
index 8c22939c..eb6247a4 100644
--- a/README
+++ b/README
@@ -1,5 +1,5 @@
-This is Lua 5.3.2, released on 18 Nov 2015.
+This is Lua 5.3.2, released on 25 Nov 2015.
For installation instructions, license details, and
further information about Lua, see doc/readme.html.
diff --git a/doc/manual.html b/doc/manual.html
index ccf1246c..60c4c98b 100644
--- a/doc/manual.html
+++ b/doc/manual.html
@@ -35,7 +35,7 @@ Freely available under the terms of the
<!-- ====================================================================== -->
<p>
-<!-- $Id: manual.of,v 1.152 2015/11/13 15:12:33 roberto Exp $ -->
+<!-- $Id: manual.of,v 1.153 2015/11/25 16:57:42 roberto Exp $ -->
@@ -3361,7 +3361,7 @@ will have as a sequence;
parameter <code>nrec</code> is a hint for how many other elements
the table will have.
Lua may use these hints to preallocate memory for the new table.
-This pre-allocation is useful for performance when you know in advance
+This preallocation is useful for performance when you know in advance
how many elements the table will have.
Otherwise you can use the function <a href="#lua_newtable"><code>lua_newtable</code></a>.
@@ -8207,9 +8207,11 @@ Options <code>c</code>, <code>d</code>,
<code>i</code>, <code>o</code>, <code>u</code>, <code>X</code>, and <code>x</code>
expect an integer.
Option <code>q</code> expects a string.
-Option <code>s</code> expects a string without embedded zeros;
+Option <code>s</code> expects a string;
if its argument is not a string,
it is converted to one following the same rules of <a href="#pdf-tostring"><code>tostring</code></a>.
+If the option has any modifier (flags, width, length),
+the string argument should not contain embedded zeros.
<p>
@@ -10823,7 +10825,7 @@ and LiteralString, see <a href="#3.1">&sect;3.1</a>.)
<P CLASS="footer">
Last update:
-Wed Nov 18 17:07:02 BRST 2015
+Wed Nov 25 15:19:10 BRST 2015
</P>
<!--
Last change: revised for Lua 5.3.2
diff --git a/src/lauxlib.c b/src/lauxlib.c
index ba51060d..5d362c35 100644
--- a/src/lauxlib.c
+++ b/src/lauxlib.c
@@ -1,5 +1,5 @@
/*
-** $Id: lauxlib.c,v 1.283 2015/10/06 16:10:22 roberto Exp $
+** $Id: lauxlib.c,v 1.284 2015/11/19 19:16:22 roberto Exp $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
@@ -471,7 +471,7 @@ static void *newbox (lua_State *L, size_t newsize) {
box->bsize = 0;
if (luaL_newmetatable(L, "LUABOX")) { /* creating metatable? */
lua_pushcfunction(L, boxgc);
- lua_setfield(L, -2, "__gc"); /* metatalbe.__gc = boxgc */
+ lua_setfield(L, -2, "__gc"); /* metatable.__gc = boxgc */
}
lua_setmetatable(L, -2);
return resizebox(L, -1, newsize);
@@ -653,7 +653,7 @@ static int errfile (lua_State *L, const char *what, int fnameindex) {
static int skipBOM (LoadF *lf) {
- const char *p = "\xEF\xBB\xBF"; /* Utf8 BOM mark */
+ const char *p = "\xEF\xBB\xBF"; /* UTF-8 BOM mark */
int c;
lf->n = 0;
do {
diff --git a/src/lauxlib.h b/src/lauxlib.h
index 0bac2467..ddb7c228 100644
--- a/src/lauxlib.h
+++ b/src/lauxlib.h
@@ -1,5 +1,5 @@
/*
-** $Id: lauxlib.h,v 1.128 2014/10/29 16:11:17 roberto Exp $
+** $Id: lauxlib.h,v 1.129 2015/11/23 11:29:43 roberto Exp $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
@@ -65,7 +65,7 @@ LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def,
LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname);
LUALIB_API int (luaL_execresult) (lua_State *L, int stat);
-/* pre-defined references */
+/* predefined references */
#define LUA_NOREF (-2)
#define LUA_REFNIL (-1)
diff --git a/src/lcode.c b/src/lcode.c
index e2f57490..7c6918fd 100644
--- a/src/lcode.c
+++ b/src/lcode.c
@@ -1,5 +1,5 @@
/*
-** $Id: lcode.c,v 2.102 2015/10/26 14:27:47 roberto Exp $
+** $Id: lcode.c,v 2.103 2015/11/19 19:16:22 roberto Exp $
** Code generator for Lua
** See Copyright Notice in lua.h
*/
@@ -816,7 +816,7 @@ static void codeexpval (FuncState *fs, OpCode op,
freeexp(fs, e1);
}
e1->u.info = luaK_codeABC(fs, op, 0, o1, o2); /* generate opcode */
- e1->k = VRELOCABLE; /* all those operations are relocable */
+ e1->k = VRELOCABLE; /* all those operations are relocatable */
luaK_fixline(fs, line);
}
}
diff --git a/src/ldblib.c b/src/ldblib.c
index 91514584..786f6cd9 100644
--- a/src/ldblib.c
+++ b/src/ldblib.c
@@ -1,5 +1,5 @@
/*
-** $Id: ldblib.c,v 1.149 2015/02/19 17:06:21 roberto Exp $
+** $Id: ldblib.c,v 1.151 2015/11/23 11:29:43 roberto Exp $
** Interface from Lua to its debug API
** See Copyright Notice in lua.h
*/
@@ -28,8 +28,8 @@ static const int HOOKKEY = 0;
/*
-** If L1 != L, L1 can be in any state, and therefore there is no
-** garanties about its stack space; any push in L1 must be
+** If L1 != L, L1 can be in any state, and therefore there are no
+** guarantees about its stack space; any push in L1 must be
** checked.
*/
static void checkstack (lua_State *L, lua_State *L1, int n) {
diff --git a/src/ldo.c b/src/ldo.c
index f3f7d4f9..95efd560 100644
--- a/src/ldo.c
+++ b/src/ldo.c
@@ -1,5 +1,5 @@
/*
-** $Id: ldo.c,v 2.149 2015/11/13 13:24:26 roberto Exp $
+** $Id: ldo.c,v 2.150 2015/11/19 19:16:22 roberto Exp $
** Stack and Call structure of Lua
** See Copyright Notice in lua.h
*/
@@ -409,7 +409,7 @@ static int moveresults (lua_State *L, const TValue *firstResult, StkId res,
case 0: break; /* nothing to move */
case 1: { /* one result needed */
if (nres == 0) /* no results? */
- firstResult = luaO_nilobject; /* ajdust with nil */
+ firstResult = luaO_nilobject; /* adjust with nil */
setobjs2s(L, res, firstResult); /* move it to proper place */
break;
}
@@ -442,7 +442,7 @@ static int moveresults (lua_State *L, const TValue *firstResult, StkId res,
/*
** Finishes a function call: calls hook if necessary, removes CallInfo,
-** moves corrent number of results to proper place; returns 0 iff call
+** moves current number of results to proper place; returns 0 iff call
** wanted multiple (variable number of) results.
*/
int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) {
diff --git a/src/ldo.h b/src/ldo.h
index 2044f825..80582dc2 100644
--- a/src/ldo.h
+++ b/src/ldo.h
@@ -1,5 +1,5 @@
/*
-** $Id: ldo.h,v 2.26 2015/11/13 13:24:26 roberto Exp $
+** $Id: ldo.h,v 2.28 2015/11/23 11:29:43 roberto Exp $
** Stack and Call structure of Lua
** See Copyright Notice in lua.h
*/
@@ -16,7 +16,7 @@
/*
** Macro to check stack size and grow stack if needed. Parameters
** 'pre'/'pos' allow the macro to preserve a pointer into the
-** stack across realalocations, doing the work only when needed.
+** stack across reallocations, doing the work only when needed.
** 'condmovestack' is used in heavy tests to force a stack reallocation
** at every check.
*/
diff --git a/src/liolib.c b/src/liolib.c
index fde29de9..a91ba391 100644
--- a/src/liolib.c
+++ b/src/liolib.c
@@ -1,5 +1,5 @@
/*
-** $Id: liolib.c,v 2.147 2015/07/15 14:40:28 roberto Exp $
+** $Id: liolib.c,v 2.148 2015/11/23 11:36:11 roberto Exp $
** Standard I/O (and system) library
** See Copyright Notice in lua.h
*/
@@ -23,18 +23,24 @@
#include "lualib.h"
-#if !defined(l_checkmode)
+
/*
-** Check whether 'mode' matches '[rwa]%+?b?'.
** Change this macro to accept other modes for 'fopen' besides
** the standard ones.
*/
+#if !defined(l_checkmode)
+
+/* accepted extensions to 'mode' in 'fopen' */
+#if !defined(L_MODEEXT)
+#define L_MODEEXT "b"
+#endif
+
+/* Check whether 'mode' matches '[rwa]%+?[L_MODEEXT]*' */
#define l_checkmode(mode) \
(*mode != '\0' && strchr("rwa", *(mode++)) != NULL && \
- (*mode != '+' || ++mode) && /* skip if char is '+' */ \
- (*mode != 'b' || ++mode) && /* skip if char is 'b' */ \
- (*mode == '\0'))
+ (*mode != '+' || (++mode, 1)) && /* skip if char is '+' */ \
+ (strspn(mode, L_MODEEXT) == strlen(mode)))
#endif
@@ -469,7 +475,7 @@ static int read_line (lua_State *L, FILE *f, int chop) {
int c = '\0';
luaL_buffinit(L, &b);
while (c != EOF && c != '\n') { /* repeat until end of line */
- char *buff = luaL_prepbuffer(&b); /* pre-allocate buffer */
+ char *buff = luaL_prepbuffer(&b); /* preallocate buffer */
int i = 0;
l_lockfile(f); /* no memory errors can happen inside the lock */
while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n')
diff --git a/src/llex.c b/src/llex.c
index 10ff441b..16ea3ebe 100644
--- a/src/llex.c
+++ b/src/llex.c
@@ -1,5 +1,5 @@
/*
-** $Id: llex.c,v 2.94 2015/10/28 18:51:47 roberto Exp $
+** $Id: llex.c,v 2.95 2015/11/19 19:16:22 roberto Exp $
** Lexical Analyzer
** See Copyright Notice in lua.h
*/
@@ -275,7 +275,7 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) {
/*
-** skip a sequence '[=*[' or ']=*]'; if sequence is wellformed, return
+** skip a sequence '[=*[' or ']=*]'; if sequence is well formed, return
** its number of '='s; otherwise, return a negative number (-1 iff there
** are no '='s after initial bracket)
*/
diff --git a/src/llimits.h b/src/llimits.h
index 0791f0ec..f21377fe 100644
--- a/src/llimits.h
+++ b/src/llimits.h
@@ -1,5 +1,5 @@
/*
-** $Id: llimits.h,v 1.140 2015/10/21 18:40:47 roberto Exp $
+** $Id: llimits.h,v 1.141 2015/11/19 19:16:22 roberto Exp $
** Limits, basic types, and some other 'installation-dependent' definitions
** See Copyright Notice in lua.h
*/
@@ -207,7 +207,7 @@ typedef unsigned long Instruction;
/*
-** macros that are executed whenether program enters the Lua core
+** macros that are executed whenever program enters the Lua core
** ('lua_lock') and leaves the core ('lua_unlock')
*/
#if !defined(lua_lock)
diff --git a/src/loadlib.c b/src/loadlib.c
index bbf8f67a..79119287 100644
--- a/src/loadlib.c
+++ b/src/loadlib.c
@@ -1,5 +1,5 @@
/*
-** $Id: loadlib.c,v 1.126 2015/02/16 13:14:33 roberto Exp $
+** $Id: loadlib.c,v 1.127 2015/11/23 11:30:45 roberto Exp $
** Dynamic library loader for Lua
** See Copyright Notice in lua.h
**
@@ -732,7 +732,7 @@ static void createsearcherstable (lua_State *L) {
int i;
/* create 'searchers' table */
lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0);
- /* fill it with pre-defined searchers */
+ /* fill it with predefined searchers */
for (i=0; searchers[i] != NULL; i++) {
lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */
lua_pushcclosure(L, searchers[i], 1);
diff --git a/src/loslib.c b/src/loslib.c
index 8a46c53b..7dae5336 100644
--- a/src/loslib.c
+++ b/src/loslib.c
@@ -1,5 +1,5 @@
/*
-** $Id: loslib.c,v 1.59 2015/07/06 15:16:51 roberto Exp $
+** $Id: loslib.c,v 1.60 2015/11/19 19:16:22 roberto Exp $
** Standard Operating System library
** See Copyright Notice in lua.h
*/
@@ -215,7 +215,7 @@ static int getfield (lua_State *L, const char *key, int d, int delta) {
if (!isnum) { /* field is not a number? */
if (t != LUA_TNIL) /* some other value? */
return luaL_error(L, "field '%s' not an integer", key);
- else if (d < 0) /* abssent field; no default? */
+ else if (d < 0) /* absent field; no default? */
return luaL_error(L, "field '%s' missing in date table", key);
res = d;
}
diff --git a/src/lstring.c b/src/lstring.c
index 44f17ac0..9351766f 100644
--- a/src/lstring.c
+++ b/src/lstring.c
@@ -1,5 +1,5 @@
/*
-** $Id: lstring.c,v 2.55 2015/11/03 15:36:01 roberto Exp $
+** $Id: lstring.c,v 2.56 2015/11/23 11:32:51 roberto Exp $
** String table (keeps all strings handled by Lua)
** See Copyright Notice in lua.h
*/
@@ -48,10 +48,9 @@ int luaS_eqlngstr (TString *a, TString *b) {
unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
unsigned int h = seed ^ cast(unsigned int, l);
- size_t l1;
size_t step = (l >> LUAI_HASHLIMIT) + 1;
- for (l1 = l; l1 >= step; l1 -= step)
- h = h ^ ((h<<5) + (h>>2) + cast_byte(str[l1 - 1]));
+ for (; l >= step; l -= step)
+ h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1]));
return h;
}
diff --git a/src/lstrlib.c b/src/lstrlib.c
index 65c4c750..fe30e34b 100644
--- a/src/lstrlib.c
+++ b/src/lstrlib.c
@@ -1,5 +1,5 @@
/*
-** $Id: lstrlib.c,v 1.237 2015/10/29 15:11:41 roberto Exp $
+** $Id: lstrlib.c,v 1.239 2015/11/25 16:28:17 roberto Exp $
** Standard library for string operations and pattern-matching
** See Copyright Notice in lua.h
*/
@@ -1031,14 +1031,18 @@ static int str_format (lua_State *L) {
case 's': {
size_t l;
const char *s = luaL_tolstring(L, arg, &l);
- if (!strchr(form, '.') && l >= 100) {
- /* no precision and string is too long to be formatted;
- keep original string */
- luaL_addvalue(&b);
- }
+ if (form[2] == '\0') /* no modifiers? */
+ luaL_addvalue(&b); /* keep entire string */
else {
- nb = l_sprintf(buff, MAX_ITEM, form, s);
- lua_pop(L, 1); /* remove result from 'luaL_tolstring' */
+ luaL_argcheck(L, l == strlen(s), arg, "string contains zeros");
+ if (!strchr(form, '.') && l >= 100) {
+ /* no precision and string is too long to be formatted */
+ luaL_addvalue(&b); /* keep entire string */
+ }
+ else { /* format the string into 'buff' */
+ nb = l_sprintf(buff, MAX_ITEM, form, s);
+ lua_pop(L, 1); /* remove result from 'luaL_tolstring' */
+ }
}
break;
}
diff --git a/src/ltable.c b/src/ltable.c
index aff36564..7e15b71b 100644
--- a/src/ltable.c
+++ b/src/ltable.c
@@ -1,5 +1,5 @@
/*
-** $Id: ltable.c,v 2.116 2015/11/03 18:35:21 roberto Exp $
+** $Id: ltable.c,v 2.117 2015/11/19 19:16:22 roberto Exp $
** Lua tables (hash)
** See Copyright Notice in lua.h
*/
@@ -85,7 +85,7 @@ static const Node dummynode_ = {
/*
** Hash for floating-point numbers.
** The main computation should be just
-** n = frepx(n, &i); return (n * INT_MAX) + i
+** n = frexp(n, &i); return (n * INT_MAX) + i
** but there are some numerical subtleties.
** In a two-complement representation, INT_MAX does not has an exact
** representation as a float, but INT_MIN does; because the absolute
diff --git a/src/ltablib.c b/src/ltablib.c
index 867ff9df..b3c9a7c5 100644
--- a/src/ltablib.c
+++ b/src/ltablib.c
@@ -1,5 +1,5 @@
/*
-** $Id: ltablib.c,v 1.85 2015/11/12 18:07:25 roberto Exp $
+** $Id: ltablib.c,v 1.90 2015/11/25 12:48:57 roberto Exp $
** Library for Table Manipulation
** See Copyright Notice in lua.h
*/
@@ -12,7 +12,7 @@
#include <limits.h>
#include <stddef.h>
-#include <time.h>
+#include <string.h>
#include "lua.h"
@@ -24,10 +24,10 @@
** Operations that an object must define to mimic a table
** (some functions only need some of them)
*/
-#define TAB_R 1
-#define TAB_W 2
-#define TAB_L 4
-#define TAB_RW (TAB_R | TAB_W)
+#define TAB_R 1 /* read */
+#define TAB_W 2 /* write */
+#define TAB_L 4 /* length */
+#define TAB_RW (TAB_R | TAB_W) /* read/write */
#define aux_getn(L,n,w) (checktab(L, n, (w) | TAB_L), luaL_len(L, n))
@@ -51,7 +51,7 @@ static void checktab (lua_State *L, int arg, int what) {
(!(what & TAB_W) || checkfield(L, "__newindex", ++n)) &&
(!(what & TAB_L) || checkfield(L, "__len", ++n))) {
lua_pop(L, n); /* pop metatable and tested metamethods */
-}
+ }
else
luaL_argerror(L, arg, "table expected"); /* force an error */
}
@@ -168,11 +168,10 @@ static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) {
static int tconcat (lua_State *L) {
luaL_Buffer b;
- size_t lsep;
- lua_Integer i;
lua_Integer last = aux_getn(L, 1, TAB_R);
+ size_t lsep;
const char *sep = luaL_optlstring(L, 2, "", &lsep);
- i = luaL_optinteger(L, 3, 1);
+ lua_Integer i = luaL_optinteger(L, 3, 1);
last = luaL_opt(L, luaL_checkinteger, 4, last);
luaL_buffinit(L, &b);
for (; i < last; i++) {
@@ -206,10 +205,9 @@ static int pack (lua_State *L) {
static int unpack (lua_State *L) {
- lua_Integer i, e;
lua_Unsigned n;
- i = luaL_optinteger(L, 2, 1);
- e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1));
+ lua_Integer i = luaL_optinteger(L, 2, 1);
+ lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1));
if (i > e) return 0; /* empty range */
n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */
if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n)))
@@ -233,24 +231,68 @@ static int unpack (lua_State *L) {
** =======================================================
*/
-static void set2 (lua_State *L, int i, int j) {
+
+/*
+** Produce a "random" 'unsigned int' to randomize pivot choice. This
+** macro is used only when 'sort' detects a big imbalance in the result
+** of a partition. (If you don't want/need this "randomness", ~0 is a
+** good choice.)
+*/
+#if !defined(l_randomizePivot) /* { */
+
+#include <time.h>
+
+/* size of 'e' measured in number of 'unsigned int's */
+#define sof(e) (sizeof(e) / sizeof(unsigned int))
+
+/*
+** Use 'time' and 'clock' as sources of "randomness". Because we don't
+** know the types 'clock_t' and 'time_t', we cannot cast them to
+** anything without risking overflows. A safe way to use their values
+** is to copy them to an array of a known type and use the array values.
+*/
+static unsigned int l_randomizePivot (void) {
+ clock_t c = clock();
+ time_t t = time(NULL);
+ unsigned int buff[sof(c) + sof(t)];
+ unsigned int i, rnd = 0;
+ memcpy(buff, &c, sof(c) * sizeof(unsigned int));
+ memcpy(buff + sof(c), &t, sof(t) * sizeof(unsigned int));
+ for (i = 0; i < sof(buff); i++)
+ rnd += buff[i];
+ return rnd;
+}
+
+#endif /* } */
+
+
+/* arrays larger than 'RANLIMIT' may use randomized pivots */
+#define RANLIMIT 100u
+
+
+static void set2 (lua_State *L, unsigned int i, unsigned int j) {
lua_seti(L, 1, i);
lua_seti(L, 1, j);
}
+
+/*
+** Return true iff value at stack index 'a' is less than the value at
+** index 'b' (according to the order of the sort).
+*/
static int sort_comp (lua_State *L, int a, int b) {
- if (!lua_isnil(L, 2)) { /* function? */
+ if (lua_isnil(L, 2)) /* no function? */
+ return lua_compare(L, a, b, LUA_OPLT); /* a < b */
+ else { /* function */
int res;
- lua_pushvalue(L, 2);
+ lua_pushvalue(L, 2); /* push function */
lua_pushvalue(L, a-1); /* -1 to compensate function */
lua_pushvalue(L, b-2); /* -2 to compensate function and 'a' */
- lua_call(L, 2, 1);
- res = lua_toboolean(L, -1);
- lua_pop(L, 1);
+ lua_call(L, 2, 1); /* call function */
+ res = lua_toboolean(L, -1); /* get result */
+ lua_pop(L, 1); /* pop result */
return res;
}
- else /* a < b? */
- return lua_compare(L, a, b, LUA_OPLT);
}
@@ -261,9 +303,10 @@ static int sort_comp (lua_State *L, int a, int b) {
** Pos-condition: a[lo .. i - 1] <= a[i] == P <= a[i + 1 .. up]
** returns 'i'.
*/
-static int partition (lua_State *L, int lo, int up) {
- int i = lo; /* will be incremented before first use */
- int j = up - 1; /* will be decremented before first use */
+static unsigned int partition (lua_State *L, unsigned int lo,
+ unsigned int up) {
+ unsigned int i = lo; /* will be incremented before first use */
+ unsigned int j = up - 1; /* will be decremented before first use */
/* loop invariant: a[lo .. i] <= P <= a[j .. up] */
for (;;) {
/* next loop: repeat ++i while a[i] < P */
@@ -294,23 +337,27 @@ static int partition (lua_State *L, int lo, int up) {
/*
-** Choose a "random" pivot in the middle part of the interval [lo, up].
-** Use 'time' and 'clock' as sources of "randomness".
+** Choose an element in the middle (2nd-3th quarters) of [lo,up]
+** "randomized" by 'rnd'
*/
-static int choosePivot (int lo, int up) {
- unsigned int t = (unsigned int)(unsigned long)time(NULL); /* time */
- unsigned int c = (unsigned int)(unsigned long)clock(); /* clock */
+static unsigned int choosePivot (unsigned int lo, unsigned int up,
+ unsigned int rnd) {
unsigned int r4 = (unsigned int)(up - lo) / 4u; /* range/4 */
- unsigned int p = (c + t) % (r4 * 2) + (lo + r4);
+ unsigned int p = rnd % (r4 * 2) + (lo + r4);
lua_assert(lo + r4 <= p && p <= up - r4);
- return (int)p;
+ return p;
}
-static void auxsort (lua_State *L, int lo, int up) {
+/*
+** QuickSort algorithm (recursive function)
+*/
+static void auxsort (lua_State *L, unsigned int lo, unsigned int up,
+ unsigned int rnd) {
while (lo < up) { /* loop for tail recursion */
- int p;
- /* sort elements 'lo', '(lo + up)/2' and 'up' */
+ unsigned int p; /* Pivot index */
+ unsigned int n; /* to be used later */
+ /* sort elements 'lo', 'p', and 'up' */
lua_geti(L, 1, lo);
lua_geti(L, 1, up);
if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */
@@ -319,10 +366,10 @@ static void auxsort (lua_State *L, int lo, int up) {
lua_pop(L, 2); /* remove both values */
if (up - lo == 1) /* only 2 elements? */
return; /* already sorted */
- if (up - lo < 100) /* small interval? */
+ if (up - lo < RANLIMIT || rnd == 0) /* small interval or no randomize? */
p = (lo + up)/2; /* middle element is a good pivot */
else /* for larger intervals, it is worth a random pivot */
- p = choosePivot(lo, up);
+ p = choosePivot(lo, up, rnd);
lua_geti(L, 1, p);
lua_geti(L, 1, lo);
if (sort_comp(L, -2, -1)) /* a[p] < a[lo]? */
@@ -344,24 +391,31 @@ static void auxsort (lua_State *L, int lo, int up) {
p = partition(L, lo, up);
/* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */
if (p - lo < up - p) { /* lower interval is smaller? */
- auxsort(L, lo, p - 1); /* call recursively for lower interval */
+ auxsort(L, lo, p - 1, rnd); /* call recursively for lower interval */
+ n = p - lo; /* size of smaller interval */
lo = p + 1; /* tail call for [p + 1 .. up] (upper interval) */
}
else {
- auxsort(L, p + 1, up); /* call recursively for upper interval */
+ auxsort(L, p + 1, up, rnd); /* call recursively for upper interval */
+ n = up - p; /* size of smaller interval */
up = p - 1; /* tail call for [lo .. p - 1] (lower interval) */
}
- } /* tail call auxsort(L, lo, up) */
+ if ((up - lo) / 128u > n) /* partition too imbalanced? */
+ rnd = l_randomizePivot(); /* try a new randomization */
+ } /* tail call auxsort(L, lo, up, rnd) */
}
static int sort (lua_State *L) {
- int n = (int)aux_getn(L, 1, TAB_RW);
- luaL_checkstack(L, 50, ""); /* assume array is smaller than 2^50 */
- if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
- luaL_checktype(L, 2, LUA_TFUNCTION);
- lua_settop(L, 2); /* make sure there are two arguments */
- auxsort(L, 1, n);
+ lua_Integer n = aux_getn(L, 1, TAB_RW);
+ if (n > 1) { /* non-trivial interval? */
+ luaL_argcheck(L, n < INT_MAX, 1, "array too big");
+ luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */
+ if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
+ luaL_checktype(L, 2, LUA_TFUNCTION); /* must be a function */
+ lua_settop(L, 2); /* make sure there are two arguments */
+ auxsort(L, 1, (unsigned int)n, 0u);
+ }
return 0;
}
diff --git a/src/lvm.c b/src/lvm.c
index 40435c7f..aba7ace8 100644
--- a/src/lvm.c
+++ b/src/lvm.c
@@ -1,5 +1,5 @@
/*
-** $Id: lvm.c,v 2.263 2015/11/17 16:00:28 roberto Exp $
+** $Id: lvm.c,v 2.265 2015/11/23 11:30:45 roberto Exp $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@@ -1051,7 +1051,7 @@ void luaV_execute (lua_State *L) {
StkId rb;
L->top = base + c + 1; /* mark the end of concat operands */
Protect(luaV_concat(L, c - b + 1));
- ra = RA(i); /* 'luav_concat' may invoke TMs and move the stack */
+ ra = RA(i); /* 'luaV_concat' may invoke TMs and move the stack */
rb = base + b;
setobjs2s(L, ra, rb);
checkGC(L, (ra >= rb ? ra + 1 : rb));
@@ -1254,7 +1254,7 @@ void luaV_execute (lua_State *L) {
h = hvalue(ra);
last = ((c-1)*LFIELDS_PER_FLUSH) + n;
if (last > h->sizearray) /* needs more space? */
- luaH_resizearray(L, h, last); /* pre-allocate it at once */
+ luaH_resizearray(L, h, last); /* preallocate it at once */
for (; n > 0; n--) {
TValue *val = ra+n;
luaH_setint(L, h, last--, val);