Blender V5.3
io/common/intern/string_utils.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "IO_string_utils.hh"
6
7/* NOTE: we could use C++17 <charconv> from_chars to parse
8 * floats, but even if some compilers claim full support,
9 * their standard libraries are not quite there yet.
10 * LLVM/libc++ only has a float parser since LLVM 14,
11 * and GCC/libstdc++ since 11.1. So until at least these are
12 * the minimum spec, use an external library. */
13#include "fast_float.h"
14#include <charconv>
15
16namespace blender::io {
17
19{
20 const char *start = buffer.begin();
21 const char *end = buffer.end();
22 size_t len = 0;
23 const char *ptr = start;
24 while (ptr < end) {
25 char c = *ptr++;
26 if (c == '\n') {
27 break;
28 }
29 ++len;
30 }
31
32 buffer = StringRef(ptr, end);
33 return StringRef(start, len);
34}
35
36static bool is_whitespace(char c)
37{
38 return c <= ' ';
39}
40
41const char *drop_whitespace(const char *p, const char *end)
42{
43 while (p < end && is_whitespace(*p)) {
44 ++p;
45 }
46 return p;
47}
48
49const char *drop_non_whitespace(const char *p, const char *end)
50{
51 while (p < end && !is_whitespace(*p)) {
52 ++p;
53 }
54 return p;
55}
56
57static const char *drop_sign(const char *p, const char *end, int &sign)
58{
59 sign = 1;
60 if (p < end) {
61 if (*p == '+') {
62 ++p;
63 }
64 if (*p == '-') {
65 sign = -1;
66 ++p;
67 }
68 }
69 return p;
70}
71
72const char *try_parse_float(
73 const char *p, const char *end, int fallback, bool &success, float &dst, bool skip_space)
74{
75 if (skip_space) {
76 p = drop_whitespace(p, end);
77 }
78 int sign = 0;
79 p = drop_sign(p, end, sign);
80 fast_float::from_chars_result res = fast_float::from_chars(p, end, dst);
81 if (ELEM(res.ec, std::errc::invalid_argument, std::errc::result_out_of_range) || res.ptr < end) {
82 dst = fallback;
83 success = false;
84 }
85 else {
86 dst *= sign;
87 success = true;
88 }
89 return res.ptr;
90}
91
92const char *try_parse_int(
93 const char *p, const char *end, int fallback, bool &success, int &dst, bool skip_space)
94{
95 if (skip_space) {
96 p = drop_whitespace(p, end);
97 }
98 int sign = 0;
99 p = drop_sign(p, end, sign);
100 std::from_chars_result res = std::from_chars(p, end, dst);
101 if (ELEM(res.ec, std::errc::invalid_argument, std::errc::result_out_of_range) || res.ptr < end) {
102 dst = fallback;
103 success = false;
104 }
105 else {
106 dst *= sign;
107 success = true;
108 }
109 return res.ptr;
110}
111
112static const char *drop_plus(const char *p, const char *end)
113{
114 if (p < end && *p == '+') {
115 ++p;
116 }
117 return p;
118}
119
120const char *parse_float(const char *p,
121 const char *end,
122 float fallback,
123 float &dst,
124 bool skip_space,
125 bool require_trailing_space)
126{
127 if (skip_space) {
128 p = drop_whitespace(p, end);
129 }
130 p = drop_plus(p, end);
131 fast_float::from_chars_result res = fast_float::from_chars(p, end, dst);
132 if (ELEM(res.ec, std::errc::invalid_argument, std::errc::result_out_of_range)) {
133 dst = fallback;
134 }
135 else if (require_trailing_space && res.ptr < end && !is_whitespace(*res.ptr)) {
136 /* If there are trailing non-space characters, do not eat up the number. */
137 dst = fallback;
138 return p;
139 }
140 return res.ptr;
141}
142
143const char *parse_floats(const char *p,
144 const char *end,
145 float fallback,
146 float *dst,
147 int count,
148 bool require_trailing_space)
149{
150 for (int i = 0; i < count; ++i) {
151 p = parse_float(p, end, fallback, dst[i], true, require_trailing_space);
152 }
153 return p;
154}
155
156const char *parse_int(const char *p, const char *end, int fallback, int &dst, bool skip_space)
157{
158 if (skip_space) {
159 p = drop_whitespace(p, end);
160 }
161 p = drop_plus(p, end);
162 std::from_chars_result res = std::from_chars(p, end, dst);
163 if (ELEM(res.ec, std::errc::invalid_argument, std::errc::result_out_of_range)) {
164 dst = fallback;
165 }
166 return res.ptr;
167}
168
169} // namespace blender::io
#define ELEM(...)
constexpr StringRef()
constexpr const char * end() const
constexpr const char * begin() const
constexpr T sign(T) RET
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
const char * parse_float(const char *p, const char *end, float fallback, float &dst, bool skip_space, bool require_trailing_space)
static const char * drop_plus(const char *p, const char *end)
const char * parse_floats(const char *p, const char *end, float fallback, float *dst, int count, bool require_trailing_space)
const char * drop_non_whitespace(const char *p, const char *end)
static bool is_whitespace(char c)
StringRef read_next_line(StringRef &buffer)
const char * try_parse_int(const char *p, const char *end, int fallback, bool &success, int &dst, bool skip_space)
const char * parse_int(const char *p, const char *end, int fallback, int &dst, bool skip_space)
static const char * drop_sign(const char *p, const char *end, int &sign)
const char * drop_whitespace(const char *p, const char *end)
const char * try_parse_float(const char *p, const char *end, int fallback, bool &success, float &dst, bool skip_space)