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:
Andreas Kling 2019-07-03 14:56:27 +02:00
parent 8ac2b30de7
commit b79112e6d6
Notes: sideshowbarker 2024-07-19 13:24:05 +09:00
11 changed files with 26 additions and 14 deletions

View file

@ -181,6 +181,8 @@ public:
}
static String format(const char*, ...);
static String number(unsigned);
static String number(int);
StringView view() const { return { characters(), length() }; }

View file

@ -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;

View file

@ -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);
}

View file

@ -207,9 +207,9 @@ void ProcessModel::update()
{
auto it = m_usernames.find((uid_t)uid);
if (it != m_usernames.end())
state.user = String::format("%s", (*it).value.characters());
state.user = (*it).value;
else
state.user = String::format("%u", uid);
state.user = String::number(uid);
}
state.priority = process_object.get("priority").to_string();
state.syscalls = process_object.get("syscall_count").to_dword();

View file

@ -185,7 +185,7 @@ void Field::reset()
m_time_elapsed = 0;
m_time_label.set_text("0");
m_flags_left = m_mine_count;
m_flag_label.set_text(String::format("%u", m_flags_left));
m_flag_label.set_text(String::number(m_flags_left));
m_timer.stop();
set_greedy_for_hits(false);
set_face(Face::Default);
@ -384,7 +384,7 @@ void Field::set_flag(Square& square, bool flag)
}
square.has_flag = flag;
m_flag_label.set_text(String::format("%u", m_flags_left));
m_flag_label.set_text(String::number(m_flags_left));
square.button->set_icon(square.has_flag ? m_flag_bitmap : nullptr);
square.button->update();
}
@ -396,7 +396,7 @@ void Field::on_square_middle_clicked(Square& square)
if (square.has_flag) {
++m_flags_left;
square.has_flag = false;
m_flag_label.set_text(String::format("%u", m_flags_left));
m_flag_label.set_text(String::number(m_flags_left));
}
square.is_considering = !square.is_considering;
square.button->set_icon(square.is_considering ? m_consider_bitmap : nullptr);

View file

@ -148,7 +148,7 @@ void CConfigFile::write_entry(const String& group, const String& key, const Stri
void CConfigFile::write_num_entry(const String& group, const String& key, int value)
{
write_entry(group, key, String::format("%d", value));
write_entry(group, key, String::number(value));
}
void CConfigFile::write_bool_entry(const String& group, const String& key, bool value)
{

View file

@ -54,5 +54,5 @@ String CProcessStatisticsReader::get_username_from_uid(const uid_t uid)
if (it != m_usernames.end())
return (*it).value;
else
return String::format("%u", uid);
return String::number(uid);
}

View file

@ -202,7 +202,7 @@ String GDirectoryModel::name_for_uid(uid_t uid) const
{
auto it = m_user_names.find(uid);
if (it == m_user_names.end())
return String::format("%u", uid);
return String::number(uid);
return (*it).value;
}
@ -210,7 +210,7 @@ String GDirectoryModel::name_for_gid(uid_t gid) const
{
auto it = m_user_names.find(gid);
if (it == m_user_names.end())
return String::format("%u", gid);
return String::number(gid);
return (*it).value;
}

View file

@ -13,7 +13,7 @@ GSpinBox::GSpinBox(GWidget* parent)
if (ok)
set_value(value);
else
m_editor->set_text(String::format("%d", m_value));
m_editor->set_text(String::number(m_value));
};
m_increment_button = new GButton(this);
m_increment_button->set_focusable(false);
@ -38,7 +38,7 @@ void GSpinBox::set_value(int value)
if (m_value == value)
return;
m_value = value;
m_editor->set_text(String::format("%d", value));
m_editor->set_text(String::number(value));
update();
if (on_change)
on_change(value);

View file

@ -292,7 +292,7 @@ void GTextEditor::paint_event(GPaintEvent& event)
auto ruler_line_rect = ruler_content_rect(i);
painter.draw_text(
ruler_line_rect.shrunken(2, 0),
String::format("%u", i),
String::number(i),
is_current_line ? Font::default_bold_font() : font(),
TextAlignment::CenterRight,
is_current_line ? Color::DarkGray : Color::MidGray);

View file

@ -289,7 +289,7 @@ String GVariant::to_string() const
case Type::Bool:
return as_bool() ? "true" : "false";
case Type::Int:
return String::format("%d", as_int());
return String::number(as_int());
case Type::Float:
return String::format("%f", (double)as_float());
case Type::String: