Debian Codegraph @

thirdparty/thorvg/src/common/tvgStr.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved.
3
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #include "config.h"
24 #include <cmath>
25 #include <cstdlib>
26 #include <cstring>
27 #include <memory.h>
28 #include "tvgMath.h"
29 #include "tvgStr.h"
30
31
32 /************************************************************************/
33 /* Internal Class Implementation */
34 /************************************************************************/
35
36 static inline bool _floatExact(float a, float b)
37 {
38 return memcmp(&a, &b, sizeof(float)) == 0;
39 }
40
41
42 /************************************************************************/
43 /* External Class Implementation */
44 /************************************************************************/
45
46 namespace tvg {
47
48 /*
49 * https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/strtof-strtof-l-wcstof-wcstof-l?view=msvc-160
50 *
51 * src should be one of the following form :
52 *
53 * [whitespace] [sign] {digits [radix digits] | radix digits} [{e | E} [sign] digits]
54 * [whitespace] [sign] {INF | INFINITY}
55 * [whitespace] [sign] NAN [sequence]
56 *
57 * No hexadecimal form supported
58 * no sequence supported after NAN
59 */
60 float strToFloat(const char *nPtr, char **endPtr)
61 {
62 if (endPtr) *endPtr = (char *) (nPtr);
63 if (!nPtr) return 0.0f;
64
65 auto a = nPtr;
66 auto iter = nPtr;
67 auto val = 0.0f;
68 unsigned long long integerPart = 0;
69 int minus = 1;
70
71 //ignore leading whitespaces
72 while (isspace(*iter)) iter++;
73
74 //signed or not
75 if (*iter == '-') {
76 minus = -1;
77 iter++;
78 } else if (*iter == '+') {
79 iter++;
80 }
81
82 if (tolower(*iter) == 'i') {
83 if ((tolower(*(iter + 1)) == 'n') && (tolower(*(iter + 2)) == 'f')) iter += 3;
84 else goto error;
85
86 if (tolower(*(iter)) == 'i') {
87 if ((tolower(*(iter + 1)) == 'n') && (tolower(*(iter + 2)) == 'i') && (tolower(*(iter + 3)) == 't') &&
88 (tolower(*(iter + 4)) == 'y'))
89 iter += 5;
90 else goto error;
91 }
92 if (endPtr) *endPtr = (char *) (iter);
93 return (minus == -1) ? -INFINITY : INFINITY;
94 }
95
96 if (tolower(*iter) == 'n') {
97 if ((tolower(*(iter + 1)) == 'a') && (tolower(*(iter + 2)) == 'n')) iter += 3;
98 else goto error;
99
100 if (endPtr) *endPtr = (char *) (iter);
101 return (minus == -1) ? -NAN : NAN;
102 }
103
104 //Optional: integer part before dot
105 if (isdigit(*iter)) {
106 for (; isdigit(*iter); iter++) {
107 integerPart = integerPart * 10ULL + (unsigned long long) (*iter - '0');
108 }
109 a = iter;
110 } else if (*iter != '.') {
111 goto success;
112 }
113
114 val = static_cast<float>(integerPart);
115
116 //Optional: decimal part after dot
117 if (*iter == '.') {
118 unsigned long long decimalPart = 0;
119 unsigned long long pow10 = 1;
120 int count = 0;
121
122 iter++;
123
124 if (isdigit(*iter)) {
125 for (; isdigit(*iter); iter++, count++) {
126 if (count < 19) {
127 decimalPart = decimalPart * 10ULL + +static_cast<unsigned long long>(*iter - '0');
128 pow10 *= 10ULL;
129 }
130 }
131 } else if (isspace(*iter)) { //skip if there is a space after the dot.
132 a = iter;
133 goto success;
134 }
135
136 val += static_cast<float>(decimalPart) / static_cast<float>(pow10);
137 a = iter;
138 }
139
140 //Optional: exponent
141 if (*iter == 'e' || *iter == 'E') {
142 ++iter;
143
144 //Exception: svg may have 'em' unit for fonts. ex) 5em, 10.5em
145 if ((*iter == 'm') || (*iter == 'M')) {
146 //TODO: We don't support font em unit now, but has to multiply val * font size later...
147 a = iter + 1;
148 goto success;
149 }
150
151 //signed or not
152 int minus_e = 1;
153
154 if (*iter == '-') {
155 minus_e = -1;
156 ++iter;
157 } else if (*iter == '+') {
158 iter++;
159 }
160
161 unsigned int exponentPart = 0;
162
163 if (isdigit(*iter)) {
164 while (*iter == '0') iter++;
165 for (; isdigit(*iter); iter++) {
166 exponentPart = exponentPart * 10U + static_cast<unsigned int>(*iter - '0');
167 }
168 } else if (!isdigit(*(a - 1))) {
169 a = nPtr;
170 goto success;
171 } else if (*iter == 0) {
172 goto success;
173 }
174
175 //if ((_floatExact(val, 2.2250738585072011f)) && ((minus_e * static_cast<int>(exponentPart)) <= -308)) {
176 if ((_floatExact(val, 1.175494351f)) && ((minus_e * static_cast<int>(exponentPart)) <= -38)) {
177 //val *= 1.0e-308f;
178 val *= 1.0e-38f;
179 a = iter;
180 goto success;
181 }
182
183 a = iter;
184 auto scale = 1.0f;
185
186 while (exponentPart >= 8U) {
187 scale *= 1E8f;
188 exponentPart -= 8U;
189 }
190 while (exponentPart > 0U) {
191 scale *= 10.0f;
192 exponentPart--;
193 }
194 val = (minus_e == -1) ? (val / scale) : (val * scale);
195 } else if ((iter > nPtr) && !isdigit(*(iter - 1))) {
196 a = nPtr;
197 goto success;
198 }
199
200 success:
201 if (endPtr) *endPtr = (char *)(a);
202 if (!std::isfinite(val)) return 0.0f;
203
204 return minus * val;
205
206 error:
207 if (endPtr) *endPtr = (char *)(nPtr);
208 return 0.0f;
209 }
210
211 char* strDuplicate(const char *str, size_t n)
212 {
213 auto len = strlen(str);
214 if (len < n) n = len;
215
216 auto ret = (char *) malloc(n + 1);
217 if (!ret) return nullptr;
218 ret[n] = '\0';
219
220 return (char *) memcpy(ret, str, n);
221 }
222
223 char* strAppend(char* lhs, const char* rhs, size_t n)
224 {
225 if (!rhs) return lhs;
226 if (!lhs) return strDuplicate(rhs, n);
227 lhs = (char*)realloc(lhs, strlen(lhs) + n + 1);
228 return strncat(lhs, rhs, n);
229 }
230
231 char* strDirname(const char* path)
232 {
233 auto ptr = strrchr(path, '/');
234 #ifdef _WIN32
235 auto ptr2 = strrchr(ptr ? ptr : path, '\\');
236 if (ptr2) ptr = ptr2;
237 #endif
238 auto len = ptr ? size_t(ptr - path + 1) : SIZE_MAX;
239 return strDuplicate(path, len);
240 }
241
242 }