Branch data TLA Line data Source code
1 : : // Copyright (c) 2024-2025 xlnt-community
2 : : //
3 : : // Permission is hereby granted, free of charge, to any person obtaining a copy
4 : : // of this software and associated documentation files (the "Software"), to deal
5 : : // in the Software without restriction, including without limitation the rights
6 : : // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 : : // copies of the Software, and to permit persons to whom the Software is
8 : : // furnished to do so, subject to the following conditions:
9 : : //
10 : : // The above copyright notice and this permission notice shall be included in
11 : : // all copies or substantial portions of the Software.
12 : : //
13 : : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 : : // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 : : // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 : : // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 : : // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 : : // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 : : // THE SOFTWARE
20 : : //
21 : : // @license: http://www.opensource.org/licenses/mit-license.php
22 : : // @author: see AUTHORS file
23 : :
24 : : #pragma once
25 : :
26 : : #include <string>
27 : : #include <system_error>
28 : : #include <fast_float/fast_float.h>
29 : : #include <xlnt/xlnt_config.hpp>
30 : : #include <detail/xlnt_config_impl.hpp>
31 : :
32 : : /// The following parsers offer the same convenience as C++'s parsers (std::stoll, std::stod, etc.) but do NOT require exception handling,
33 : : /// resulting in improved performance and convenience to the programmer (exception handling is not required everywhere).
34 : : namespace xlnt {
35 : : namespace detail {
36 : :
37 : : /// ----- INTERNAL FUNCTIONS -----
38 : : namespace internal {
39 : : static constexpr fast_float::chars_format FAST_FLOAT_FORMAT =
40 : : fast_float::chars_format::general |
41 : : fast_float::chars_format::allow_leading_plus |
42 : : fast_float::chars_format::skip_white_space;
43 : : }
44 : :
45 : : /// ----- INTEGER PARSING -----
46 : :
47 : : template <typename T, typename std::enable_if<fast_float::is_supported_integer_type<T>::value, bool>::type = true>
48 :CBC 12 : std::errc parse(const char *string, T &result, const char **end = nullptr, int base = 10)
49 : : {
50 : 12 : fast_float::parse_options options {
51 : : internal::FAST_FLOAT_FORMAT,
52 : : '.',
53 : : base
54 : : };
55 : :
56 : 12 : auto parsing_result = fast_float::from_chars_int_advanced(string, string + strlen(string), result, options);
57 : :
58 [ + - ]: 12 : if (end != nullptr)
59 : : {
60 : 12 : *end = parsing_result.ptr;
61 : : }
62 : :
63 : 12 : return parsing_result.ec;
64 : : }
65 : :
66 : : template <typename T, typename std::enable_if<fast_float::is_supported_integer_type<T>::value, bool>::type = true>
67 : 6977 : std::errc parse(const std::string &string, T &result, std::size_t *num_characters_parsed = nullptr, int base = 10)
68 : : {
69 : 6977 : fast_float::parse_options options {
70 : : internal::FAST_FLOAT_FORMAT,
71 : : '.',
72 : : base
73 : : };
74 : :
75 : 6977 : auto parsing_result = fast_float::from_chars_int_advanced(string.c_str(), string.c_str() + string.length(), result, options);
76 : :
77 [ + + ]: 6977 : if (num_characters_parsed != nullptr)
78 : : {
79 : 52 : *num_characters_parsed = parsing_result.ptr - string.c_str();
80 : : }
81 : :
82 : 6977 : return parsing_result.ec;
83 : : }
84 : :
85 : :
86 : : /// ----- FLOATING-POINT NUMBER PARSING -----
87 : :
88 : : template <typename T, typename std::enable_if<fast_float::is_supported_float_type<T>::value, bool>::type = true>
89 : 10 : std::errc parse(const char *string, T &result, const char **end = nullptr, char decimal_separator = '.')
90 : : {
91 : 10 : fast_float::parse_options options {
92 : : internal::FAST_FLOAT_FORMAT,
93 : : decimal_separator
94 : : };
95 : :
96 : 10 : auto parsing_result = fast_float::from_chars_float_advanced(string, string + strlen(string), result, options);
97 : :
98 [ + + ]: 10 : if (end != nullptr)
99 : : {
100 : 6 : *end = parsing_result.ptr;
101 : : }
102 : :
103 : 10 : return parsing_result.ec;
104 : : }
105 : :
106 : : template <typename T, typename std::enable_if<fast_float::is_supported_float_type<T>::value, bool>::type = true>
107 : 4454 : std::errc parse(const std::string &string, T &result, std::size_t *num_characters_parsed = nullptr, char decimal_separator = '.')
108 : : {
109 : 4454 : fast_float::parse_options options {
110 : : internal::FAST_FLOAT_FORMAT,
111 : : decimal_separator
112 : : };
113 : :
114 : 4454 : auto parsing_result = fast_float::from_chars_float_advanced(string.c_str(), string.c_str() + string.length(), result, options);
115 : :
116 [ + + ]: 4454 : if (num_characters_parsed != nullptr)
117 : : {
118 : 17 : *num_characters_parsed = parsing_result.ptr - string.c_str();
119 : : }
120 : :
121 : 4454 : return parsing_result.ec;
122 : : }
123 : :
124 : : } // namespace detail
125 : : } // namespace xlnt
|