| 564 | } |
| 565 | |
| 566 | static JSON parse_number( const std::string &str, size_t &offset ) { |
| 567 | std::string val, exp_str; |
| 568 | char c = '\0'; |
| 569 | bool isDouble = false; |
| 570 | bool isNegative = false; |
| 571 | int64_t exp = 0; |
| 572 | bool isExpNegative = false; |
| 573 | if( offset < str.size() && str.at(offset) == '-' ) { |
| 574 | isNegative = true; |
| 575 | ++offset; |
| 576 | } |
| 577 | for (; offset < str.size() ;) { |
| 578 | c = str.at(offset++); |
| 579 | if( c >= '0' && c <= '9' ) { |
| 580 | val += c; |
| 581 | } else if( c == '.' && !isDouble ) { |
| 582 | val += c; |
| 583 | isDouble = true; |
| 584 | } else { |
| 585 | break; |
| 586 | } |
| 587 | } |
| 588 | if( offset < str.size() && (c == 'E' || c == 'e' )) { |
| 589 | c = str.at(offset++); |
| 590 | if( c == '-' ) { |
| 591 | isExpNegative = true; |
| 592 | } else if( c == '+' ) { |
| 593 | // do nothing |
| 594 | } else { |
| 595 | --offset; |
| 596 | } |
| 597 | |
| 598 | for (; offset < str.size() ;) { |
| 599 | c = str.at(offset++); |
| 600 | if( c >= '0' && c <= '9' ) { |
| 601 | exp_str += c; |
| 602 | } else if( !isspace( c ) && c != ',' && c != ']' && c != '}' ) { |
| 603 | throw std::runtime_error(std::string("JSON ERROR: Number: Expected a number for exponent, found '") + c + "'"); |
| 604 | } |
| 605 | else { |
| 606 | break; |
| 607 | } |
| 608 | } |
| 609 | exp = chaiscript::parse_num<int64_t>( exp_str ) * (isExpNegative?-1:1); |
| 610 | } |
| 611 | else if( offset < str.size() && (!isspace( c ) && c != ',' && c != ']' && c != '}' )) { |
| 612 | throw std::runtime_error(std::string("JSON ERROR: Number: unexpected character '") + c + "'"); |
| 613 | } |
| 614 | --offset; |
| 615 | |
| 616 | if( isDouble ) { |
| 617 | return JSON((isNegative?-1:1) * chaiscript::parse_num<double>( val ) * std::pow( 10, exp )); |
| 618 | } else { |
| 619 | if( !exp_str.empty() ) { |
| 620 | return JSON((isNegative?-1:1) * static_cast<double>(chaiscript::parse_num<int64_t>( val )) * std::pow( 10, exp )); |
| 621 | } else { |
| 622 | return JSON((isNegative?-1:1) * chaiscript::parse_num<int64_t>( val )); |
| 623 | } |