@@ -55,6 +55,34 @@ int main() {
5555}
5656```
5757
58+ You can parse delimited numbers:
59+ ``` C++
60+ const std::string input = " 234532.3426362,7869234.9823,324562.645" ;
61+ double result;
62+ auto answer = fast_float::from_chars(input.data(), input.data()+input.size(), result);
63+ if (answer.ec != std::errc()) {
64+ // check error
65+ }
66+ // we have result == 234532.3426362.
67+ if(answer.ptr[ 0] != ',') {
68+ // unexpected delimiter
69+ }
70+ answer = fast_float::from_chars(answer.ptr + 1, input.data()+input.size(), result);
71+ if(answer.ec != std::errc()) {
72+ // check error
73+ }
74+ // we have result == 7869234.9823.
75+ if(answer.ptr[ 0] != ',') {
76+ // unexpected delimiter
77+ }
78+ answer = fast_float::from_chars(answer.ptr + 1, input.data()+input.size(), result);
79+ if(answer.ec != std::errc()) {
80+ // check error
81+ }
82+ // we have result == 324562.645.
83+ ```
84+
85+
5886
5987Like the C++17 standard, the `fast_float::from_chars` functions take an optional last argument of
6088the type `fast_float::chars_format`. It is a bitset value: we check whether
@@ -115,7 +143,7 @@ int main() {
115143}
116144```
117145
118- ## Using commas as decimal separator
146+ ## Advanced options: using commas as decimal separator, JSON and Fortran
119147
120148
121149The C++ standard stipulate that ` from_chars ` has to be locale-independent. In
@@ -140,33 +168,72 @@ int main() {
140168}
141169```
142170
143- You can parse delimited numbers:
171+ You can also parse Fortran-like inputs:
172+
144173``` C++
145- const std::string input = " 234532.3426362,7869234.9823,324562.645" ;
146- double result;
147- auto answer = fast_float::from_chars(input.data(), input.data()+input.size(), result);
148- if (answer.ec != std::errc()) {
149- // check error
150- }
151- // we have result == 234532.3426362.
152- if(answer.ptr[ 0] != ',') {
153- // unexpected delimiter
154- }
155- answer = fast_float::from_chars(answer.ptr + 1, input.data()+input.size(), result);
156- if(answer.ec != std::errc()) {
157- // check error
158- }
159- // we have result == 7869234.9823.
160- if(answer.ptr[ 0] != ',') {
161- // unexpected delimiter
162- }
163- answer = fast_float::from_chars(answer.ptr + 1, input.data()+input.size(), result);
164- if(answer.ec != std::errc()) {
165- // check error
166- }
167- // we have result == 324562.645.
174+ #include " fast_float/fast_float.h"
175+ #include < iostream>
176+
177+ int main () {
178+ const std::string input = "1d+4";
179+ double result;
180+ fast_float::parse_options options{ fast_float::chars_format::fortran };
181+ auto answer = fast_float::from_chars_advanced(input.data(), input.data()+input.size(), result, options);
182+ if((answer.ec != std::errc()) || ((result != 10000))) { std::cerr << "parsing failure\n"; return EXIT_FAILURE; }
183+ std::cout << "parsed the number " << result << std::endl;
184+ return EXIT_SUCCESS;
185+ }
168186```
169187
188+ You may also enforce the JSON format ([ RFC 8259] ( https://datatracker.ietf.org/doc/html/rfc8259#section-6 ) ):
189+
190+
191+ ``` C++
192+ #include " fast_float/fast_float.h"
193+ #include < iostream>
194+
195+ int main () {
196+ const std::string input = "+.1"; // not valid
197+ double result;
198+ fast_float::parse_options options{ fast_float::chars_format::json };
199+ auto answer = fast_float::from_chars_advanced(input.data(), input.data()+input.size(), result, options);
200+ if(answer.ec == std::errc()) { std::cerr << "should have failed\n"; return EXIT_FAILURE; }
201+ return EXIT_SUCCESS;
202+ }
203+ ```
204+
205+ By default the JSON format does not allow ` inf ` :
206+
207+ ``` C++
208+
209+ #include " fast_float/fast_float.h"
210+ #include < iostream>
211+
212+ int main () {
213+ const std::string input = "inf"; // not valid in JSON
214+ double result;
215+ fast_float::parse_options options{ fast_float::chars_format::json };
216+ auto answer = fast_float::from_chars_advanced(input.data(), input.data()+input.size(), result, options);
217+ if(answer.ec == std::errc()) { std::cerr << "should have failed\n"; return EXIT_FAILURE; }
218+ }
219+ ```
220+
221+
222+ You can allow it with a non-standard ` json_or_infnan ` variant:
223+
224+ ``` C++
225+ #include " fast_float/fast_float.h"
226+ #include < iostream>
227+
228+ int main () {
229+ const std::string input = "inf"; // not valid in JSON but we allow it with json_or_infnan
230+ double result;
231+ fast_float::parse_options options{ fast_float::chars_format::json_or_infnan };
232+ auto answer = fast_float::from_chars_advanced(input.data(), input.data()+input.size(), result, options);
233+ if(answer.ec != std::errc() || (!std::isinf(result))) { std::cerr << "should have parsed infinity\n"; return EXIT_FAILURE; }
234+ return EXIT_SUCCESS;
235+ }
236+ ``````
170237
171238## Relation With Other Work
172239
0 commit comments