@@ -203,27 +203,27 @@ float clamp_css_float(T f) { // Clamp to float 0.0 .. 1.0.
203
203
return f < 0 ? 0 : f > 1 ? 1 : f;
204
204
}
205
205
206
- float parseFloat (const std::string& str) {
207
- return strtof (str. c_str () , nullptr );
206
+ float parseFloat (const char * str) {
207
+ return strtof (str, nullptr );
208
208
}
209
209
210
- int64_t parseInt (const std::string& str, uint8_t base = 10 ) {
211
- return strtoll (str. c_str () , nullptr , base);
210
+ int64_t parseInt (const char * str, uint8_t base = 10 ) {
211
+ return strtoll (str, nullptr , base);
212
212
}
213
213
214
214
uint8_t parse_css_int (const std::string& str) { // int or percentage.
215
215
if (str.length () && str.back () == ' %' ) {
216
- return clamp_css_byte (parseFloat (str) / 100 .0f * 255 .0f );
216
+ return clamp_css_byte (parseFloat (str. c_str () ) / 100 .0f * 255 .0f );
217
217
} else {
218
- return clamp_css_byte (parseInt (str));
218
+ return clamp_css_byte (parseInt (str. c_str () ));
219
219
}
220
220
}
221
221
222
222
float parse_css_float (const std::string& str) { // float or percentage.
223
223
if (str.length () && str.back () == ' %' ) {
224
- return clamp_css_float (parseFloat (str) / 100 .0f );
224
+ return clamp_css_float (parseFloat (str. c_str () ) / 100 .0f );
225
225
} else {
226
- return clamp_css_float (parseFloat (str));
226
+ return clamp_css_float (parseFloat (str. c_str () ));
227
227
}
228
228
}
229
229
@@ -259,18 +259,11 @@ std::vector<std::string> split(const std::string& s, char delim) {
259
259
}
260
260
261
261
Color CSSColorParser::parse (const std::string& css_str) {
262
- std::string str = css_str;
263
-
264
- // Remove all whitespace, not compliant, but should just be more accepting.
265
- str.erase (std::remove (str.begin (), str.end (), ' ' ), str.end ());
266
-
267
- // Convert to lowercase.
268
- std::transform (str.begin (), str.end (), str.begin (), ::tolower);
269
262
270
263
// #abc and #abc123 syntax.
271
- if (str .length () && str .front () == ' #' ) {
272
- if (str .length () == 4 ) {
273
- int64_t iv = parseInt (str. substr ( 1 ) , 16 ); // TODO(deanm): Stricter parsing.
264
+ if (css_str .length () && css_str .front () == ' #' ) {
265
+ if (css_str .length () == 4 ) {
266
+ int64_t iv = parseInt (css_str. c_str ()+ 1 , 16 ); // TODO(deanm): Stricter parsing.
274
267
if (!(iv >= 0 && iv <= 0xfff )) {
275
268
return {};
276
269
} else {
@@ -281,8 +274,8 @@ Color CSSColorParser::parse(const std::string& css_str) {
281
274
1
282
275
};
283
276
}
284
- } else if (str .length () == 7 ) {
285
- int64_t iv = parseInt (str. substr ( 1 ) , 16 ); // TODO(deanm): Stricter parsing.
277
+ } else if (css_str .length () == 7 ) {
278
+ int64_t iv = parseInt (css_str. c_str ()+ 1 , 16 ); // TODO(deanm): Stricter parsing.
286
279
if (!(iv >= 0 && iv <= 0xffffff )) {
287
280
return {}; // Covers NaN.
288
281
} else {
@@ -298,6 +291,15 @@ Color CSSColorParser::parse(const std::string& css_str) {
298
291
return {};
299
292
}
300
293
294
+ // TODO avoid copy
295
+ std::string str = css_str;
296
+
297
+ // Remove all whitespace, not compliant, but should just be more accepting.
298
+ str.erase (std::remove (str.begin (), str.end (), ' ' ), str.end ());
299
+
300
+ // Convert to lowercase.
301
+ std::transform (str.begin (), str.end (), str.begin (), ::tolower);
302
+
301
303
size_t op = str.find_first_of (' (' ), ep = str.find_first_of (' )' );
302
304
if (op != std::string::npos && ep + 1 == str.length ()) {
303
305
const std::string fname = str.substr (0 , op);
@@ -336,7 +338,7 @@ Color CSSColorParser::parse(const std::string& css_str) {
336
338
}
337
339
}
338
340
339
- float h = parseFloat (params[0 ]) / 360 .0f ;
341
+ float h = parseFloat (params[0 ]. c_str () ) / 360 .0f ;
340
342
while (h < 0 .0f ) h++;
341
343
while (h > 1 .0f ) h--;
342
344
0 commit comments