Skip to content

Commit 44fc7f3

Browse files
committed
change back to return color
--alloc in prefix match
1 parent c2052a6 commit 44fc7f3

File tree

1 file changed

+35
-45
lines changed

1 file changed

+35
-45
lines changed

csscolorparser.cpp

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ bool match(char c, const std::string& text, size_t& pos, size_t end) {
250250
return true;
251251
}
252252

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);
255255
if (length + pos > end)
256256
return false;
257257

@@ -269,28 +269,28 @@ void skip_whitespace(const std::string& text, size_t& pos, size_t end){
269269
}
270270
}
271271

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) {
273273

274274
int read = 0;
275275

276276
//const char* str = css_str.c_str() + pos;
277277
int64_t iv = parse_int(css_str, pos, read, 16);
278278
if (iv < 0) {
279279
// Invalid: out of range.
280-
return;
280+
return {};
281281
}
282282

283283
pos += read;
284284
skip_whitespace(css_str, pos, end);
285285
if (pos != end) {
286286
// Invalid: contains trailing chars.
287-
return;
287+
return {};
288288
}
289289

290290
if (read == 3) { // rgb
291291
if (iv <= 0xfff) {
292292
valid = true;
293-
color = {
293+
return {
294294
static_cast<uint8_t>(((iv & 0xf00) >> 4) | ((iv & 0xf00) >> 8)),
295295
static_cast<uint8_t>((iv & 0xf0) | ((iv & 0xf0) >> 4)),
296296
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
300300
} else if (read == 6) { // rrggbb
301301
if (iv <= 0xffffff) {
302302
valid = true;
303-
color = {
303+
return {
304304
static_cast<uint8_t>((iv & 0xff0000) >> 16),
305305
static_cast<uint8_t>((iv & 0xff00) >> 8),
306306
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
310310
} else if (read == 4) { // argb
311311
if (iv <= 0xffff) {
312312
valid = true;
313-
color = {
313+
return {
314314
static_cast<uint8_t>(((iv & 0xf00) >> 4) | ((iv & 0xf00) >> 8)),
315315
static_cast<uint8_t>((iv & 0xf0) | ((iv & 0xf0) >> 4)),
316316
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
320320
} else if (read == 8) { // aarrggbb
321321
if (iv <= 0xffffffff) {
322322
valid = true;
323-
color = {
323+
return {
324324
static_cast<uint8_t>((iv & 0xff0000) >> 16),
325325
static_cast<uint8_t>((iv & 0xff00) >> 8),
326326
static_cast<uint8_t>(iv & 0xff),
327327
static_cast<uint8_t>((iv & 0xff000000) >> 24) / 255.0f,
328328
};
329329
}
330330
}
331+
return {};
331332
}
332333

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) {
339335

340336
bool hasAlpha = match('a', css_str, pos, end);
341337

342338
skip_whitespace(css_str, pos, end);
343339

344-
if (!match('(', css_str, pos, end)) { return; }
340+
if (!match('(', css_str, pos, end)) { return {}; }
345341

346342
float values[4] = { 0, 0, 0, 1 };
347343

348344
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 {}; }
350346

351347
int read = 0;
352348
values[i] = parse_float(css_str, pos, read);
353349

354-
if (read == 0) { return; }
350+
if (read == 0) { return {}; }
355351
pos += read;
356352

357353
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,
363359
}
364360
skip_whitespace(css_str, pos, end);
365361
}
366-
if (!match(')', css_str, pos, end)) { return; }
362+
if (!match(')', css_str, pos, end)) { return {}; }
367363

368364
valid = true;
369-
color = {
365+
return {
370366
clamp_css_byte(values[0]),
371367
clamp_css_byte(values[1]),
372368
clamp_css_byte(values[2]),
373369
values[3]
374370
};
375371
}
376372

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) {
382374

383375
bool hasAlpha = match('a', css_str, pos, end);
384376

385377
skip_whitespace(css_str, pos, end);
386378

387-
if (!match('(', css_str, pos, end)) { return; }
379+
if (!match('(', css_str, pos, end)) { return {}; }
388380

389381
float values[4] = { 0, 0, 0, 1 };
390382

391383
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 {}; }
393385

394386
int read = 0;
395387
values[i] = parse_float(css_str, pos, read);
396388

397-
if (read == 0) { return; }
389+
if (read == 0) { return {}; }
398390
pos += read;
399391

400392
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,
405397
}
406398
skip_whitespace(css_str, pos, end);
407399
}
408-
if (!match(')', css_str, pos, end)) { return; }
400+
if (!match(')', css_str, pos, end)) { return {}; }
409401

410402
float h = values[0] / 360.0f;
411403
while (h < 0.0f) h++;
@@ -420,21 +412,21 @@ void parseHSL(const std::string& css_str, size_t pos, size_t end, bool& matched,
420412
float m1 = l * 2.0f - m2;
421413

422414
valid = true;
423-
color = {
415+
return {
424416
clamp_css_byte(css_hue_to_rgb(m1, m2, h + 1.0f / 3.0f) * 255.0f),
425417
clamp_css_byte(css_hue_to_rgb(m1, m2, h) * 255.0f),
426418
clamp_css_byte(css_hue_to_rgb(m1, m2, h - 1.0f / 3.0f) * 255.0f),
427419
values[3]
428420
};
429421
}
430422

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){
432424
// TODO: ignore trailing whitespace?
433425
size_t length = end - pos;
434426

435427
// Skip if longer than longest named color
436428
if (length > 20)
437-
return;
429+
return {};
438430

439431
// Convert to lowercase.
440432
char cstr[32];
@@ -452,8 +444,9 @@ void parseNamedColor(const std::string& css_str, size_t pos, size_t end, bool& v
452444

453445
if (it != itEnd && std::strcmp(it->name, cstr) == 0) {
454446
valid = true;
455-
color = it->color;
447+
return it->color;
456448
}
449+
return {};
457450
}
458451

459452

@@ -472,33 +465,30 @@ Color CSSColorParser::parse(const std::string& css_str, bool& valid) {
472465

473466
size_t pos = 0;
474467
size_t end = css_str.length();
475-
bool matched = false;
476-
Color color;
477468

478469
skip_whitespace(css_str, pos, end);
479470
if (pos == end) {
480471
return {};
481472
}
482473

483-
switch(*(css_str.c_str() + pos)) {
474+
switch(css_str[pos]) {
484475
case '#':
485-
parseHexRGB(css_str, pos+1, end, valid, color);
486-
matched = true;
487-
break;
476+
return parseHexRGB(css_str, pos+1, end, valid);
488477

489478
case 'r':
490479
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+
}
492483
break;
493484

494485
case 'h':
495486
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+
}
497490
break;
498491
}
499492

500-
if (!matched)
501-
parseNamedColor(css_str, pos, end, valid, color);
502-
503-
return color;
493+
return parseNamedColor(css_str, pos, end, valid);
504494
}

0 commit comments

Comments
 (0)