Debian Codegraph @

melder/melder_atof.cpp

raw symbols SCIP
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"cxx . . $ `<file>/melder/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 *findEndOfNumericStringcxx . . $ findEndOfNumericString(bd9f52c6ae4be57a). (const T *string) {
28 const T *p = & string [0];
29 /*
30 Leading white space is OK.
31 */
32 while (Melder_isAsciiHorizontalOrVerticalSpacecxx . . $ Melder_isAsciiHorizontalOrVerticalSpace(b8d0204ce8416907). (*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_isAsciiDecimalNumbercxx . . $ Melder_isAsciiDecimalNumber(b8d0204ce8416907). (*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 +=cxx . . $ `operator+=`(7b88dc05e9f3af85). 2;
50 while (Melder_isHexadecimalDigitcxx . . $ Melder_isHexadecimalDigit(b8d0204ce8416907). (*p))
51 p ++;
52 if (*p == '.')
53 p ++;
54 while (Melder_isHexadecimalDigitcxx . . $ Melder_isHexadecimalDigit(b8d0204ce8416907). (*p))
55 p ++;
56 if (*p == 'p' || *p == 'P')
57 p ++;
58 if (*p == '+' || *p == '-')
59 p ++;
60 while (Melder_isAsciiDecimalNumbercxx . . $ Melder_isAsciiDecimalNumber(b8d0204ce8416907). (*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_isAsciiDecimalNumbercxx . . $ Melder_isAsciiDecimalNumber(b8d0204ce8416907). (*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_isAsciiDecimalNumbercxx . . $ Melder_isAsciiDecimalNumber(b8d0204ce8416907). (*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_isAsciiDecimalNumbercxx . . $ Melder_isAsciiDecimalNumber(b8d0204ce8416907). (*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_isAsciiDecimalNumbercxx . . $ Melder_isAsciiDecimalNumber(b8d0204ce8416907). (*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_isStringNumericcxx . . $ Melder_isStringNumeric(2df281243f91fe91). (conststring32cxx . . $ conststring32# string) {
114 if (! string)
115 return false;
116 const char32cxx . . $ char32# *p = findEndOfNumericStringcxx . . $ findEndOfNumericString(bd9f52c6ae4be57a). (string);
117 bool weFoundANumber = !! p;
118 if (! weFoundANumber)
119 return false;
120 Melder_skipHorizontalOrVerticalSpacecxx . . $ Melder_skipHorizontalOrVerticalSpace(1babf4dbe7380927). (& p);
121 bool contentFollowsTheNumber = ( *p != U'\0' );
122 if (contentFollowsTheNumber)
123 return false;
124 return true;
125 }
126
127 double Melder_a8tofcxx . . $ Melder_a8tof(9a18bdb0e56c1bc2). (conststring8cxx . . $ conststring8# string) {
128 if (! string)
129 return undefinedcxx . . $ undefined.;
130 const char *p = findEndOfNumericStringcxx . . $ findEndOfNumericString(bd9f52c6ae4be57a). (string);
131 const bool weFoundANumber = !! p;
132 if (! weFoundANumber)
133 return undefinedcxx . . $ undefined.;
134 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (p - & string [0] > 0);
135 return p [-1] == '%' ? 0.01 * strtodcxx . . $ strtod(d6476bb9e2fdf17c). (string, nullptr) : strtodcxx . . $ strtod(d6476bb9e2fdf17c). (string, nullptr);
136 }
137
138 double Melder_atofcxx . . $ Melder_atof(13d725aae188c0cd). (conststring32cxx . . $ conststring32# string) {
139 if (! string)
140 return undefinedcxx . . $ undefined.;
141 const char32cxx . . $ char32# *endOfNumericString = findEndOfNumericStringcxx . . $ findEndOfNumericString(bd9f52c6ae4be57a). (string);
142 const bool weFoundANumber = !! endOfNumericString;
143 if (! weFoundANumber)
144 return undefinedcxx . . $ undefined.;
145 const integercxx . . $ integer# numberOfCharacters = endOfNumericString - & string [0];
146 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (numberOfCharacters > 0);
147 static MelderStringcxx . . $ MelderString# buffer;
148 MelderString_ncopycxx . . $ MelderString_ncopy(b76236fd155635ca). (& buffer, string, numberOfCharacters);
149 const char *string8 = Melder_peek32to8 (buffer.stringcxx . . $ MelderString#string.);
150 const integercxx . . $ integer# string8length = Melder8_lengthcxx . . $ Melder8_length(3c95bd14d5d5d286). (string8);
151 if (string8length < 1)
152 return undefinedcxx . . $ undefined.;
153 return string8 [string8length - 1] == '%' ? 0.01 * strtodcxx . . $ strtod(d6476bb9e2fdf17c). (string8, nullptr) : strtodcxx . . $ strtod(d6476bb9e2fdf17c). (string8, nullptr);
154 }
155
156 int64cxx . . $ int64# Melder_atoicxx . . $ Melder_atoi(c94c67ff4219bf83). (conststring32cxx . . $ conststring32# string) {
157 return strtoll (Melder_peek32to8 (string), nullptr, 10);
158 }
159
160 /* End of file melder_atof.cpp */