@@ -250,8 +250,8 @@ bool match(char c, const std::string& text, size_t& pos, size_t end) {
250
250
return true ;
251
251
}
252
252
253
- bool match_prefix (const std::string& prefix, const std::string& text, size_t & pos, size_t end) {
254
- size_t length = prefix. size ( );
253
+ bool match_prefix (const char * prefix, const std::string& text, size_t & pos, size_t end) {
254
+ size_t length = strlen (prefix );
255
255
if (length + pos > end)
256
256
return false ;
257
257
@@ -269,28 +269,28 @@ void skip_whitespace(const std::string& text, size_t& pos, size_t end){
269
269
}
270
270
}
271
271
272
- void parseHexRGB (const std::string& css_str, size_t pos, size_t end, bool & valid, Color& color ) {
272
+ Color parseHexRGB (const std::string& css_str, size_t pos, size_t end, bool & valid) {
273
273
274
274
int read = 0 ;
275
275
276
276
// const char* str = css_str.c_str() + pos;
277
277
int64_t iv = parse_int (css_str, pos, read, 16 );
278
278
if (iv < 0 ) {
279
279
// Invalid: out of range.
280
- return ;
280
+ return {} ;
281
281
}
282
282
283
283
pos += read;
284
284
skip_whitespace (css_str, pos, end);
285
285
if (pos != end) {
286
286
// Invalid: contains trailing chars.
287
- return ;
287
+ return {} ;
288
288
}
289
289
290
290
if (read == 3 ) { // rgb
291
291
if (iv <= 0xfff ) {
292
292
valid = true ;
293
- color = {
293
+ return {
294
294
static_cast <uint8_t >(((iv & 0xf00 ) >> 4 ) | ((iv & 0xf00 ) >> 8 )),
295
295
static_cast <uint8_t >((iv & 0xf0 ) | ((iv & 0xf0 ) >> 4 )),
296
296
static_cast <uint8_t >((iv & 0xf ) | ((iv & 0xf ) << 4 )),
@@ -300,7 +300,7 @@ void parseHexRGB(const std::string& css_str, size_t pos, size_t end, bool& valid
300
300
} else if (read == 6 ) { // rrggbb
301
301
if (iv <= 0xffffff ) {
302
302
valid = true ;
303
- color = {
303
+ return {
304
304
static_cast <uint8_t >((iv & 0xff0000 ) >> 16 ),
305
305
static_cast <uint8_t >((iv & 0xff00 ) >> 8 ),
306
306
static_cast <uint8_t >(iv & 0xff ),
@@ -310,7 +310,7 @@ void parseHexRGB(const std::string& css_str, size_t pos, size_t end, bool& valid
310
310
} else if (read == 4 ) { // argb
311
311
if (iv <= 0xffff ) {
312
312
valid = true ;
313
- color = {
313
+ return {
314
314
static_cast <uint8_t >(((iv & 0xf00 ) >> 4 ) | ((iv & 0xf00 ) >> 8 )),
315
315
static_cast <uint8_t >((iv & 0xf0 ) | ((iv & 0xf0 ) >> 4 )),
316
316
static_cast <uint8_t >((iv & 0xf ) | ((iv & 0xf ) << 4 )),
@@ -320,38 +320,34 @@ void parseHexRGB(const std::string& css_str, size_t pos, size_t end, bool& valid
320
320
} else if (read == 8 ) { // aarrggbb
321
321
if (iv <= 0xffffffff ) {
322
322
valid = true ;
323
- color = {
323
+ return {
324
324
static_cast <uint8_t >((iv & 0xff0000 ) >> 16 ),
325
325
static_cast <uint8_t >((iv & 0xff00 ) >> 8 ),
326
326
static_cast <uint8_t >(iv & 0xff ),
327
327
static_cast <uint8_t >((iv & 0xff000000 ) >> 24 ) / 255 .0f ,
328
328
};
329
329
}
330
330
}
331
+ return {};
331
332
}
332
333
333
- void parseRGB (const std::string& css_str, size_t pos, size_t end, bool & matched, bool & valid, Color& color) {
334
-
335
- if (!match_prefix (" rgb" , css_str, pos, end))
336
- return ;
337
-
338
- matched = true ;
334
+ Color parseRGB (const std::string& css_str, size_t pos, size_t end, bool & valid) {
339
335
340
336
bool hasAlpha = match (' a' , css_str, pos, end);
341
337
342
338
skip_whitespace (css_str, pos, end);
343
339
344
- if (!match (' (' , css_str, pos, end)) { return ; }
340
+ if (!match (' (' , css_str, pos, end)) { return {} ; }
345
341
346
342
float values[4 ] = { 0 , 0 , 0 , 1 };
347
343
348
344
for (int i = 0 ; i < (hasAlpha ? 4 : 3 ); i++) {
349
- if (i > 0 && !match (' ,' , css_str, pos, end)) { return ; }
345
+ if (i > 0 && !match (' ,' , css_str, pos, end)) { return {} ; }
350
346
351
347
int read = 0 ;
352
348
values[i] = parse_float (css_str, pos, read);
353
349
354
- if (read == 0 ) { return ; }
350
+ if (read == 0 ) { return {} ; }
355
351
pos += read;
356
352
357
353
if (match (' %' , css_str, pos, end)) {
@@ -363,38 +359,34 @@ void parseRGB(const std::string& css_str, size_t pos, size_t end, bool& matched,
363
359
}
364
360
skip_whitespace (css_str, pos, end);
365
361
}
366
- if (!match (' )' , css_str, pos, end)) { return ; }
362
+ if (!match (' )' , css_str, pos, end)) { return {} ; }
367
363
368
364
valid = true ;
369
- color = {
365
+ return {
370
366
clamp_css_byte (values[0 ]),
371
367
clamp_css_byte (values[1 ]),
372
368
clamp_css_byte (values[2 ]),
373
369
values[3 ]
374
370
};
375
371
}
376
372
377
- void parseHSL (const std::string& css_str, size_t pos, size_t end, bool & matched, bool & valid, Color& color) {
378
- if (!match_prefix (" hsl" , css_str, pos, end))
379
- return ;
380
-
381
- matched = true ;
373
+ Color parseHSL (const std::string& css_str, size_t pos, size_t end, bool & valid) {
382
374
383
375
bool hasAlpha = match (' a' , css_str, pos, end);
384
376
385
377
skip_whitespace (css_str, pos, end);
386
378
387
- if (!match (' (' , css_str, pos, end)) { return ; }
379
+ if (!match (' (' , css_str, pos, end)) { return {} ; }
388
380
389
381
float values[4 ] = { 0 , 0 , 0 , 1 };
390
382
391
383
for (int i = 0 ; i < (hasAlpha ? 4 : 3 ); i++) {
392
- if (i > 0 && !match (' ,' , css_str, pos, end)) { return ; }
384
+ if (i > 0 && !match (' ,' , css_str, pos, end)) { return {} ; }
393
385
394
386
int read = 0 ;
395
387
values[i] = parse_float (css_str, pos, read);
396
388
397
- if (read == 0 ) { return ; }
389
+ if (read == 0 ) { return {} ; }
398
390
pos += read;
399
391
400
392
if (match (' %' , css_str, pos, end)) {
@@ -405,7 +397,7 @@ void parseHSL(const std::string& css_str, size_t pos, size_t end, bool& matched,
405
397
}
406
398
skip_whitespace (css_str, pos, end);
407
399
}
408
- if (!match (' )' , css_str, pos, end)) { return ; }
400
+ if (!match (' )' , css_str, pos, end)) { return {} ; }
409
401
410
402
float h = values[0 ] / 360 .0f ;
411
403
while (h < 0 .0f ) h++;
@@ -420,21 +412,21 @@ void parseHSL(const std::string& css_str, size_t pos, size_t end, bool& matched,
420
412
float m1 = l * 2 .0f - m2;
421
413
422
414
valid = true ;
423
- color = {
415
+ return {
424
416
clamp_css_byte (css_hue_to_rgb (m1, m2, h + 1 .0f / 3 .0f ) * 255 .0f ),
425
417
clamp_css_byte (css_hue_to_rgb (m1, m2, h) * 255 .0f ),
426
418
clamp_css_byte (css_hue_to_rgb (m1, m2, h - 1 .0f / 3 .0f ) * 255 .0f ),
427
419
values[3 ]
428
420
};
429
421
}
430
422
431
- void parseNamedColor (const std::string& css_str, size_t pos, size_t end, bool & valid, Color& color ){
423
+ Color parseNamedColor (const std::string& css_str, size_t pos, size_t end, bool & valid){
432
424
// TODO: ignore trailing whitespace?
433
425
size_t length = end - pos;
434
426
435
427
// Skip if longer than longest named color
436
428
if (length > 20 )
437
- return ;
429
+ return {} ;
438
430
439
431
// Convert to lowercase.
440
432
char cstr[32 ];
@@ -452,8 +444,9 @@ void parseNamedColor(const std::string& css_str, size_t pos, size_t end, bool& v
452
444
453
445
if (it != itEnd && std::strcmp (it->name , cstr) == 0 ) {
454
446
valid = true ;
455
- color = it->color ;
447
+ return it->color ;
456
448
}
449
+ return {};
457
450
}
458
451
459
452
@@ -472,33 +465,30 @@ Color CSSColorParser::parse(const std::string& css_str, bool& valid) {
472
465
473
466
size_t pos = 0 ;
474
467
size_t end = css_str.length ();
475
- bool matched = false ;
476
- Color color;
477
468
478
469
skip_whitespace (css_str, pos, end);
479
470
if (pos == end) {
480
471
return {};
481
472
}
482
473
483
- switch (*( css_str. c_str () + pos) ) {
474
+ switch (css_str[ pos] ) {
484
475
case ' #' :
485
- parseHexRGB (css_str, pos+1 , end, valid, color);
486
- matched = true ;
487
- break ;
476
+ return parseHexRGB (css_str, pos+1 , end, valid);
488
477
489
478
case ' r' :
490
479
case ' R' :
491
- parseRGB (css_str, pos, end, matched, valid, color);
480
+ if (match_prefix (" rgb" , css_str, pos, end)) {
481
+ return parseRGB (css_str, pos, end, valid);
482
+ }
492
483
break ;
493
484
494
485
case ' h' :
495
486
case ' H' :
496
- parseHSL (css_str, pos, end, matched, valid, color);
487
+ if (match_prefix (" hsl" , css_str, pos, end)) {
488
+ return parseHSL (css_str, pos, end, valid);
489
+ }
497
490
break ;
498
491
}
499
492
500
- if (!matched)
501
- parseNamedColor (css_str, pos, end, valid, color);
502
-
503
- return color;
493
+ return parseNamedColor (css_str, pos, end, valid);
504
494
}
0 commit comments