static int skipBinNum(lex_state_t * state) { int someNumbers = 0; while (!iseos(state) && isbdigit(state->pos[0])) { state->pos++; someNumbers++; } return someNumbers; }
void zpcgetdouble(struct zpctoken *token, const char *str, char **retstr) { char *ptr = (char *)str; float dbl = 0.0; float div; if (*ptr == '0') { ptr++; if (*ptr == 'x' || *ptr == 'X') { /* hexadecimal value */ ptr++; while (*ptr != '.' && isxdigit(*ptr)) { dbl *= 16.0; dbl += tohexdbl(*ptr); ptr++; } ptr++; div = 16.0; while (isxdigit(*ptr)) { dbl += tohexdbl(*ptr) / div; div *= 16.0; ptr++; } token->radix = 16; token->data.f64 = dbl; } else if (*ptr == 'b' || *ptr == 'B') { /* binary value */ ptr++; while (*ptr != '.' && isbdigit(*ptr)) { dbl *= 2.0; dbl += tobindbl(*ptr); ptr++; } ptr++; div = 2.0; while (isbdigit(*ptr)) { dbl += tohexdbl(*ptr) / div; div *= 2.0; ptr++; } token->radix = 2; token->data.f64 = dbl; } else { /* octal value */ ptr++; while (*ptr != '.' && isodigit(*ptr)) { dbl *= 8.0; dbl += tobindbl(*ptr); ptr++; } ptr++; div = 8.0; while (isodigit(*ptr)) { dbl += tohexdbl(*ptr) / div; div *= 8.0; ptr++; } token->radix = 8; token->data.f64 = dbl; } } else { /* decimal value */ while (*ptr != '.' && isdigit(*ptr)) { dbl *= 10.0; dbl += tobindbl(*ptr); ptr++; } ptr++; div = 10.0; while (isdigit(*ptr)) { dbl += tohexdbl(*ptr) / div; div *= 10.0; ptr++; } token->radix = 10; token->data.f64 = dbl; } if (*ptr == ',') { ptr++; } *retstr = (char *)ptr; return; }
//static inline num_result_t parse_number(const char* c, size_t i){ num_result_t num_result; num_result.type = CH_NO_TYPE; if( c[i] == '\0'){ return num_result; } uint64_t uint_accumulator = 0; int64_t int_accumulator = 0; double float_accumulator = 0; double float_base_accumulator = 1; int64_t sign = 1; char prefix = 0; size_t state = STATE_INIT; for( ; state != STATE_NONE_FOUND && state != STATE_FINISHED_INT && state != STATE_FINISHED_UINT && state != STATE_FINISHED_FLOAT; i++ ){ switch(state){ case STATE_INIT:{ uint_accumulator = 0; float_accumulator = 0; float_base_accumulator = 1; sign = 1; if( c[i] == '-') { sign = -1; state = STATE_FOUND_SIGN; continue; } if( c[i] == '+') { state = STATE_FOUND_SIGN; continue; } if( c[i] == '.') { state = STATE_GET_FLO_DIGITS; continue; } if( c[i] == '0') { state = STATE_FOUND_INIT_ZERO; continue; } if( isdigit(c[i]) ) { uint_accumulator = (c[i] - '0'); state = STATE_GET_DEC_DIGITS; continue; } if( iswhite(c[i]) ) { state = STATE_INIT; continue; } else { state = STATE_NONE_FOUND; continue; } } case STATE_FOUND_SIGN:{ if( c[i] == '0' ) { state = STATE_MUST_BE_ZERO_OR_PERIOD; continue; } if( c[i] == '.' ) { state = STATE_GET_FLO_DIGITS; continue; } if( isdigit(c[i]) ) { uint_accumulator = (c[i] - '0'); state = STATE_GET_DEC_DIGITS; continue; } else { state = STATE_NONE_FOUND; continue; } } case STATE_MUST_BE_ZERO_OR_PERIOD: { if( c[i] == '0' ) { state = STATE_MUST_BE_ZERO_OR_PERIOD; continue; } if( c[i] == '.' ) { state = STATE_GET_FLO_DIGITS; continue; } else { state = STATE_NONE_FOUND; continue; } } case STATE_FOUND_INIT_ZERO: { if( c[i] == 'x') { state = STATE_GET_HEX_DIGITS; continue; } if( c[i] == 'X') { state = STATE_GET_HEX_DIGITS; continue; } if( c[i] == 'b') { state = STATE_GET_BIN_DIGITS; continue; } if( c[i] == 'B') { state = STATE_GET_BIN_DIGITS; continue; } if( c[i] == '.') { state = STATE_GET_FLO_DIGITS; continue; } if( c[i] == '\0') { state = STATE_FINISHED_UINT; continue; } if( isprefix(c[i])) { prefix = c[i]; state = STATE_END_UINT; continue; } if( isodigit(c[i])) { uint_accumulator = (c[i] - '0'); state = STATE_GET_OCT_DIGITS; continue; } else { state = STATE_NONE_FOUND; continue; } } case STATE_GET_DEC_DIGITS:{ if( isdigit(c[i]) ) { uint_accumulator *= 10; uint_accumulator += c[i] - '0'; state = STATE_GET_DEC_DIGITS; continue; } if( c[i] == '.') { float_accumulator = (double)uint_accumulator; state = STATE_GET_FLO_DIGITS; continue; } if( isprefix(c[i])) { prefix = c[i]; state = STATE_END_UINT; continue; } if( issci(c[i]) ) if( iswhite(c[i]) ) { state = STATE_END_UINT; continue; } if( isnull(c[i]) ) { state = STATE_FINISHED_UINT; continue; } else { state = STATE_NONE_FOUND; continue; } } case STATE_GET_BIN_DIGITS: { if( isbdigit(c[i]) ) { uint_accumulator <<= 1; uint_accumulator += c[i] - '0'; state = STATE_GET_BIN_DIGITS; continue; } if( isprefix(c[i])) { prefix = c[i]; state = STATE_END_UINT; continue; } if( iswhite(c[i]) ) { state = STATE_END_UINT; continue; } if( isnull(c[i]) ) { state = STATE_FINISHED_UINT; continue; } else { state = STATE_NONE_FOUND; continue; } } case STATE_GET_OCT_DIGITS: { if( isodigit(c[i]) ) { uint_accumulator *= 8; uint_accumulator += c[i] - '0'; state = STATE_GET_OCT_DIGITS; continue; } if( isprefix(c[i])) { prefix = c[i]; state = STATE_END_UINT; continue; } if( iswhite(c[i]) ) { state = STATE_END_UINT; continue; } if( isnull(c[i]) ) { state = STATE_FINISHED_UINT; continue; } else { state = STATE_NONE_FOUND; continue; } } case STATE_GET_HEX_DIGITS: { if( isdigit(c[i]) ) { uint_accumulator *= 16; uint_accumulator += c[i] - '0'; state = STATE_GET_HEX_DIGITS; continue; } if( isxdigit(c[i]) ) { uint_accumulator *= 16; uint_accumulator += getxdigit(c[i]); state = STATE_GET_HEX_DIGITS; continue; } if( isprefix(c[i])) { prefix = c[i]; state = STATE_END_UINT; continue; } if( iswhite(c[i]) ) { state = STATE_END_UINT; continue; } if( isnull(c[i]) ) { state = STATE_FINISHED_UINT; continue; } else { state = STATE_NONE_FOUND; continue; } } case STATE_GET_FLO_DIGITS: { if( isdigit(c[i]) ) { float_base_accumulator *= 10.0; float_accumulator += (double)(c[i] - '0') / float_base_accumulator; state = STATE_GET_FLO_DIGITS; continue; } if( isprefix(c[i])) { prefix = c[i]; state = STATE_END_FLOAT; continue; } if( iswhite(c[i]) ) { state = STATE_END_FLOAT; continue; } if( isnull(c[i]) ) { state = STATE_FINISHED_FLOAT; continue; } else { state = STATE_NONE_FOUND; continue; } } case STATE_END_UINT:{ if( iswhite(c[i]) ) { state = STATE_END_UINT; continue; } if( isbin( c[i]) && isprefix(prefix)) { uint_accumulator *= get_bin_prefix(prefix); state = STATE_FINISHED_UINT; continue; } if( isprefix(prefix)) { uint_accumulator *= get_prefix(prefix); state = STATE_FINISHED_UINT; continue; } if( isnull(c[i]) ) { state = STATE_FINISHED_UINT; continue; } else { state = STATE_NONE_FOUND; continue; } } case STATE_END_INT: { if( iswhite(c[i]) ) { state = STATE_END_INT; continue; } if( isprefix(prefix)) { int_accumulator *= get_prefix(prefix); state = STATE_FINISHED_INT; continue; } if( isbin(c[i]) && isprefix(prefix)) { int_accumulator *= get_bin_prefix(prefix); state = STATE_FINISHED_INT; continue; } if( isnull(c[i]) ) { state = STATE_FINISHED_INT; continue; } else { state = STATE_NONE_FOUND; continue; } } case STATE_END_FLOAT: { if( iswhite(c[i]) ) { state = STATE_END_FLOAT; continue; } if( isprefix(prefix)) { float_accumulator *= get_prefix(prefix); state = STATE_FINISHED_FLOAT; continue; } if( isbin( c[i] && isprefix(prefix))) { float_accumulator *= get_bin_prefix(prefix); state = STATE_FINISHED_FLOAT; continue; } if( isnull(c[i]) ) { state = STATE_FINISHED_FLOAT; continue; } else { state = STATE_NONE_FOUND; continue; } } default:{ ch_log_error("Undefined state parsing numeric %lu\n", state); num_result.type = CH_NO_TYPE; return num_result; } } } switch(state){ case STATE_NONE_FOUND:{ num_result.type = CH_NO_TYPE; break; } case STATE_FINISHED_INT: { num_result.type = CH_INT64; num_result.val_int = int_accumulator; break; } case STATE_FINISHED_UINT:{ if(sign == -1){ int_accumulator = uint_accumulator * sign; num_result.type = CH_INT64; num_result.val_int = int_accumulator; break; } num_result.type = CH_UINT64; num_result.val_uint = uint_accumulator; break; } case STATE_FINISHED_FLOAT:{ num_result.type = CH_DOUBLE; num_result.val_dble = float_accumulator * (double)sign; break; } default:{ ch_log_error("Undefined state parsing numeric %lu\n", state); num_result.type = CH_NO_TYPE; return num_result; } } return num_result; }
void zpcgetfloat(struct zpctoken *token, const char *str, char **retstr) { char *ptr = (char *)str; float flt = 0.0; float div; if (*ptr == '0') { ptr++; if (*ptr == 'x' || *ptr == 'X') { /* hexadecimal value */ ptr++; while (*ptr != '.' && isxdigit(*ptr)) { flt *= 16.0f; flt += tohexflt(*ptr); ptr++; } ptr++; div = 16.0f; while (isxdigit(*ptr)) { flt += tohexflt(*ptr) / div; div *= 16.0f; ptr++; } token->radix = 16; token->data.f32 = flt; } else if (*ptr == 'b' || *ptr == 'B') { /* binary value */ ptr++; while (*ptr != '.' && isbdigit(*ptr)) { flt *= 2.0f; flt += tobinflt(*ptr); ptr++; } ptr++; div = 2.0f; while (isbdigit(*ptr)) { flt += tohexflt(*ptr) / div; div *= 2.0f; ptr++; } token->radix = 2; token->data.f32 = flt; } else { /* octal value */ ptr++; while (*ptr != '.' && isodigit(*ptr)) { flt *= 8.0f; flt += tobinflt(*ptr); ptr++; } ptr++; div = 8.0f; while (isodigit(*ptr)) { flt += tohexflt(*ptr) / div; div *= 8.0f; ptr++; } token->radix = 8; token->data.f32 = flt; } } else { /* decimal value */ while (*ptr != '.' && isdigit(*ptr)) { flt *= 10.0f; flt += tobinflt(*ptr); ptr++; } ptr++; div = 10.0; while (isdigit(*ptr)) { flt += tohexflt(*ptr) / div; div *= 10.0; ptr++; } token->radix = 10; token->data.f32 = flt; } if (*ptr == 'f' || *ptr == 'F' || *ptr == ',') { ptr++; } *retstr = (char *)ptr; return; }