Support the '#' string as index for wrapper vector insert at end.

develop
Alexander Gavrilov 2012-04-17 12:15:45 +04:00
parent 3beb2ebf25
commit 378a1fb914
3 changed files with 14 additions and 5 deletions

@ -209,7 +209,7 @@ Implemented features:
* ``ref:insert(index,item)``
Inserts a new item at the specified index. To add at the end,
use ``#ref`` as index.
use ``#ref``, or just ``'#'`` as index.
* ``ref:erase(index)``

@ -516,7 +516,7 @@ possible.</p>
</li>
<li><p class="first"><tt class="docutils literal">ref:insert(index,item)</tt></p>
<p>Inserts a new item at the specified index. To add at the end,
use <tt class="docutils literal">#ref</tt> as index.</p>
use <tt class="docutils literal">#ref</tt>, or just <tt class="docutils literal">'#'</tt> as index.</p>
</li>
<li><p class="first"><tt class="docutils literal">ref:erase(index)</tt></p>
<p>Removes the element at the given valid index.</p>

@ -733,8 +733,18 @@ static int lookup_container_field(lua_State *state, int field, const char *mode
* Index verification: number and in range.
*/
static int check_container_index(lua_State *state, int len,
int fidx, int iidx, const char *mode)
int fidx, int iidx, const char *mode,
bool is_insert = false)
{
if (is_insert && len >= 0)
{
if (lua_type(state, iidx) == LUA_TSTRING
&& strcmp(lua_tostring(state, iidx), "#") == 0)
return len;
len++;
}
if (!lua_isnumber(state, iidx))
field_error(state, fidx, "invalid index", mode);
@ -867,8 +877,7 @@ static int method_container_insert(lua_State *state)
auto id = (container_identity*)lua_touserdata(state, UPVAL_CONTAINER_ID);
int len = id->lua_item_count(state, ptr, container_identity::COUNT_LEN);
if (len >= 0) len++;
int idx = check_container_index(state, len, UPVAL_METHOD_NAME, 2, "call");
int idx = check_container_index(state, len, UPVAL_METHOD_NAME, 2, "call", true);
if (!id->lua_insert(state, UPVAL_METHOD_NAME, ptr, idx, 3))
field_error(state, UPVAL_METHOD_NAME, "not supported", "call");