Ejemplo n.º 1
0
double strtofloat(const char* s) {
  int minus = 0;
  int v = 0;
  int d;
  int k = 1;
  int w = 0;

  while(*s && isspace(static_cast<unsigned char>(*s))) s++;

  // Single optional + or -
  if (*s == '-' || *s == '+') {
    minus = (*s == '-');
    s++;
  }

  // Actual number parsing
  for (; *s && (d = DigitValue(*s, 10)) >= 0; s++)
    v = v*10 + d;
  if (*s == '.') {
    for (++s; *s && (d = DigitValue(*s, 10)) >= 0; s++) {
      w = w*10 + d;
      k *= 10;
    }
  }
  if (*s == 'e' || *s == 'E')
    tprintf("WARNING: Scientific Notation not supported!");

  double f  = static_cast<double>(v)
            + static_cast<double>(w) / static_cast<double>(k);

  return minus ? -f : f;
}
Ejemplo n.º 2
0
double streamtofloat(FILE* s)
{
  int minus = 0;
  int v = 0;
  int d, c = 0;
  int k = 1;
  int w = 0;

  for (c = fgetc(s); 
    isspace(static_cast<unsigned char>(c)) && (c != EOF); 
    c = fgetc(s)); 
  
  // Single optional + or -
  if (c == '-' || c == '+') {
    minus = (c == '-');
    c = fgetc(s);
  }

  // Actual number parsing
  for (; (c != EOF) && (d = DigitValue(c)) >= 0; c = fgetc(s))
    v = v*10 + d;
  if (c == '.') {
    for (c = fgetc(s); (c != EOF) && (d = DigitValue(c)) >= 0; c = fgetc(s)) {
      w = w*10 + d;
      k *= 10;
    }
  } else if (c == 'e' || c == 'E') 
    printf("WARNING: Scientific Notation not supported!");
  
  ungetc(c, s);
  double f  = static_cast<double>(v) 
            + static_cast<double>(w) / static_cast<double>(k);
  
  return minus ? -f : f;
}
Ejemplo n.º 3
0
inline int Lexer::ReadHexCodePoint()
{
	int digit = DigitValue(PeekAtChar());
	if (digit < 0 || digit >= 16)
	{
		Step();
		return -1;
	}

	int codePoint = 0;

	do
	{
		Step();
		
		// Avoid potential overflow for long hex sequence 
		if (codePoint < 256)
		{
			codePoint = (codePoint * 16) + digit;
		}

		digit = DigitValue(PeekAtChar());
	} 
	while (digit >= 0 && digit < 16);

	return codePoint;
}
Ejemplo n.º 4
0
// Note that the first digit has already been placed in the token buffer
void Lexer::ScanInteger(int radix)
{
	m_integer = 0;
	m_tokenType = SmallIntegerConst;
	int digit = DigitValue(PeekAtChar());

	int maxval = INT_MAX / radix;

	while (digit >= 0 && digit < radix)
	{
		*tp++ = NextChar();

		if (m_tokenType == SmallIntegerConst)
		{
			if (m_integer < maxval || (m_integer == maxval && digit <= INT_MAX % radix))
			{
				// we won't overflow, go ahead 
				m_integer = (m_integer * radix) + digit;
			}
			else
				// It will have to be left to Smalltalk to calc the large integer value
				m_tokenType = LargeIntegerConst;
		}

		digit = DigitValue(PeekAtChar());
	}
}
Ejemplo n.º 5
0
double streamtofloat(FILE* s) {
  int minus = 0;
  int v = 0;
  int d, c = 0;
  int k = 1;
  int w = 0;

  for (c = fgetc(s);
    isspace(static_cast<unsigned char>(c)) && (c != EOF);
    c = fgetc(s));

  // Single optional + or -
  if (c == '-' || c == '+') {
    minus = (c == '-');
    c = fgetc(s);
  }

  // Actual number parsing
  for (; c != EOF && (d = DigitValue(c, 10)) >= 0; c = fgetc(s))
    v = v*10 + d;
  if (c == '.') {
    for (c = fgetc(s); c != EOF && (d = DigitValue(c, 10)) >= 0; c = fgetc(s)) {
      w = w*10 + d;
      k *= 10;
    }
  }
  double f  = static_cast<double>(v)
            + static_cast<double>(w) / static_cast<double>(k);
  if (c == 'e' || c == 'E') {
    c = fgetc(s);
    int expsign = 1;
    if (c == '-' || c == '+') {
      expsign = (c == '-') ? -1 : 1;
      c = fgetc(s);
    }
    int exponent = 0;
    for (; (c != EOF) && (d = DigitValue(c, 10)) >= 0; c = fgetc(s)) {
      exponent = exponent * 10 + d;
    }
    exponent *= expsign;
    f *= pow(10.0, static_cast<double>(exponent));
  }
  ungetc(c, s);

  return minus ? -f : f;
}
Ejemplo n.º 6
0
static double streamtofloat(FILE* s) {
  bool minus = false;
  uint64_t v = 0;
  int d, c;
  uint64_t k = 1;
  uint64_t w = 0;

  for (c = fgetc(s); isascii(c) && isspace(c); c = fgetc(s));

  // Single optional + or -
  if (c == '-' || c == '+') {
    minus = (c == '-');
    c = fgetc(s);
  }

  // Actual number parsing
  for (; c != EOF && (d = DigitValue(c, 10)) >= 0; c = fgetc(s))
    v = v*10 + d;
  if (c == '.') {
    for (c = fgetc(s); c != EOF && (d = DigitValue(c, 10)) >= 0; c = fgetc(s)) {
      w = w*10 + d;
      k *= 10;
    }
  }
  double f = v + static_cast<double>(w) / k;
  if (c == 'e' || c == 'E') {
    c = fgetc(s);
    int expsign = 1;
    if (c == '-' || c == '+') {
      expsign = (c == '-') ? -1 : 1;
      c = fgetc(s);
    }
    int exponent = 0;
    for (; (c != EOF) && (d = DigitValue(c, 10)) >= 0; c = fgetc(s)) {
      exponent = exponent * 10 + d;
    }
    exponent *= expsign;
    f *= pow(10.0, static_cast<double>(exponent));
  }
  ungetc(c, s);

  return minus ? -f : f;
}
Ejemplo n.º 7
0
static void EvalRadixConstant(void)
{
	int radix;
	int digitVal;

	radix = tk_Number;
	if(radix < 2 || radix > 36)
	{
		ERR_Error(ERR_BAD_RADIX_CONSTANT, YES, NULL);
		radix = 36;
	}
	tk_Number = 0;
	while((digitVal = DigitValue(Chr, radix)) != -1)
	{
		tk_Number = radix*tk_Number+digitVal;
		NextChr();
	}
	tk_Token = TK_NUMBER;
}
Ejemplo n.º 8
0
// IO (re-)implementations -----------------------------------------------------
uintmax_t streamtoumax(FILE* s, int base)
{
  int minus = 0;
  uintmax_t v = 0;
  int d, c = 0;

  for (c = fgetc(s); 
    isspace(static_cast<unsigned char>(c)) && (c != EOF); 
    c = fgetc(s)) 
  
  // Single optional + or - 
  if (c == '-' || c == '+') {
    minus = (c == '-');
    c = fgetc(s);
  }

  // Assign correct base
  if (base == 0) {
    if (c == '0') {
      c = fgetc(s);
      if (c == 'x' || c == 'X') {
        base = 16;
        c = fgetc(s);
      } else {
        base = 8;
      }
    }
  } else if (base == 16) {
    if (c == '0') {
      c = fgetc(s);
      if (c == 'x' && c == 'X') c = fgetc(s);
    }
  }

  // Actual number parsing
  for (; (c != EOF) && (d = DigitValue(c)) >= 0 && d < base; c = fgetc(s))
    v = v*base + d;

  ungetc(c, s);
  return minus ? -v : v;
}
Ejemplo n.º 9
0
bool Tokenizer::ParseInteger(const string& text, uint64 max_value, uint64* output) 
{
 // Sadly, we can't just use strtoul() since it is only 32-bit and strtoull()
 // is non-standard.  I hate the C standard library.  :(
 //  return strtoull(text.c_str(), NULL, 0);

	const char* ptr = text.c_str();
	int base = 10;
	if (ptr[0] == '0') {
		if (ptr[1] == 'x' || ptr[1] == 'X') 
		{
			// This is hex.
			base = 16;
			ptr += 2;
		} else {
			// This is octal.
			base = 8;
		}
	}

	uint64 result = 0;
	for (; *ptr != '\0'; ptr++) 
	{
		int digit = DigitValue(*ptr);
		GOOGLE_LOG_IF(DFATAL, digit < 0 || digit >= base)
			<< " Tokenizer::ParseInteger() passed text that could not have been"
			" tokenized as an integer: " << CEscape(text);
		if (digit > max_value || result > (max_value - digit) / base) 
		{
			// Overflow.
			return false;
		}
		result = result * base + digit;
	}

	*output = result;
	return true;
}
Ejemplo n.º 10
0
static float LexNumber (void)
{
	int	c, c2;
	int	intNumber;
	char	*buffer;
	qboolean	neg;
	int	digitVal;
	int	radix;

	if (*pr_file_p == '.')
	{
		return LexFraction();
	}
	buffer = pr_token;
	c = *pr_file_p++;
	c2 = *pr_file_p;
	if (c == '0' && (c2 == 'x' || c2 =='X'))
	{
		*buffer++ = c;
		*buffer++ = *pr_file_p++;
		intNumber = 0;
		c = *pr_file_p;
		while ((digitVal = DigitValue(c, 16)) != -1)
		{
			*buffer++ = c;
			intNumber = (intNumber<<4)+digitVal;
			c = *++pr_file_p;
		}
		*buffer = 0;
		return (float)intNumber;
	}
	if (c == '-')
	{
		intNumber = 0;
		neg = true;
	}
	else
	{
		intNumber = c-'0';
		neg = false;
	}
	*buffer++ = c;
	while (ASCIIToChrCode[c2] == CHR_NUMBER)
	{
		*buffer++ = c2;
		intNumber = 10*intNumber+(c2-'0');
		c2 = *++pr_file_p;
	}
	if (c2 == '_')
	{
		*buffer++ = *pr_file_p++;
		radix = intNumber;
		if (radix < 2 || radix > 36)
		{
			PR_ParseError("bad radix in integer constant (%d)", radix);
		}
		intNumber = 0;
		c = *pr_file_p;
		while ((digitVal = DigitValue(c, radix)) != -1)
		{
			*buffer++ = c;
			intNumber = radix*intNumber+digitVal;
			c = *++pr_file_p;
		}
		*buffer = 0;
		return neg ? (float)-intNumber : (float)intNumber;
	}
	if (c2 == '.' && pr_file_p[1] != '.')
	{
		*buffer++ = c2;
		c2 = *++pr_file_p;
		while (ASCIIToChrCode[c2] == CHR_NUMBER)
		{
			*buffer++ = c2;
			c2 = *++pr_file_p;
		}
		*buffer = 0;
		return (float)atof(pr_token);
	}
	*buffer = 0;
	return neg ? (float)-intNumber : (float)intNumber;
}