| 66 | } |
| 67 | |
| 68 | double to_double(std::string_view input) |
| 69 | { |
| 70 | #ifdef __cpp_lib_to_chars |
| 71 | double data_out; |
| 72 | std::from_chars(input.data(), input.data() + input.size(), data_out); |
| 73 | return data_out; |
| 74 | #else |
| 75 | const char* num = input.data(); |
| 76 | if (!num || !*num) |
| 77 | { |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | int sign = 1; |
| 82 | double integerPart = 0.0; |
| 83 | double fractionPart = 0.0; |
| 84 | bool hasFraction = false; |
| 85 | |
| 86 | /*Take care of +/- sign*/ |
| 87 | if (*num == '-') |
| 88 | { |
| 89 | ++num; |
| 90 | sign = -1; |
| 91 | } |
| 92 | else if (*num == '+') |
| 93 | { |
| 94 | ++num; |
| 95 | } |
| 96 | |
| 97 | while (*num != '\0') |
| 98 | { |
| 99 | if (*num >= '0' && *num <= '9') |
| 100 | { |
| 101 | integerPart = integerPart * 10 + (*num - '0'); |
| 102 | } |
| 103 | else if (*num == '.') |
| 104 | { |
| 105 | hasFraction = true; |
| 106 | ++num; |
| 107 | break; |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | return sign * integerPart; |
| 112 | } |
| 113 | ++num; |
| 114 | } |
| 115 | |
| 116 | if (hasFraction) |
| 117 | { |
| 118 | double fractionExpo = 0.1; |
| 119 | |
| 120 | while (*num != '\0') |
| 121 | { |
| 122 | if (*num >= '0' && *num <= '9') |
| 123 | { |
| 124 | fractionPart += fractionExpo * (*num - '0'); |
| 125 | fractionExpo *= 0.1; |