예제 #1
0
파일: lnum.c 프로젝트: 7568168/cheat-engine
/* 's' is expected to be LUAI_MAXNUMBER2STR long (enough for any number)
*/
void luaO_num2buf( char *s, const TValue *o )
{
  lua_Number n;
  lua_assert( ttisnumber(o) );

  /* Reason to handle integers differently is not only speed, but accuracy as
   * well. We want to make any integer tostring() without roundings, at all.
   */
#ifdef LUA_TINT
  if (ttisint(o)) {
    lua_integer2str( s, ivalue(o) );
    return;
  }
#endif
  n= nvalue_fast(o);
  lua_real2str(s, n);

#ifdef LNUM_COMPLEX
  lua_Number n2= nvalue_img_fast(o);
  if (n2!=0) {   /* Postfix with +-Ni */
      int re0= (n == 0);
      char *s2= re0 ? s : strchr(s,'\0'); 
      if ((!re0) && (n2>0)) *s2++= '+';
      lua_real2str( s2, n2 );
      strcat(s2,"i");
  }
#endif
}
예제 #2
0
static void json_append_number(lua_State *l, json_config_t *cfg,
                               strbuf_t *json, int lindex)
{
    int len;
#if LUA_VERSION_NUM >= 503
    if (lua_isinteger(l, lindex)) {
        lua_Integer num = lua_tointeger(l, lindex);
        strbuf_ensure_empty_length(json, FPCONV_G_FMT_BUFSIZE); /* max length of int64 is 19 */
        len = lua_integer2str(strbuf_empty_ptr(json), num);
        strbuf_extend_length(json, len);
        return;
    }
#endif
    double num = lua_tonumber(l, lindex);

    if (cfg->encode_invalid_numbers == 0) {
        /* Prevent encoding invalid numbers */
        if (isinf(num) || isnan(num))
            json_encode_exception(l, cfg, json, lindex,
                                  "must not be NaN or Infinity");
    } else if (cfg->encode_invalid_numbers == 1) {
        /* Encode NaN/Infinity separately to ensure Javascript compatible
         * values are used. */
        if (isnan(num)) {
            strbuf_append_mem(json, "NaN", 3);
            return;
        }
        if (isinf(num)) {
            if (num < 0)
                strbuf_append_mem(json, "-Infinity", 9);
            else
                strbuf_append_mem(json, "Infinity", 8);
            return;
        }
    } else {
        /* Encode invalid numbers as "null" */
        if (isinf(num) || isnan(num)) {
            strbuf_append_mem(json, "null", 4);
            return;
        }
    }

    strbuf_ensure_empty_length(json, FPCONV_G_FMT_BUFSIZE);
    len = fpconv_g_fmt(strbuf_empty_ptr(json), num, cfg->encode_number_precision);
    strbuf_extend_length(json, len);
}
예제 #3
0
int luaV_tostring (lua_State *L, StkId obj) {
  if (!ttisnumber(obj))
    return 0;
  else {
    char buff[MAXNUMBER2STR];
    size_t len;
    if (ttisinteger(obj))
      len = lua_integer2str(buff, ivalue(obj));
    else {
      len = lua_number2str(buff, fltvalue(obj));
      if (strspn(buff, "-0123456789") == len) {  /* look like an integer? */
        buff[len++] = '.';  /* add a '.0' */
        buff[len++] = '0';
        buff[len] = '\0';
      }
    }
    setsvalue2s(L, obj, luaS_newlstr(L, buff, len));
    return 1;
  }
}