From b79112e6d6c572ad32d247ceb022cc1dda413d25 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 3 Jul 2019 14:56:27 +0200 Subject: [PATCH] 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. --- AK/AKString.h | 2 ++ AK/String.cpp | 10 ++++++++++ AK/Tests/TestQueue.cpp | 2 +- Applications/ProcessManager/ProcessModel.cpp | 4 ++-- Games/Minesweeper/Field.cpp | 6 +++--- LibCore/CConfigFile.cpp | 2 +- LibCore/CProcessStatisticsReader.cpp | 2 +- LibGUI/GDirectoryModel.cpp | 4 ++-- LibGUI/GSpinBox.cpp | 4 ++-- LibGUI/GTextEditor.cpp | 2 +- LibGUI/GVariant.cpp | 2 +- 11 files changed, 26 insertions(+), 14 deletions(-) diff --git a/AK/AKString.h b/AK/AKString.h index a7de84b8564..70e0fb09172 100644 --- a/AK/AKString.h +++ b/AK/AKString.h @@ -181,6 +181,8 @@ public: } static String format(const char*, ...); + static String number(unsigned); + static String number(int); StringView view() const { return { characters(), length() }; } diff --git a/AK/String.cpp b/AK/String.cpp index fce8006e4d6..b7e8c0cd699 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -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; diff --git a/AK/Tests/TestQueue.cpp b/AK/Tests/TestQueue.cpp index 708835f154d..a604074215b 100644 --- a/AK/Tests/TestQueue.cpp +++ b/AK/Tests/TestQueue.cpp @@ -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); } diff --git a/Applications/ProcessManager/ProcessModel.cpp b/Applications/ProcessManager/ProcessModel.cpp index 7a75bbf8fb9..9c0986d6e35 100644 --- a/Applications/ProcessManager/ProcessModel.cpp +++ b/Applications/ProcessManager/ProcessModel.cpp @@ -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(); diff --git a/Games/Minesweeper/Field.cpp b/Games/Minesweeper/Field.cpp index 9baeb813154..c3fbd92667a 100644 --- a/Games/Minesweeper/Field.cpp +++ b/Games/Minesweeper/Field.cpp @@ -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); diff --git a/LibCore/CConfigFile.cpp b/LibCore/CConfigFile.cpp index e4b8453dd7d..2f1fec736a4 100644 --- a/LibCore/CConfigFile.cpp +++ b/LibCore/CConfigFile.cpp @@ -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) { diff --git a/LibCore/CProcessStatisticsReader.cpp b/LibCore/CProcessStatisticsReader.cpp index 599fa6ef35c..67c81386a36 100644 --- a/LibCore/CProcessStatisticsReader.cpp +++ b/LibCore/CProcessStatisticsReader.cpp @@ -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); } diff --git a/LibGUI/GDirectoryModel.cpp b/LibGUI/GDirectoryModel.cpp index b4cc3116327..2db018b97fb 100644 --- a/LibGUI/GDirectoryModel.cpp +++ b/LibGUI/GDirectoryModel.cpp @@ -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; } diff --git a/LibGUI/GSpinBox.cpp b/LibGUI/GSpinBox.cpp index cc9bdf8082f..d37ffafcf4e 100644 --- a/LibGUI/GSpinBox.cpp +++ b/LibGUI/GSpinBox.cpp @@ -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); diff --git a/LibGUI/GTextEditor.cpp b/LibGUI/GTextEditor.cpp index 07f6c3c9237..225e942c702 100644 --- a/LibGUI/GTextEditor.cpp +++ b/LibGUI/GTextEditor.cpp @@ -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); diff --git a/LibGUI/GVariant.cpp b/LibGUI/GVariant.cpp index 25f2d05667c..a2da6d3402b 100644 --- a/LibGUI/GVariant.cpp +++ b/LibGUI/GVariant.cpp @@ -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: