MCPcopy Create free account
hub / github.com/dhbloo/rapfi / parse_number

Function parse_number

Rapfi/external/cpptoml/include/cpptoml.h:2657–2785  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2655 }
2656
2657 std::shared_ptr<base> parse_number(std::string::iterator& it,
2658 const std::string::iterator& end)
2659 {
2660 auto check_it = it;
2661 auto check_end = find_end_of_number(it, end);
2662
2663 auto eat_sign = [&]() {
2664 if (check_it != end && (*check_it == '-' || *check_it == '+'))
2665 ++check_it;
2666 };
2667
2668 auto check_no_leading_zero = [&]() {
2669 if (check_it != end && *check_it == '0' && check_it + 1 != check_end
2670 && check_it[1] != '.')
2671 {
2672 throw_parse_exception("Numbers may not have leading zeros");
2673 }
2674 };
2675
2676 auto eat_digits = [&](bool (*check_char)(char)) {
2677 auto beg = check_it;
2678 while (check_it != end && check_char(*check_it))
2679 {
2680 ++check_it;
2681 if (check_it != end && *check_it == '_')
2682 {
2683 ++check_it;
2684 if (check_it == end || !check_char(*check_it))
2685 throw_parse_exception("Malformed number");
2686 }
2687 }
2688
2689 if (check_it == beg)
2690 throw_parse_exception("Malformed number");
2691 };
2692
2693 auto eat_hex = [&]() { eat_digits(&is_hex); };
2694
2695 auto eat_numbers = [&]() { eat_digits(&is_number); };
2696
2697 if (check_it != end && *check_it == '0' && check_it + 1 != check_end
2698 && (check_it[1] == 'x' || check_it[1] == 'o' || check_it[1] == 'b'))
2699 {
2700 ++check_it;
2701 char base = *check_it;
2702 ++check_it;
2703 if (base == 'x')
2704 {
2705 eat_hex();
2706 return parse_int(it, check_it, 16);
2707 }
2708 else if (base == 'o')
2709 {
2710 auto start = check_it;
2711 eat_numbers();
2712 auto val = parse_int(start, check_it, 8, "0");
2713 it = start;
2714 return val;

Callers 1

parse_valueFunction · 0.85

Calls 5

find_end_of_numberFunction · 0.85
throw_parse_exceptionFunction · 0.85
parse_intFunction · 0.85
make_valueFunction · 0.85
parse_floatFunction · 0.85

Tested by

no test coverage detected