Example #1
0
// 100_199 = "1" DIGIT DIGIT
TextCursor parse_100_199(TextCursor cursor)
{
    char c = get_char(cursor);
    if(c != '1')
        throw ParseError();
    return parse_digit(parse_digit(cursor));
}
Example #2
0
CAMLprim value caml_int64_of_string(value s)
{
  char * p;
  uint64 max_uint64 = I64_literal(0xFFFFFFFF, 0xFFFFFFFF);
  uint64 max_int64  = I64_literal(0x80000000, 0x00000000);
  uint64 res, threshold;
  int sign, base, d;

  p = parse_sign_and_base(String_val(s), &base, &sign);
  I64_udivmod(max_uint64, I64_of_int32(base), &threshold, &res);
  d = parse_digit(*p);
  if (d < 0 || d >= base) caml_failwith("int_of_string");
  res = I64_of_int32(d);
  for (p++; /*nothing*/; p++) {
    char c = *p;
    if (c == '_') continue;
    d = parse_digit(c);
    if (d < 0 || d >= base) break;
    /* Detect overflow in multiplication base * res */
    if (I64_ult(threshold, res)) caml_failwith("int_of_string");
    res = I64_add(I64_mul(I64_of_int32(base), res), I64_of_int32(d));
    /* Detect overflow in addition (base * res) + d */
    if (I64_ult(res, I64_of_int32(d))) caml_failwith("int_of_string");
  }
  if (p != String_val(s) + caml_string_length(s)){
    caml_failwith("int_of_string");
  }
  if (base == 10 && I64_ult(max_int64, res)) caml_failwith("int_of_string");
  if (sign < 0) res = I64_neg(res);
  return caml_copy_int64(res);
}
static int parser_SetTextColor( char *psz_command, char *psz_end,
                                commandparams_t *p_params )
{
    int r = 0, g = 0, b = 0;
    VLC_UNUSED(psz_end);

    skip_space( &psz_command );
    if( isdigit( (unsigned char)*psz_command ) )
    {
        if( parse_digit( &psz_command, &p_params->i_id ) == VLC_EGENERIC )
            return VLC_EGENERIC;
    }
    skip_space( &psz_command );
    if( isdigit( (unsigned char)*psz_command ) )
    {
        if( parse_digit( &psz_command, &r ) == VLC_EGENERIC )
            return VLC_EGENERIC;
    }
    skip_space( &psz_command );
    if( isdigit( (unsigned char)*psz_command ) )
    {
        if( parse_digit( &psz_command, &g ) == VLC_EGENERIC )
            return VLC_EGENERIC;
    }
    skip_space( &psz_command );
    if( isdigit( (unsigned char)*psz_command ) )
    {
        if( parse_digit( &psz_command, &b ) == VLC_EGENERIC )
            return VLC_EGENERIC;
    }
    p_params->fontstyle.i_font_color = (r<<16) | (g<<8) | (b<<0);
    return VLC_SUCCESS;
}
Example #4
0
static inline bool parse_part(uint8_t *out, const char *in, size_t len)
{
	size_t i;

	for (i = 0; i < len; i++) {
		int a = parse_digit(in[2 * i]);
		int b = parse_digit(in[2 * i + 1]);

		if ((a < 0) || (b < 0))
			return false;

		out[i] = (a << 4) | b;
	}

	return true;
}
Example #5
0
// digits = 1*DIGIT
const char* parse_digits(unsigned char** p)
{
    int len = 0;
    while(!parse_digit(p))
        len++;

    return len ? NULL : ERR;
}
static int parser_SetAlpha( char *psz_command, char *psz_end,
                            commandparams_t *p_params )
{
    VLC_UNUSED(psz_end);
    skip_space( &psz_command );
    if( isdigit( (unsigned char)*psz_command ) )
    {
        if( parse_digit( &psz_command, &p_params->i_id ) == VLC_EGENERIC  )
            return VLC_EGENERIC;
    }
    skip_space( &psz_command );
    if( isdigit( (unsigned char)*psz_command ) )
    {
        if( parse_digit( &psz_command, &p_params->i_alpha ) == VLC_EGENERIC )
            return VLC_EGENERIC;
    }
    return VLC_SUCCESS;
}
static int parser_SetTextSize( char *psz_command, char *psz_end,
                               commandparams_t *p_params )
{
    VLC_UNUSED(psz_end);
    skip_space( &psz_command );
    if( isdigit( (unsigned char)*psz_command ) )
    {
        if( parse_digit( &psz_command, &p_params->i_id ) == VLC_EGENERIC )
            return VLC_EGENERIC;
    }
    skip_space( &psz_command );
    if( isdigit( (unsigned char)*psz_command ) )
    {
        if( parse_digit( &psz_command, &p_params->fontstyle.i_font_size ) == VLC_EGENERIC )
            return VLC_EGENERIC;
    }
    return VLC_SUCCESS;
}
Example #8
0
// 10_99 = 1-9 DIGIT
TextCursor parse_10_99(TextCursor cursor)
{
    char c = get_char(cursor);
    if ('1' <= c && c <= '9')
    {
        return parse_digit(cursor);
    }
    else 
        throw ParseError();
}
static int parser_SetVisibility( char *psz_command, char *psz_end,
                                 commandparams_t *p_params )
{
    VLC_UNUSED(psz_end);
    skip_space( &psz_command );
    if( isdigit( (unsigned char)*psz_command ) )
    {
        if( parse_digit( &psz_command, &p_params->i_id ) == VLC_EGENERIC )
            return VLC_EGENERIC;
    }
    skip_space( &psz_command );
    if( isdigit( (unsigned char)*psz_command ) )
    {
        int32_t i_vis = 0;
        if( parse_digit( &psz_command, &i_vis ) == VLC_EGENERIC )
            return VLC_EGENERIC;
        p_params->b_visible = (i_vis == 1) ? true : false;
    }
    return VLC_SUCCESS;
}
Example #10
0
static int parse_http_version(http_parser_t *parser)
{

    if (parse_string(parser, "HTTP/", 5))
        goto err;

    if (parse_digit(parser))
        goto err;

    if (parse_char(parser, '.'))
        goto err;

    if (parse_digit(parser))
        goto err;

    return 0;
err:
    // restore state
    return 1;
}
static int parser_Id( char *psz_command, char *psz_end,
                      commandparams_t *p_params )
{
    VLC_UNUSED(psz_end);
    skip_space( &psz_command );
    if( isdigit( *psz_command ) )
    {
        if( parse_digit( &psz_command, &p_params->i_id ) == VLC_EGENERIC )
            return VLC_EGENERIC;
    }
    return VLC_SUCCESS;
}
Example #12
0
// 200_249 = "2" 0-4 DIGIT
TextCursor parse_200_249(TextCursor cursor)
{
    char c = get_char(cursor);
    if (c != '2')
        throw ParseError();
    c = get_char(cursor);
    if ('0' <= c && c <= '4')
    {
        return parse_digit(cursor);
    }
    else
        throw ParseError();
}
static int parser_DataSharedMem( char *psz_command,
                                 char *psz_end,
                                 commandparams_t *p_params )
{
    /* Parse: 0 128 128 RGBA 9404459 */
    skip_space( &psz_command );
    if( isdigit( (unsigned char)*psz_command ) )
    {
        if( parse_digit( &psz_command, &p_params->i_id ) == VLC_EGENERIC )
            return VLC_EGENERIC;
    }
    skip_space( &psz_command );
    if( isdigit( (unsigned char)*psz_command ) )
    {
        if( parse_digit( &psz_command, &p_params->i_width ) == VLC_EGENERIC )
            return VLC_EGENERIC;
    }
    skip_space( &psz_command );
    if( isdigit( (unsigned char)*psz_command ) )
    {
        if( parse_digit( &psz_command, &p_params->i_height ) == VLC_EGENERIC )
            return VLC_EGENERIC;
    }
    skip_space( &psz_command );
    if( isascii( (unsigned char)*psz_command ) )
    {
        if( parse_char( &psz_command, &psz_end, 4, (char*)&p_params->fourcc )
            == VLC_EGENERIC )
            return VLC_EGENERIC;
    }
    skip_space( &psz_command );
    if( isdigit( (unsigned char)*psz_command ) )
    {
        if( parse_digit( &psz_command, &p_params->i_shmid ) == VLC_EGENERIC )
            return VLC_EGENERIC;
    }
    return VLC_SUCCESS;
}
Example #14
0
static intnat parse_intnat(value s, int nbits)
{
  char * p;
  uintnat res, threshold;
  int sign, base, d;

  p = parse_sign_and_base(String_val(s), &base, &sign);
  threshold = ((uintnat) -1) / base;
  d = parse_digit(*p);
  if (d < 0 || d >= base) caml_failwith("int_of_string");
  for (p++, res = d; /*nothing*/; p++) {
    char c = *p;
    if (c == '_') continue;
    d = parse_digit(c);
    if (d < 0 || d >= base) break;
    /* Detect overflow in multiplication base * res */
    if (res > threshold) caml_failwith("int_of_string");
    res = base * res + d;
    /* Detect overflow in addition (base * res) + d */
    if (res < (uintnat) d) caml_failwith("int_of_string");
  }
  if (p != String_val(s) + caml_string_length(s)){
    caml_failwith("int_of_string");
  }
  if (base == 10) {
    /* Signed representation expected, allow -2^(nbits-1) to 2^(nbits - 1) */
    if (res > (uintnat)1 << (nbits - 1))
      caml_failwith("int_of_string");
  } else {
    /* Unsigned representation expected, allow 0 to 2^nbits - 1
       and tolerate -(2^nbits - 1) to 0 */
    if (nbits < sizeof(uintnat) * 8 && res >= (uintnat)1 << nbits)
      caml_failwith("int_of_string");
  }
  return sign < 0 ? -((intnat) res) : (intnat) res;
}
Example #15
0
void LongNums::read_from_string(char* sNum) {
    bool err = false;
    //Считываем знак числа
    if (sNum[0] == '-') {
        sign = true;
        sNum++;
    }
    if (sNum[0] == '+') {
        sign = false;
        sNum++;
    }
    // Находим позицию точки и экспоненты в числе (если их нет, то позцияи равна нулю)
    char *stStart = sNum, *expStart = 0, *point_pos = 0;
    while(*sNum != '\0') {
        if (*sNum == POINT) {
            point_pos = sNum;
        }
        if ((*sNum == 'e') || (*sNum == 'E')) {
            expStart = sNum;
            expStart++;
        }
        sNum++;
    }

    sNum = stStart;

    if (point_pos != 0) {
        if (expStart != 0) {

            // 1) Если в числе есть точка и экспонента
            exponent_level = str_to_int(expStart, &err) + (point_pos - sNum);
            if (err) {
                error = "input error";
                return;
            }
            for (int i = 0, j = 0; sNum[i] != ' '; i++) {
                if (sNum[i] != POINT) {
                    if (parse_digit(sNum[i]) != -1) {
                        aDigits[j] = parse_digit(sNum[i]);
                    }
                    else {
                        error = "input error";
                        return;
                    }
                    j++;
                }
            }
        }
        else {

            // 2) Если в числе есть точка и нет экспоненты
            exponent_level = point_pos - sNum;
            for (int i = 0, j = 0; sNum[i] != '\0'; i++) {
                if (sNum[i] != POINT) {
                    if (parse_digit(sNum[i]) != -1) {
                        aDigits[j] = parse_digit(sNum[i]);
                    }
                    else {
                        error = "input error";
                        return;
                    }
                    j++;
                }
            }
        }
    }
    else {

        // 3) Если в числе нет точки и экспоненты
        if (expStart == 0) {
            exponent_level = strlen(sNum);
            for (int i = 0; i < exponent_level; i++) {
                if (parse_digit(sNum[i]) != -1) {
                    aDigits[i] = parse_digit(sNum[i]);
                }
                else {
                    error = "input error";
                    return;
                }
            }
        }
        else {

            // 4) Если в числе нет точки и есть экспонента
            err = false;
            exponent_level = (expStart - sNum - 2) + str_to_int(expStart, &err);
            if (err) {
                error = "input error";
                return;
            }
            for (int i = 0, j = 0; sNum[i] != ' '; i++) {
                if (sNum[i] != POINT) {
                    if (parse_digit(sNum[i]) != -1) {
                        aDigits[j] = parse_digit(sNum[i]);
                    }
                    else {
                        error = "input error";
                        return;
                    }
                    j++;
                }
            }
        }

    }

}
Example #16
0
int
yylex (void)
{
  unichar *start_token;
  unichar ch;

  if (! input_stream_pos)
    {
      fatal ("Input stream not setuped.\n");
      return -1;
    }
  if (mclex_want_line)
    {
      start_token = input_stream_pos;
      if (input_stream_pos[0] == '.'
	  && (input_stream_pos[1] == '\n'
	      || (input_stream_pos[1] == '\r' && input_stream_pos[2] == '\n')))
      {
	mclex_want_line = FALSE;
	while (input_stream_pos[0] != 0 && input_stream_pos[0] != '\n')
	  ++input_stream_pos;
	if (input_stream_pos[0] == '\n')
	  ++input_stream_pos;
	return MCENDLINE;
      }
      while (input_stream_pos[0] != 0 && input_stream_pos[0] != '\n')
	++input_stream_pos;
      if (input_stream_pos[0] == '\n')
	++input_stream_pos;
      yylval.ustr = get_diff (input_stream_pos, start_token);
      return MCLINE;
    }
  while ((ch = input_stream_pos[0]) <= 0x20)
    {
      if (ch == 0)
	return -1;
      ++input_stream_pos;
      if (ch == '\n')
	input_line += 1;
      if (mclex_want_nl && ch == '\n')
	{
	  mclex_want_nl = FALSE;
	  return NL;
	}
    }
  start_token = input_stream_pos;
  ++input_stream_pos;
  if (mclex_want_filename)
    {
      mclex_want_filename = FALSE;
      if (ch == '"')
	{
	  start_token++;
	  while ((ch = input_stream_pos[0]) != 0)
	    {
	      if (ch == '"')
		break;
	      ++input_stream_pos;
	    }
	  yylval.ustr = get_diff (input_stream_pos, start_token);
	  if (ch == '"')
	    ++input_stream_pos;
	}
      else
	{
	  while ((ch = input_stream_pos[0]) != 0)
	    {
	      if (ch <= 0x20 || ch == ')')
		break;
	      ++input_stream_pos;
	    }
	  yylval.ustr = get_diff (input_stream_pos, start_token);
	}
      return MCFILENAME;
    }
  switch (ch)
  {
  case ';':
    ++start_token;
    while (input_stream_pos[0] != '\n' && input_stream_pos[0] != 0)
      ++input_stream_pos;
    if (input_stream_pos[0] == '\n')
      input_stream_pos++;
    yylval.ustr = get_diff (input_stream_pos, start_token);
    return MCCOMMENT;
  case '=':
    return '=';
  case '(':
    return '(';
  case ')':
    return ')';
  case '+':
    return '+';
  case ':':
    return ':';
  case '0': case '1': case '2': case '3': case '4':
  case '5': case '6': case '7': case '8': case '9':
    yylval.ival = parse_digit (ch);
    return MCNUMBER;
  default:
    if (ch >= 0x40)
      {
	int ret;
	while (input_stream_pos[0] >= 0x40 || (input_stream_pos[0] >= '0' && input_stream_pos[0] <= '9'))
	  ++input_stream_pos;
	ret = mc_token (start_token, (size_t) (input_stream_pos - start_token));
	if (ret != -1)
	  return ret;
	yylval.ustr = get_diff (input_stream_pos, start_token);
	return MCIDENT;
      }
    yyerror ("illegal character 0x%x.", ch);
  }
  return -1;
}
Example #17
0
STRTOF_FLOAT STRTOF(const STRTOF_CHAR* str, STRTOF_CHAR** nptr)
{
	if ( nptr )
		*nptr = (STRTOF_CHAR*) str;

	while ( *str && STRTOF_ISSPACE((STRTOF_CTYPE_CHAR) *str) )
		str++;

	if ( !STRTOF_STRNCASECMP(str, STRTOF_L("INF"), 3) )
	{
		str += 3;
		if ( !STRTOF_STRNCASECMP(str, STRTOF_L("INITY"), 5) )
			str += 5;
		if ( nptr )
			*nptr = (STRTOF_CHAR*) str;
		return INFINITY;
	}

	if ( !STRTOF_STRNCASECMP(str, STRTOF_L("NAN"), 3) )
	{
		str += 3;
		str += nan_parameter(str);
		if ( nptr )
			*nptr = (STRTOF_CHAR*) str;
		return NAN;
	}

	bool negative = *str == STRTOF_L('-');
	if ( *str == STRTOF_L('-') )
		str++;
	else if ( *str == STRTOF_L('+') )
		str++;

	int base = 10;
	STRTOF_CHAR elc = 'e';
	STRTOF_CHAR euc = 'E';

	if ( is_hexadecimal_float(str) )
	{
		str += 2;
		base = 16;
		elc = 'p';
		euc = 'P';
	}

	bool any_digits = false;
	STRTOF_FLOAT result = 0.0;
	STRTOF_FLOAT add;

	while ( parse_digit(str, base, &add) )
	{
		str++;
		result = base * result + add;
		any_digits = true;
	}

	if ( *str == STRTOF_L('.' ) )
	{
		str++;
		STRTOF_FLOAT magnitude = 1.0;
		while ( parse_digit(str, base, &add) )
		{
			str++;
			magnitude /= base;
			result += add * magnitude;
			any_digits = true;
		}
	}

	if ( !any_digits )
		return errno = EINVAL, 0;

	if ( is_exponent(str, elc, euc) )
	{
		str++;
		bool exponent_negative = *str == STRTOF_L('-');
		if ( *str == STRTOF_L('-') )
			str++;
		else if ( *str == STRTOF_L('+') )
			str++;
		STRTOF_FLOAT exponent = 0.0;
		while ( parse_digit(str, 10, &add) )
		{
			str++;
			exponent = 10.0 * exponent + add;
			any_digits = true;
		}
		if ( exponent_negative )
			exponent = -exponent;
		result *= STRTOF_POW(base == 16 ? 2 : base, exponent);
	}

	if ( negative )
		result = -result;

	if ( nptr )
		*nptr = (STRTOF_CHAR*) str;
	return result;
}