1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 15:38:10 +00:00

AK: Make string-to-number conversion helpers return Optional

Get rid of the weird old signature:

- int StringType::to_int(bool& ok) const

And replace it with sensible new signature:

- Optional<int> StringType::to_int() const
This commit is contained in:
Andreas Kling 2020-06-12 21:07:52 +02:00
parent 15f4043a7a
commit fdfda6dec2
55 changed files with 354 additions and 455 deletions

View file

@ -139,22 +139,11 @@ static Optional<Color> parse_rgb_color(const StringView& string)
if (parts.size() != 3)
return {};
bool ok;
auto r = parts[0].to_int(ok);
if (!ok)
return {};
auto g = parts[1].to_int(ok);
if (!ok)
return {};
auto b = parts[2].to_int(ok);
if (!ok)
return {};
auto r = parts[0].to_uint().value_or(256);
auto g = parts[1].to_uint().value_or(256);
auto b = parts[2].to_uint().value_or(256);
if (r < 0 || r > 255)
return {};
if (g < 0 || g > 255)
return {};
if (b < 0 || b > 255)
if (r > 255 || g > 255 || b > 255)
return {};
return Color(r, g, b);
@ -171,27 +160,14 @@ static Optional<Color> parse_rgba_color(const StringView& string)
if (parts.size() != 4)
return {};
bool ok;
auto r = parts[0].to_int(ok);
if (!ok)
return {};
auto g = parts[1].to_int(ok);
if (!ok)
return {};
auto b = parts[2].to_int(ok);
if (!ok)
return {};
auto r = parts[0].to_int().value_or(256);
auto g = parts[1].to_int().value_or(256);
auto b = parts[2].to_int().value_or(256);
double alpha = strtod(parts[3].to_string().characters(), nullptr);
int a = alpha * 255;
unsigned a = alpha * 255;
if (r < 0 || r > 255)
return {};
if (g < 0 || g > 255)
return {};
if (b < 0 || b > 255)
return {};
if (a < 0 || a > 255)
if (r > 255 || g > 255 || b > 255 || a > 255)
return {};
return Color(r, g, b, a);