コード例 #1
0
ファイル: liolib.c プロジェクト: 825126369/2018_Server
/*
** Read a number: first reads a valid prefix of a numeral into a buffer.
** Then it calls 'lua_stringtonumber' to check whether the format is
** correct and to convert it to a Lua number
*/
static int read_number (lua_State *L, FILE *f) {
  RN rn;
  int count = 0;
  int hex = 0;
  char decp[2];
  rn.f = f; rn.n = 0;
  decp[0] = lua_getlocaledecpoint();  /* get decimal point from locale */
  decp[1] = '.';  /* always accept a dot */
  l_lockfile(rn.f);
  do { rn.c = l_getc(rn.f); } while (isspace(rn.c));  /* skip spaces */
  test2(&rn, "-+");  /* optional signal */
  if (test2(&rn, "00")) {
    if (test2(&rn, "xX")) hex = 1;  /* numeral is hexadecimal */
    else count = 1;  /* count initial '0' as a valid digit */
  }
  count += readdigits(&rn, hex);  /* integral part */
  if (test2(&rn, decp))  /* decimal point? */
    count += readdigits(&rn, hex);  /* fractional part */
  if (count > 0 && test2(&rn, (hex ? "pP" : "eE"))) {  /* exponent mark? */
    test2(&rn, "-+");  /* exponent signal */
    readdigits(&rn, 0);  /* exponent digits */
  }
  ungetc(rn.c, rn.f);  /* unread look-ahead char */
  l_unlockfile(rn.f);
  rn.buff[rn.n] = '\0';  /* finish string */
  if (lua_stringtonumber(L, rn.buff))  /* is this a valid number? */
    return 1;  /* ok */
  else {  /* invalid format */
   lua_pushnil(L);  /* "result" to be removed */
   return 0;  /* read fails */
  }
}
コード例 #2
0
ファイル: lstrlib.c プロジェクト: guodawei/lua
static int num2straux (char *buff, int sz, lua_Number x) {
  /* if 'inf' or 'NaN', format it like '%g' */
  if (x != x || x == (lua_Number)HUGE_VAL || x == -(lua_Number)HUGE_VAL)
    return l_sprintf(buff, sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)x);
  else if (x == 0) {  /* can be -0... */
    /* create "0" or "-0" followed by exponent */
    return l_sprintf(buff, sz, LUA_NUMBER_FMT "x0p+0", (LUAI_UACNUMBER)x);
  }
  else {
    int e;
    lua_Number m = l_mathop(frexp)(x, &e);  /* 'x' fraction and exponent */
    int n = 0;  /* character count */
    if (m < 0) {  /* is number negative? */
      buff[n++] = '-';  /* add sign */
      m = -m;  /* make it positive */
    }
    buff[n++] = '0'; buff[n++] = 'x';  /* add "0x" */
    m = adddigit(buff, n++, m * (1 << L_NBFD));  /* add first digit */
    e -= L_NBFD;  /* this digit goes before the radix point */
    if (m > 0) {  /* more digits? */
      buff[n++] = lua_getlocaledecpoint();  /* add radix point */
      do {  /* add as many digits as needed */
        m = adddigit(buff, n++, m * 16);
      } while (m > 0);
    }
    n += l_sprintf(buff + n, sz - n, "p%+d", e);  /* add exponent */
    lua_assert(n < sz);
    return n;
  }
}
コード例 #3
0
ファイル: lstrlib.c プロジェクト: iboB/lua
static int num2straux (char *buff, lua_Number x) {
  if (x != x || x == HUGE_VAL || x == -HUGE_VAL)  /* inf or NaN? */
    return sprintf(buff, LUA_NUMBER_FMT, x);  /* equal to '%g' */
  else if (x == 0) {  /* can be -0... */
    sprintf(buff, LUA_NUMBER_FMT, x);
    strcat(buff, "x0p+0");  /* reuses '0/-0' from 'sprintf'... */
    return (int)strlen(buff); /* Custom change: fixed 64-bit compilation warning */
  }
  else {
    int e;
    lua_Number m = l_mathop(frexp)(x, &e);  /* 'x' fraction and exponent */
    int n = 0;  /* character count */
    if (m < 0) {  /* is number negative? */
      buff[n++] = '-';  /* add signal */
      m = -m;  /* make it positive */
    }
    buff[n++] = '0'; buff[n++] = 'x';  /* add "0x" */
    m = adddigit(buff, n++, m * (1 << L_NBFD));  /* add first digit */
    e -= L_NBFD;  /* this digit goes before the radix point */
    if (m > 0) {  /* more digits? */
      buff[n++] = lua_getlocaledecpoint();  /* add radix point */
      do {  /* add as many digits as needed */
        m = adddigit(buff, n++, m * 16);
      } while (m > 0);
    }
    n += sprintf(buff + n, "p%+d", e);  /* add exponent */
    return n;
  }
}
コード例 #4
0
ファイル: lstrlib.c プロジェクト: Enverex/mame
/*
** Ensures the 'buff' string uses a dot as the radix character.
*/
static void checkdp (char *buff, int nb) {
  if (memchr(buff, '.', nb) == NULL) {  /* no dot? */
    char point = lua_getlocaledecpoint();  /* try locale point */
    char *ppoint = (char*)memchr(buff, point, nb);
    if (ppoint) *ppoint = '.';  /* change it to a dot */
  }
}
コード例 #5
0
/*
** in case of format error, try to change decimal point separator to
** the one defined in the current locale and check again
*/
static void trydecpoint (LexState *ls, TValue *o) {
  char old = ls->decpoint;
  ls->decpoint = lua_getlocaledecpoint();
  buffreplace(ls, old, ls->decpoint);  /* try new decimal separator */
  if (!buff2num(ls->buff, o)) {
    /* format error with correct decimal point: no more options */
    buffreplace(ls, ls->decpoint, '.');  /* undo change (for error message) */
    lexerror(ls, "malformed number", TK_FLT);
  }
}
コード例 #6
0
ファイル: lobject.cpp プロジェクト: swizl/lua
/*
** convert an hexadecimal numeric string to a number, following
** C99 specification for 'strtod'
*/
static lua_Number lua_strx2number (const char *s, char **endptr) {
	int dot = lua_getlocaledecpoint();
	lua_Number r = 0.0;  /* result (accumulator) */
	int sigdig = 0;  /* number of significant digits */
	int nosigdig = 0;  /* number of non-significant digits */
	int e = 0;  /* exponent correction */
	int neg;  /* 1 if number is negative */
	int hasdot = 0;  /* true after seen a dot */
	*endptr = cast(char *, s);  /* nothing is valid yet */
	while (lisspace(cast_uchar(*s))) s++;  /* skip initial spaces */
	neg = isneg(&s);  /* check signal */
	if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X')))  /* check '0x' */
		return 0.0;  /* invalid format (no '0x') */
	for (s += 2; ; s++) {  /* skip '0x' and read numeral */
		if (*s == dot) {
			if (hasdot) break;  /* second dot? stop loop */
			else hasdot = 1;
		}
		else if (lisxdigit(cast_uchar(*s))) {
			if (sigdig == 0 && *s == '0')  /* non-significant digit (zero)? */
				nosigdig++;
			else if (++sigdig <= MAXSIGDIG)  /* can read it without overflow? */
					r = (r * cast_num(16.0)) + luaO_hexavalue(*s);
			else e++; /* too many digits; ignore, but still count for exponent */
			if (hasdot) e--;  /* decimal digit? correct exponent */
		}
		else break;  /* neither a dot nor a digit */
	}
	if (nosigdig + sigdig == 0)  /* no digits? */
		return 0.0;  /* invalid format */
	*endptr = cast(char *, s);  /* valid up to here */
	e *= 4;  /* each digit multiplies/divides value by 2^4 */
	if (*s == 'p' || *s == 'P') {  /* exponent part? */
		int exp1 = 0;  /* exponent value */
		int neg1;  /* exponent signal */
		s++;  /* skip 'p' */
		neg1 = isneg(&s);  /* signal */
		if (!lisdigit(cast_uchar(*s)))
			return 0.0;  /* invalid; must have at least one digit */
		while (lisdigit(cast_uchar(*s)))  /* read exponent */
			exp1 = exp1 * 10 + *(s++) - '0';
		if (neg1) exp1 = -exp1;
		e += exp1;
		*endptr = cast(char *, s);  /* valid up to here */
	}
コード例 #7
0
ファイル: lstrlib.c プロジェクト: guodawei/lua
/*
** Serialize a floating-point number in such a way that it can be
** scanned back by Lua. Use hexadecimal format for "common" numbers
** (to preserve precision); inf, -inf, and NaN are handled separately.
** (NaN cannot be expressed as a numeral, so we write '(0/0)' for it.)
*/
static int quotefloat (lua_State *L, char *buff, lua_Number n) {
  const char *s;  /* for the fixed representations */
  if (n == (lua_Number)HUGE_VAL)  /* inf? */
    s = "1e9999";
  else if (n == -(lua_Number)HUGE_VAL)  /* -inf? */
    s = "-1e9999";
  else if (n != n)  /* NaN? */
    s = "(0/0)";
  else {  /* format number as hexadecimal */
    int  nb = lua_number2strx(L, buff, MAX_ITEM,
                                 "%" LUA_NUMBER_FRMLEN "a", n);
    /* ensures that 'buff' string uses a dot as the radix character */
    if (memchr(buff, '.', nb) == NULL) {  /* no dot? */
      char point = lua_getlocaledecpoint();  /* try locale point */
      char *ppoint = (char *)memchr(buff, point, nb);
      if (ppoint) *ppoint = '.';  /* change it to a dot */
    }
    return nb;
  }
  /* for the fixed representations */
  return l_sprintf(buff, MAX_ITEM, "%s", s);
}