mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 02:37:35 +00:00
AK: Add String::number() for creating a String from a number.
Instead of manually doing String::format("%d"/"%u") everywhere, let's have a String API for this. It's just a wrapper around format() for now, but it could be made more efficient in the future.
This commit is contained in:
parent
8ac2b30de7
commit
b79112e6d6
11 changed files with 26 additions and 14 deletions
|
@ -181,6 +181,8 @@ public:
|
|||
}
|
||||
|
||||
static String format(const char*, ...);
|
||||
static String number(unsigned);
|
||||
static String number(int);
|
||||
|
||||
StringView view() const { return { characters(), length() }; }
|
||||
|
||||
|
|
|
@ -170,6 +170,16 @@ unsigned String::to_uint(bool& ok) const
|
|||
return value;
|
||||
}
|
||||
|
||||
String String::number(unsigned value)
|
||||
{
|
||||
return String::format("%u", value);
|
||||
}
|
||||
|
||||
String String::number(int value)
|
||||
{
|
||||
return String::format("%d", value);
|
||||
}
|
||||
|
||||
String String::format(const char* fmt, ...)
|
||||
{
|
||||
StringBuilder builder;
|
||||
|
|
|
@ -28,7 +28,7 @@ int main()
|
|||
EXPECT(strings.is_empty());
|
||||
|
||||
for (int i = 0; i < 10000; ++i) {
|
||||
strings.enqueue(String::format("%d", i));
|
||||
strings.enqueue(String::number(i));
|
||||
EXPECT_EQ(strings.size(), i + 1);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue