Example #1
0
static int str_sub (lua_State *L) {
  size_t l;
  const lua_WChar *s = luaL_checklwstring(L, 1, &l);
  sint32 start = posrelat(luaL_checklong(L, 2), l);
  sint32 end = posrelat(luaL_optlong(L, 3, -1), l);
  if (start < 1) start = 1;
  if (end > (sint32)l) end = l;
  if (start <= end)
    lua_pushlwstring(L, s+start-1, end-start+1);
  else lua_pushwliteral(L, L"");
  return 1;
}
Example #2
0
static int wstr_sub (lua_State *L) {
    size_t l;
    const lua_WChar *s = luaL_checklwstring(L, 1, &l);
    ptrdiff_t start = wstr_posrelat(luaL_checkinteger(L, 2), l);
    ptrdiff_t end = wstr_posrelat(luaL_optinteger(L, 3, -1), l);
    if (start < 1) start = 1;
    if (end > (ptrdiff_t)l) end = (ptrdiff_t)l;
    if (start <= end)
        lua_pushlwstring(L, s+start-1, end-start+1);
    else lua_pushwliteral(L, L"");
    return 1;
}