| 1 | /* melder_atof.cpp |
| 2 | * |
| 3 | * Copyright (C) 2003-2008,2011,2015-2019,2021,2023 Paul Boersma |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "melder.h" |
| 20 | |
| 21 | /** |
| 22 | Assume that the next thing that follows is a numeric string, |
| 23 | and find the end of it. |
| 24 | Return null on error. |
| 25 | */ |
| 26 | template <typename T> |
| 27 | static const T *findEndOfNumericString (const T *string) { |
| 28 | const T *p = & string [0]; |
| 29 | /* |
| 30 | Leading white space is OK. |
| 31 | */ |
| 32 | while (Melder_isAsciiHorizontalOrVerticalSpace (*p)) |
| 33 | p ++; |
| 34 | /* |
| 35 | Next we accept an optional leading plus or minus. |
| 36 | */ |
| 37 | if (*p == '+' || *p == '-') |
| 38 | p ++; |
| 39 | /* |
| 40 | The next character has to be a decimal digit. |
| 41 | So we don't allow things like ".5". |
| 42 | */ |
| 43 | if (! Melder_isAsciiDecimalNumber (*p)) |
| 44 | return nullptr; // string is not numeric |
| 45 | /* |
| 46 | Special case: hexadecimal numbers. |
| 47 | */ |
| 48 | if (*p == '0' && (p [1] == 'x' || p [1] == 'X')) { |
| 49 | p += 2; |
| 50 | while (Melder_isHexadecimalDigit (*p)) |
| 51 | p ++; |
| 52 | if (*p == '.') |
| 53 | p ++; |
| 54 | while (Melder_isHexadecimalDigit (*p)) |
| 55 | p ++; |
| 56 | if (*p == 'p' || *p == 'P') |
| 57 | p ++; |
| 58 | if (*p == '+' || *p == '-') |
| 59 | p ++; |
| 60 | while (Melder_isAsciiDecimalNumber (*p)) // not hexadecimal! |
| 61 | p ++; |
| 62 | return p; |
| 63 | } |
| 64 | p ++; |
| 65 | /* |
| 66 | Then we accept any number of decimal digits. |
| 67 | */ |
| 68 | while (Melder_isAsciiDecimalNumber (*p)) |
| 69 | p ++; |
| 70 | /* |
| 71 | Next we accept an optional decimal point. |
| 72 | */ |
| 73 | if (*p == '.') { |
| 74 | p ++; |
| 75 | /* |
| 76 | We accept any number of (even zero) decimal digits after the decimal point. |
| 77 | */ |
| 78 | while (Melder_isAsciiDecimalNumber (*p)) |
| 79 | p ++; |
| 80 | } |
| 81 | // Next we accept an optional exponential E or e. |
| 82 | if (*p == 'e' || *p == 'E') { |
| 83 | p ++; |
| 84 | /* |
| 85 | In the exponent we accept an optional leading plus or minus. |
| 86 | */ |
| 87 | if (*p == '+' || *p == '-') |
| 88 | p ++; |
| 89 | /* |
| 90 | The exponent shall contain at least one decimal digit. |
| 91 | So we don't allow things like "+2.1E". |
| 92 | */ |
| 93 | if (! Melder_isAsciiDecimalNumber (*p)) |
| 94 | return nullptr; // string is not numeric |
| 95 | p ++; // skip first decimal digit |
| 96 | /* |
| 97 | Then we accept any number of decimal digits. |
| 98 | */ |
| 99 | while (Melder_isAsciiDecimalNumber (*p)) |
| 100 | p ++; |
| 101 | } |
| 102 | /* |
| 103 | Next we accept an optional percent sign. |
| 104 | */ |
| 105 | if (*p == '%') |
| 106 | p ++; |
| 107 | /* |
| 108 | We have found the end of the numeric string. |
| 109 | */ |
| 110 | return p; |
| 111 | } |
| 112 | |
| 113 | bool Melder_isStringNumeric (conststring32 string) { |
| 114 | if (! string) |
| 115 | return false; |
| 116 | const char32 *p = findEndOfNumericString (string); |
| 117 | bool weFoundANumber = !! p; |
| 118 | if (! weFoundANumber) |
| 119 | return false; |
| 120 | Melder_skipHorizontalOrVerticalSpace (& p); |
| 121 | bool contentFollowsTheNumber = ( *p != U'\0' ); |
| 122 | if (contentFollowsTheNumber) |
| 123 | return false; |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | double Melder_a8tof (conststring8 string) { |
| 128 | if (! string) |
| 129 | return undefined; |
| 130 | const char *p = findEndOfNumericString (string); |
| 131 | const bool weFoundANumber = !! p; |
| 132 | if (! weFoundANumber) |
| 133 | return undefined; |
| 134 | Melder_assert (p - & string [0] > 0); |
| 135 | return p [-1] == '%' ? 0.01 * strtod (string, nullptr) : strtod (string, nullptr); |
| 136 | } |
| 137 | |
| 138 | double Melder_atof (conststring32 string) { |
| 139 | if (! string) |
| 140 | return undefined; |
| 141 | const char32 *endOfNumericString = findEndOfNumericString (string); |
| 142 | const bool weFoundANumber = !! endOfNumericString; |
| 143 | if (! weFoundANumber) |
| 144 | return undefined; |
| 145 | const integer numberOfCharacters = endOfNumericString - & string [0]; |
| 146 | Melder_assert (numberOfCharacters > 0); |
| 147 | static MelderString buffer; |
| 148 | MelderString_ncopy (& buffer, string, numberOfCharacters); |
| 149 | const char *string8 = Melder_peek32to8 (buffer.string); |
| 150 | const integer string8length = Melder8_length (string8); |
| 151 | if (string8length < 1) |
| 152 | return undefined; |
| 153 | return string8 [string8length - 1] == '%' ? 0.01 * strtod (string8, nullptr) : strtod (string8, nullptr); |
| 154 | } |
| 155 | |
| 156 | int64 Melder_atoi (conststring32 string) { |
| 157 | return strtoll (Melder_peek32to8 (string), nullptr, 10); |
| 158 | } |
| 159 | |
| 160 | /* End of file melder_atof.cpp */ |