Mercurial > hg > orthanc-stone
comparison Framework/Toolbox/GenericToolbox.h @ 1166:f68da12e852b
Added scientific notation support to StringToDouble
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Wed, 20 Nov 2019 10:47:42 +0100 |
parents | 141593f1aa88 |
children | ba08f2b0a779 91d86144fb79 |
comparison
equal
deleted
inserted
replaced
1143:e2212644eab3 | 1166:f68da12e852b |
---|---|
20 | 20 |
21 #pragma once | 21 #pragma once |
22 | 22 |
23 #include <string> | 23 #include <string> |
24 #include <stdint.h> | 24 #include <stdint.h> |
25 #include <math.h> | |
26 | |
25 | 27 |
26 namespace OrthancStone | 28 namespace OrthancStone |
27 { | 29 { |
28 namespace GenericToolbox | 30 namespace GenericToolbox |
29 { | 31 { |
43 return false; | 45 return false; |
44 else | 46 else |
45 period++; | 47 period++; |
46 ++p; | 48 ++p; |
47 } | 49 } |
50 else if (*p == 'e' || *p == 'E') | |
51 { | |
52 ++p; | |
53 if (*p == '-' || *p == '+') | |
54 ++p; | |
55 // "e+"/"E+" "e-"/"E-" or "e"/"E" must be followed by a number | |
56 if (!(*p >= '0' && *p <= '9')) | |
57 return false; | |
58 | |
59 // these must be the last in the string | |
60 while(*p >= '0' && *p <= '9') | |
61 ++p; | |
62 | |
63 return (*p == 0); | |
64 } | |
48 else | 65 else |
66 { | |
49 return false; | 67 return false; |
68 } | |
50 } | 69 } |
51 return true; | 70 return true; |
52 } | 71 } |
53 | 72 |
54 inline bool LegitIntegerString(const char* text) | 73 inline bool LegitIntegerString(const char* text) |
126 ++n; | 145 ++n; |
127 } | 146 } |
128 r += f; | 147 r += f; |
129 } | 148 } |
130 r *= neg; | 149 r *= neg; |
131 if (*p == 0 || (*p >= '0' && *p <= '9') ) | 150 |
151 // skip the remaining numbers until we reach not-a-digit (either the | |
152 // end of the string OR the scientific notation symbol) | |
153 while ((*p >= '0' && *p <= '9')) | |
154 ++p; | |
155 | |
156 if (*p == 0 ) | |
157 { | |
132 return true; | 158 return true; |
159 } | |
160 else if ((*p == 'e') || (*p == 'E')) | |
161 { | |
162 // process the scientific notation | |
163 double sign; // no init is safe (read below) | |
164 ++p; | |
165 if (*p == '-') | |
166 { | |
167 sign = -1.0; | |
168 // point to first number | |
169 ++p; | |
170 } | |
171 else if (*p == '+') | |
172 { | |
173 sign = 1.0; | |
174 // point to first number | |
175 ++p; | |
176 } | |
177 else if (*p >= '0' && *p <= '9') | |
178 { | |
179 sign = 1.0; | |
180 } | |
181 else | |
182 { | |
183 // only a sign char or a number is allowed | |
184 return false; | |
185 } | |
186 // now p points to the absolute value of the exponent | |
187 double exp = 0; | |
188 while (*p >= '0' && *p <= '9') | |
189 { | |
190 exp = (exp * 10.0) + static_cast<double>(*p - '0'); // 1 12 123 123 12345 | |
191 ++p; | |
192 } | |
193 // now we have our exponent. put a sign on it. | |
194 exp *= sign; | |
195 double scFac = ::pow(10.0, exp); | |
196 r *= scFac; | |
197 | |
198 // only allowed symbol here is EOS | |
199 return (*p == 0); | |
200 } | |
133 else | 201 else |
134 return false; | 202 { |
203 // not allowed | |
204 return false; | |
205 } | |
135 } | 206 } |
136 | 207 |
137 inline bool StringToDouble(double& r, const std::string& text) | 208 inline bool StringToDouble(double& r, const std::string& text) |
138 { | 209 { |
139 return StringToDouble(r, text.c_str()); | 210 return StringToDouble(r, text.c_str()); |