static int str_byte (lua_State *L) {
  size_t l;
  const char *s = luaL_checklstring(L, 1, &l);
  sint32 pos = posrelat(luaL_optlong(L, 2, 1), l);
  if (pos <= 0 || (size_t)(pos) > l)  /* index out of range? */
    return 0;  /* no answer */
  lua_pushnumber(L, uchar(s[pos-1]));
  return 1;
}
Beispiel #2
0
static int str_byte (lua_State *L)
{
	size_t l;
	const char *s = luaL_checklstring(L, 1, &l);
	ptrdiff_t posi = posrelat(luaL_optinteger(L, 2, 1), l);
	ptrdiff_t pose = posrelat(luaL_optinteger(L, 3, posi), l);
	int n, i;
	if (posi <= 0) posi = 1;
	if ((size_t)pose > l) pose = l;
	if (posi > pose) return 0;  /* empty interval; return no values */
	n = (int)(pose -  posi + 1);
	if (posi + n <= pose)  /* overflow? */
		luaL_error(L, "string slice too long");
	luaL_checkstack(L, n, "string slice too long");
	for (i=0; i<n; i++)
		lua_pushinteger(L, uchar(s[posi+i-1]));
	return n;
}
static int str_find_aux (LuaMatchState *ms, int find, const char *s, size_t ls,
                         const char *p, size_t lp, size_t init, int raw_find) {
  int result = 0; /* not found */
  ms->error = NULL;
  init = posrelat(init, ls);
  if (init < 0) init = 0;
  else if (init > ls + 1) {  /* start after string's end? */
    return 0; /* cannot find anything */
  }
  /* explicit request or no special characters? */
  if (find && (raw_find || nospecials(p, lp))) {
    /* do a plain search */
    const char *s2 = lmemfind(s + init, ls - init + 1, p, lp);
    if (s2) {
      int start_pos = ((int)(s2 - s));
      ms->level = 1;
      ms->capture[0].len = CAP_POSITION;
      ms->capture[0].init = (const char *)start_pos;
      result = start_pos + lp;
    }
  }
  else {
    const char *s1 = s + init;
    int anchor = (*p == '^');
    if (anchor) {
      p++; lp--;  /* skip anchor character */
    }
    ms->src_init = s;
    ms->src_end = s + ls;
    ms->p_end = p + lp;
    do {
      const char *res;
      ms->level = 0;
      if ((res=match(ms, s1, p)) != NULL) {
          result = (int)(res - s);
          goto eofunc;
      }
    } while (s1++ < ms->src_end && !anchor);
  }

eofunc:

  if(result){
      int i;
      for(i=0; i<ms->level; ++i){
          if(ms->capture[i].len == CAP_UNFINISHED){
              ms->error = "unfinished capture";
              return 0;
          }
      }
  }
  return result;
}
static int str_find_aux (lua_State *L, int find) {
  size_t ls, lp;
  const char *s = luaL_checklstring(L, 1, &ls);
  const char *p = luaL_checklstring(L, 2, &lp);
  size_t init = posrelat(luaL_optinteger(L, 3, 1), ls);
  if (init < 1) init = 1;
  else if (init > ls + 1) {  /* start after string's end? */
    lua_pushnil(L);  /* cannot find anything */
    return 1;
  }
  /* explicit request or no special characters? */
  if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) {
    /* do a plain search */
    const char *s2 = lmemfind(s + init - 1, ls - init + 1, p, lp);
    if (s2) {
      lua_pushinteger(L, s2 - s + 1);
      lua_pushinteger(L, s2 - s + lp);
      return 2;
    }
  }
  else {
    MatchState ms;
    const char *s1 = s + init - 1;
    int anchor = (*p == '^');
    if (anchor) {
      p++; lp--;  /* skip anchor character */
    }
    ms.L = L;
    ms.matchdepth = MAXCCALLS;
    ms.src_init = s;
    ms.src_end = s + ls;
    ms.p_end = p + lp;
    do {
      const char *res;
      ms.level = 0;
      lua_assert(ms.matchdepth == MAXCCALLS);
      if ((res=match(&ms, s1, p)) != NULL) {
        if (find) {
          lua_pushinteger(L, s1 - s + 1);  /* start */
          lua_pushinteger(L, res - s);   /* end */
          return push_captures(&ms, NULL, 0) + 2;
        }
        else
          return push_captures(&ms, s1, res);
      }
    } while (s1++ < ms.src_end && !anchor);
  }
  lua_pushnil(L);  /* not found */
  return 1;
}
Beispiel #5
0
static int t_slice (lua_State *L) {
  int n = aux_getn(L, 1);
  int start = posrelat(luaL_checkint(L, 2), n);
  int end = posrelat(luaL_optint(L, 3, -1), n);
  int skip = luaL_optint(L, 4, 1);
  luaL_argcheck(L, skip != 0, 4, "step argument must not be zero");
  if (start < 1) start = 1;
  if (end > n) end = n;

  int i, ni;
  if (skip > 0)
    ni = (end >= start) ? ((end - start) / skip) + 1 : 0;
  else  /* skip < 0 */
    ni = (start >= end) ? ((start - end) / -skip) + 1 : 0;
  lua_createtable(L, ni, 0);
  for (i = 1; i <= ni; ++i) {
    lua_rawgeti(L, 1, start);
    lua_rawseti(L, -2, i);
    start += skip;
  }

  return 1;
}
Beispiel #6
0
Datei: buffer.c Projekt: vrld/LHC
static int lhc_buffer_set(lua_State *L)
{
	float *buf  = lhc_checkbuffer(L, 1);
	size_t size = lhc_buffer_nsamples(L, 1);
	size_t pos  = posrelat(luaL_checkinteger(L, 2), size);
	float val   = luaL_checknumber(L, 3);

	if (pos < 1 || pos > size)
		return luaL_error(L, "Index out of bounds: %lu", pos);

	buf[pos-1] = val;

	lua_settop(L, 1);
	return 1;
}
Beispiel #7
0
Datei: buffer.c Projekt: vrld/LHC
/* check implementation of str_byte in lua sourcecode, lstrlib.c:110 */
static int lhc_buffer_get(lua_State *L)
{
	float *buf  = lhc_checkbuffer(L, 1);
	size_t size = lhc_buffer_nsamples(L, 1);
	size_t posi = posrelat(luaL_checkinteger(L, 2), size);
	size_t pose = posrelat(luaL_optinteger(L, 3, posi), size);

	if (posi <= 0)
		posi = 1;

	if (pose > size)
		pose = size;

	if (posi > pose)
		return 0;

	int n = (int)(pose - posi + 1);
	luaL_checkstack(L, n, "buffer slice too long");

	for (size_t i = posi; i <= pose; ++i)
		lua_pushnumber(L, buf[i - 1]);

	return n;
}
Beispiel #8
0
Datei: buffer.c Projekt: vrld/LHC
static int lhc_buffer_map(lua_State *L)
{
	float *buf  = lhc_checkbuffer(L, 1);
	size_t size = lhc_buffer_nsamples(L, 1);
	size_t posi = 0, pose = size;

	int stackpos_function = 2;
	if (lua_isnumber(L, 2))
	{
		posi = posrelat(lua_tointeger(L, 2), size);
		stackpos_function = 3;
	}

	if (lua_isnumber(L, 3))
	{
		pose = posrelat(lua_tointeger(L, 3), size);
		stackpos_function = 4;
	}

	if (!lua_isfunction(L, stackpos_function))
		return luaL_typerror(L, stackpos_function, "function");

	for (; posi <= pose; ++posi)
	{
		lua_pushvalue(L, stackpos_function);
		lua_pushinteger(L, posi);
		lua_pushnumber(L, buf[posi-1]);
		lua_call(L, 2, 1);

		buf[posi-1] = lua_tonumber(L, -1);
		lua_pop(L, 1);
	}

	lua_settop(L, 1);
	return 1;
}
Beispiel #9
0
//mod by nirenr
static int gfind_aux (lua_State *L) {
  size_t ls, lp;
  const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
  const char *p = lua_tolstring(L, lua_upvalueindex(2), &lp);
  lua_Integer init = posrelat(luaL_optinteger(L, lua_upvalueindex(3), 1), ls);
  if (init < 1) init = 1;
  else if (init > (lua_Integer)ls + 1) {  /* start after string's end? */
    return 0;  /* cannot find anything */
  }
  /* explicit request or no special characters? */
  if (lua_toboolean(L, lua_upvalueindex(4)) || nospecials(p, lp)) {
    /* do a plain search */
    const char *s2 = lmemfind(s + init - 1, ls - (size_t)init + 1, p, lp);
    if (s2) {
      lua_pushinteger(L, (s2 - s) + 1);
      lua_pushinteger(L, (s2 - s) + lp);
      lua_pushinteger(L,(s2 - s) + lp + 1);
      lua_replace(L, lua_upvalueindex(3));
      return 2;
    }
  }
  else {
    MatchState ms;
    const char *s1 = s + init - 1;
    int anchor = (*p == '^');
    if (anchor) {
      p++; lp--;  /* skip anchor character */
    }
    ms.L = L;
    ms.matchdepth = MAXCCALLS;
    ms.src_init = s;
    ms.src_end = s + ls;
    ms.p_end = p + lp;
    do {
      const char *res;
      ms.level = 0;
      lua_assert(ms.matchdepth == MAXCCALLS);
      if ((res=match(&ms, s1, p)) != NULL) {
        lua_pushinteger(L, (s1 - s) + 1);  /* start */
        lua_pushinteger(L, res - s);   /* end */
        lua_pushinteger(L, res - s + 1);
        lua_replace(L, lua_upvalueindex(3));
        return push_captures(&ms, NULL, 0) + 2;
      }
    } while (s1++ < ms.src_end && !anchor);
  }
  return 0;  /* not found */
}
Beispiel #10
0
static int str_find_aux (lua_State *L, int mode) {  /* EXT */
  size_t ls, lp;
  const char *s = luaL_checklstring(L, 1, &ls);
  const char *p = luaL_checklstring(L, 2, &lp);
  lua_Integer init = posrelat(luaL_optinteger(L, 3, 1), ls);
  if (init < 1) init = 1;
  else if (init > (lua_Integer)ls + 1) {  /* start after string's end? */
    lua_pushnil(L);  /* cannot find anything */
    return 1;
  }
  /* explicit request or no special characters? */  /* EXT */
  if (mode == MODE_FIND && (lua_toboolean(L, 4) || nospecials(p, lp))) {
    /* do a plain search */
    const char *s2 = lmemfind(s + init - 1, ls - (size_t)init + 1, p, lp);
    if (s2) {
      lua_pushinteger(L, (s2 - s) + 1);
      lua_pushinteger(L, (s2 - s) + lp);
      return 2;
    }
  }
  else {
    MatchState ms;
    const char *s1 = s + init - 1;
    int anchor = (*p == '^');
    prepstate(&ms, L, s, ls, p, lp);  /* EXT (moved before anchor check) */
    if (anchor)
      p++;  /* skip anchor character */  /* EXT */
    do {
      const char *res;
      reprepstate(&ms);
      if ((res=match(&ms, s1, p)) != NULL) {
        if (mode == MODE_FIND) {  /* EXT */
          lua_pushinteger(L, (s1 - s) + 1);  /* start */
          lua_pushinteger(L, res - s);   /* end */
          return push_captures(&ms, NULL, 0) + 2;
        }
        else if (mode == MODE_MATCH)  /* EXT */
          return push_captures(&ms, s1, res);
        else {  /* EXT */
          return build_result_table(&ms, s1, res);
        }
      }
    } while (s1++ < ms.src_end && !anchor);
  }
  lua_pushnil(L);  /* not found */
  return 1;
}
Beispiel #11
0
static int str_find_aux (lua_State *L, int find) {
  size_t l1, l2;
  const char *s = luaL_checklstring(L, 1, &l1);
  const char *p = luaL_checklstring(L, 2, &l2);
  ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1;
  if (init < 0) init = 0;
  else if ((size_t)(init) > l1) init = (ptrdiff_t)l1;
  if (find && (lua_toboolean(L, 4) ||  /* explicit request? */
      strpbrk(p, SPECIALS) == NULL)) {  /* or no special characters? */
    /* do a plain search */
    const char *s2 = lmemfind(s+init, l1-init, p, l2);
    if (s2) {
      lua_pushinteger(L, s2-s+1);
      lua_pushinteger(L, s2-s+l2);
      return 2;
    }
  }
  else {
    MatchState ms;
    int anchor = (*p == '^') ? (p++, 1) : 0;
    const char *s1=s+init;
    ms.L = L;
    ms.src_init = s;
    ms.src_end = s+l1;
    do {
      const char *res;
      ms.level = 0;
      if ((res=match(&ms, s1, p)) != NULL) {
        if (find) {
          lua_pushinteger(L, s1-s+1);  /* start */
          lua_pushinteger(L, res-s);   /* end */
          return push_captures(&ms, NULL, 0) + 2;
        }
        else
          return push_captures(&ms, s1, res);
      }
    } while (s1++ < ms.src_end && !anchor);
  }
  lua_pushnil(L);  /* not found */
  return 1;
}
Beispiel #12
0
static int str_find (lua_State *L) {
  size_t l1, l2;
  const lua_WChar *s = luaL_checklwstring(L, 1, &l1);
  const lua_WChar *p = luaL_checklwstring(L, 2, &l2);
  sint32 init = posrelat(luaL_optlong(L, 3, 1), l1) - 1;
  luaL_argcheck(L, 0 <= init && (size_t)(init) <= l1, 3, "out of range");
  if (lua_toboolean(L, 4) ||  /* explicit request? */
      lua_WChar_pbrk(p, SPECIALS) == NULL) {  /* or no special characters? */
    /* do a plain search */
    const lua_WChar *s2 = lmemfind(s+init, l1-init, p, l2);
    if (s2) {
      lua_pushnumber(L, s2-s+1);
      lua_pushnumber(L, s2-s+l2);
      return 2;
    }
  }
  else {
    MatchState ms;
    int anchor = (*p == '^') ? (p++, 1) : 0;
    const lua_WChar *s1=s+init;
    ms.L = L;
    ms.src_init = s;
    ms.src_end = s+l1;
    do {
      const lua_WChar *res;
      ms.level = 0;
      if ((res=match(&ms, s1, p)) != NULL) {
        lua_pushnumber(L, s1-s+1);  /* start */
        lua_pushnumber(L, res-s);   /* end */
        return push_captures(&ms, NULL, 0) + 2;
      }
    } while (s1++<ms.src_end && !anchor);
  }
  lua_pushnil(L);  /* not found */
  return 1;
}
Beispiel #13
0
Datei: buffer.c Projekt: vrld/LHC
static int lhc_buffer_insert(lua_State *L)
{
	float *buf      = lhc_checkbuffer(L, 1);
	size_t size_buf = lhc_buffer_nsamples(L, 1);
	size_t posi     = posrelat(luaL_checkinteger(L, 2), size_buf);

	float *insert      = NULL;
	size_t size_insert = 0;
	int should_free = 0;
	int type = lua_type(L, 3);
	if (lua_isbuffer(L, 3))
	{
		insert      = (float *)lua_touserdata(L, 3);
		size_insert = lhc_buffer_nsamples(L, 3);
	}
	else if (LUA_TSTRING == type)
	{
		insert       = (float*)lua_tolstring(L, 3, &size_insert);
		size_insert /= sizeof(float);
	}
	else if (LUA_TTABLE == type)
	{
		should_free = 1;
		size_insert = lua_objlen(L, 3);
		insert = malloc(size_insert * sizeof(float));
		for (size_t i = 0; i < size_insert; ++i)
		{
			lua_rawgeti(L, 3, i+1);
			insert[i] = lua_tonumber(L, -1);
			lua_pop(L, 1);
		}
	}
	else if (LUA_TNUMBER == type)
	{
		should_free = 1;
		if (lua_isfunction(L, 4))
		{
			size_insert = lua_tointeger(L, 3);
			insert = malloc(size_insert * sizeof(float));
			for (size_t i = 0; i < size_insert; ++i)
			{
				lua_pushvalue(L, 4);
				lua_pushinteger(L, i+posi+1);
				lua_call(L, 1, 1);
				insert[i] = lua_tonumber(L, -1);
				lua_pop(L, 1);
			}
		}
		else
		{
			size_insert = lua_gettop(L) - 2;
			insert = malloc(size_insert * sizeof(float));
			for (size_t i = 0; i < size_insert; ++i)
				insert[i] = lua_tonumber(L, 3 + i);
		}
	}
	else
		return luaL_typerror(L, 3, "buffer or table or string or number");

	/* create new buffer using lhc_buffer_new */
	size_t size_new = size_buf + size_insert;
	lua_pushcfunction(L, lhc_buffer_new);
	lua_pushnumber(L, size_new);
	lua_call(L, 1, 1);

	float *buf_new = (float *)lua_touserdata(L, -1);
	size_t pose = posi + size_insert;
	for (size_t i = 0; i < size_new; ++i)
	{
		if (i < posi)
			buf_new[i] = buf[i];
		else if (i < pose)
			buf_new[i] = insert[i - posi];
		else
			buf_new[i] = buf[i - size_insert];
	}

	if (should_free)
		free(insert);

	return 1;
}
Beispiel #14
0
static int str_unpack (lua_State *L) {
  Header h;
  const char *fmt = luaL_checkstring(L, 1);
  size_t ld;
  const char *data = luaL_checklstring(L, 2, &ld);
  size_t pos = (size_t)posrelat(luaL_optinteger(L, 3, 1), ld) - 1;
  int n = 0;  /* number of results */
  luaL_argcheck(L, pos <= ld, 3, "initial position out of string");
  initheader(L, &h);
  while (*fmt != '\0') {
    int size, ntoalign;
    KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign);
    luaL_argcheck(L, (size_t)ntoalign + size <= ld - pos, 2,
                    "data string too short");
    pos += ntoalign;  /* skip alignment */
    /* stack space for item + next position */
    luaL_checkstack(L, 2, "too many results");
    n++;
    switch (opt) {
      case Kint:
      case Kuint: {
        lua_Integer res = unpackint(L, data + pos, h.islittle, size,
                                       (opt == Kint));
        lua_pushinteger(L, res);
        break;
      }
      case Kfloat: {
        volatile Ftypes u;
        lua_Number num;
        copywithendian(u.buff, data + pos, size, h.islittle);
        if (size == sizeof(u.f)) num = (lua_Number)u.f;
        else if (size == sizeof(u.d)) num = (lua_Number)u.d;
        else num = u.n;
        lua_pushnumber(L, num);
        break;
      }
      case Kchar: {
        lua_pushlstring(L, data + pos, size);
        break;
      }
      case Kstring: {
        size_t len = (size_t)unpackint(L, data + pos, h.islittle, size, 0);
        luaL_argcheck(L, len <= ld - pos - size, 2, "data string too short");
        lua_pushlstring(L, data + pos + size, len);
        pos += len;  /* skip string */
        break;
      }
      case Kzstr: {
        size_t len = (int)strlen(data + pos);
        luaL_argcheck(L, pos + len < ld, 2,
                         "unfinished string for format 'z'");
        lua_pushlstring(L, data + pos, len);
        pos += len + 1;  /* skip string plus final '\0' */
        break;
      }
      case Kpaddalign: case Kpadding: case Knop:
        n--;  /* undo increment */
        break;
    }
    pos += size;
  }
  lua_pushinteger(L, pos + 1);  /* next position */
  return n + 1;
}