Skip to content

Commit b6f8608

Browse files
committed
avoid two string copies in the hex rgba case
1 parent 37fabd8 commit b6f8608

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

csscolorparser.cpp

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -203,27 +203,27 @@ float clamp_css_float(T f) { // Clamp to float 0.0 .. 1.0.
203203
return f < 0 ? 0 : f > 1 ? 1 : f;
204204
}
205205

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);
208208
}
209209

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);
212212
}
213213

214214
uint8_t parse_css_int(const std::string& str) { // int or percentage.
215215
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);
217217
} else {
218-
return clamp_css_byte(parseInt(str));
218+
return clamp_css_byte(parseInt(str.c_str()));
219219
}
220220
}
221221

222222
float parse_css_float(const std::string& str) { // float or percentage.
223223
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);
225225
} else {
226-
return clamp_css_float(parseFloat(str));
226+
return clamp_css_float(parseFloat(str.c_str()));
227227
}
228228
}
229229

@@ -259,18 +259,11 @@ std::vector<std::string> split(const std::string& s, char delim) {
259259
}
260260

261261
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);
269262

270263
// #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.
274267
if (!(iv >= 0 && iv <= 0xfff)) {
275268
return {};
276269
} else {
@@ -281,8 +274,8 @@ Color CSSColorParser::parse(const std::string& css_str) {
281274
1
282275
};
283276
}
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.
286279
if (!(iv >= 0 && iv <= 0xffffff)) {
287280
return {}; // Covers NaN.
288281
} else {
@@ -298,6 +291,15 @@ Color CSSColorParser::parse(const std::string& css_str) {
298291
return {};
299292
}
300293

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+
301303
size_t op = str.find_first_of('('), ep = str.find_first_of(')');
302304
if (op != std::string::npos && ep + 1 == str.length()) {
303305
const std::string fname = str.substr(0, op);
@@ -336,7 +338,7 @@ Color CSSColorParser::parse(const std::string& css_str) {
336338
}
337339
}
338340

339-
float h = parseFloat(params[0]) / 360.0f;
341+
float h = parseFloat(params[0].c_str()) / 360.0f;
340342
while (h < 0.0f) h++;
341343
while (h > 1.0f) h--;
342344

0 commit comments

Comments
 (0)