Exemplo n.º 1
0
int luaV_towstring (lua_State *L, TObject *obj) {
  if (ttype(obj) != LUA_TNUMBER)
    return 0;
  else {
    lua_WChar s[32];  /* 16 digits, sign, point and \0  (+ some extra...) */
    lua_number2wstr((s), 32, nvalue(obj));  /* convert `s' to number */
    setwsvalue2s(obj, luaWS_new(L, s));
    return 1;
  }
}
Exemplo n.º 2
0
int luaV_towstring (lua_State *L, TValue *obj) {
  if (ttype(obj) != LUA_TNUMBER)
    return 0;
  else {
    char s[32];  /* 16 digits, sign, point and \0  (+ some extra...) */
    lua_WChar ws[32];  /* 16 digits, sign, point and \0  (+ some extra...) */
	char* ptr = s;
	lua_WChar* wptr = ws;
	lua_number2wstr(s, nvalue(obj));  /* convert `s' to number */
	while (*ptr != 0)
		*wptr++ = *ptr++;
	*wptr = 0;
    setwsvalue2s(L, obj, luaWS_new(L, ws));
    return 1;
  }
}
Exemplo n.º 3
0
Arquivo: lvm.c Projeto: zapline/zlib
void luaV_concat (lua_State *L, int total, int last) {
  int useType = LUA_TSTRING;
  int i;
  StkId top = L->base + last + 1;

  for (i = 0; i < total; ++i)
  {
    if (ttype(top-1-i) == LUA_TSTRING || ttype(top-1-i) == LUA_TWSTRING)
    {
      useType = ttype(top-1-i);
      break;
    }
  }

  if (useType == LUA_TSTRING)
  {
    do {
      StkId top = L->base + last + 1;
      int n = 2;  /* number of elements handled in this pass (at least 2) */
      if (!tostring(L, top-2) || !tostring(L, top-1)) {
        if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
          luaG_concaterror(L, top-2, top-1);
      } else if (tsvalue(top-1)->len > 0) {  /* if len=0, do nothing */
        /* at least two string values; get as many as possible */
        size_t tl = tsvalue(top-1)->len;
        char *buffer;
        int i;
        /* collect total length */
        for (n = 1; n < total && tostring(L, top-n-1); n++) {
          size_t l = tsvalue(top-n-1)->len;
          if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow");
          tl += l;
        }
        buffer = luaZ_openspace(L, &G(L)->buff, tl);
        tl = 0;
        for (i=n; i>0; i--) {  /* concat all strings */
          size_t l = tsvalue(top-i)->len;
          memcpy(buffer+tl, svalue(top-i), l);
          tl += l;
#if LUA_REFCOUNT
          luarc_cleanvalue(top-i);
#endif /* LUA_REFCOUNT */
        }
        setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
      }
      total -= n-1;  /* got `n' strings to create 1 new */
      last -= n-1;
    } while (total > 1);  /* repeat until only 1 result left */
  } else {
    do {
      StkId top = L->base + last + 1;
      int n = 2;  /* number of elements handled in this pass (at least 2) */
      if (!towstring(L, top-2) || !towstring(L, top-1)) {
        if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
          luaG_concaterror(L, top-2, top-1);
      } else if (tsvalue(top-1)->len > 0) {  /* if len=0, do nothing */
        /* at least two string values; get as many as possible */
        size_t tl = tsvalue(top-1)->len;
        char *buffer;
        int i;
        /* collect total length */
        for (n = 1; n < total && towstring(L, top-n-1); n++) {
          size_t l = tsvalue(top-n-1)->len;
          if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow");
          tl += l;
        }
        buffer = luaZ_openspace(L, &G(L)->buff, tl*2);
        tl = 0;
        for (i=n; i>0; i--) {  /* concat all strings */
          size_t l = tsvalue(top-i)->len;
          memcpy(buffer+tl*2, wsvalue(top-i), l*2);
          tl += l;
#if LUA_REFCOUNT
          luarc_cleanvalue(top-i);
#endif /* LUA_REFCOUNT */
        }
        setwsvalue2s(L, top-n, luaS_newlwstr(L, (const lua_WChar*)buffer, tl));
      }
      total -= n-1;  /* got `n' strings to create 1 new */
      last -= n-1;
    } while (total > 1);  /* repeat until only 1 result left */
  }
}