From da9c995a8c634daa8f291740e840e876cc506339 Mon Sep 17 00:00:00 2001 From: asynts Date: Tue, 6 Oct 2020 14:16:35 +0200 Subject: [PATCH] IRCClient: Use new format functions. --- AK/Tests/TestFormat.cpp | 6 ++++ Applications/IRCClient/IRCAppWindow.cpp | 2 +- Applications/IRCClient/IRCChannel.cpp | 12 +++---- Applications/IRCClient/IRCClient.cpp | 2 +- Applications/IRCClient/IRCLogBuffer.cpp | 32 +++++++++---------- Applications/IRCClient/IRCWindowListModel.cpp | 2 +- Applications/IRCClient/main.cpp | 8 ++--- 7 files changed, 35 insertions(+), 29 deletions(-) diff --git a/AK/Tests/TestFormat.cpp b/AK/Tests/TestFormat.cpp index 7df8430b5e..8e29659bef 100644 --- a/AK/Tests/TestFormat.cpp +++ b/AK/Tests/TestFormat.cpp @@ -188,4 +188,10 @@ TEST_CASE(format_string_literal_as_pointer) EXPECT_EQ(String::formatted("{:p}", literal), String::formatted("{:p}", reinterpret_cast(literal))); } +TEST_CASE(format_character) +{ + char a = 'a'; + EXPECT_EQ(String::formatted("{}", true ? a : 'b'), "a"); +} + TEST_MAIN(Format) diff --git a/Applications/IRCClient/IRCAppWindow.cpp b/Applications/IRCClient/IRCAppWindow.cpp index 4df478a980..43c43fdf0b 100644 --- a/Applications/IRCClient/IRCAppWindow.cpp +++ b/Applications/IRCClient/IRCAppWindow.cpp @@ -71,7 +71,7 @@ IRCAppWindow::~IRCAppWindow() void IRCAppWindow::update_title() { - set_title(String::format("%s@%s:%d - IRC Client", m_client->nickname().characters(), m_client->hostname().characters(), m_client->port())); + set_title(String::formatted("{}@{}:{} - IRC Client", m_client->nickname(), m_client->hostname(), m_client->port())); } void IRCAppWindow::setup_client() diff --git a/Applications/IRCClient/IRCChannel.cpp b/Applications/IRCClient/IRCChannel.cpp index fd38935720..81d9173e31 100644 --- a/Applications/IRCClient/IRCChannel.cpp +++ b/Applications/IRCClient/IRCChannel.cpp @@ -92,7 +92,7 @@ void IRCChannel::handle_join(const String& nick, const String& hostmask) add_member(nick, (char)0); m_member_model->update(); if (m_client.show_join_part_messages()) - add_message(String::format("*** %s [%s] has joined %s", nick.characters(), hostmask.characters(), m_name.characters()), Color::MidGreen); + add_message(String::formatted("*** {} [{}] has joined {}", nick, hostmask, m_name), Color::MidGreen); } void IRCChannel::handle_part(const String& nick, const String& hostmask) @@ -106,7 +106,7 @@ void IRCChannel::handle_part(const String& nick, const String& hostmask) } m_member_model->update(); if (m_client.show_join_part_messages()) - add_message(String::format("*** %s [%s] has parted from %s", nick.characters(), hostmask.characters(), m_name.characters()), Color::MidGreen); + add_message(String::formatted("*** {} [{}] has parted from {}", nick, hostmask, m_name), Color::MidGreen); } void IRCChannel::handle_quit(const String& nick, const String& hostmask, const String& message) @@ -119,15 +119,15 @@ void IRCChannel::handle_quit(const String& nick, const String& hostmask, const S remove_member(nick); } m_member_model->update(); - add_message(String::format("*** %s [%s] has quit (%s)", nick.characters(), hostmask.characters(), message.characters()), Color::MidGreen); + add_message(String::formatted("*** {} [{}] has quit ({})", nick, hostmask, message), Color::MidGreen); } void IRCChannel::handle_topic(const String& nick, const String& topic) { if (nick.is_null()) - add_message(String::format("*** Topic is \"%s\"", topic.characters()), Color::MidBlue); + add_message(String::formatted("*** Topic is \"{}\"", topic), Color::MidBlue); else - add_message(String::format("*** %s set topic to \"%s\"", nick.characters(), topic.characters()), Color::MidBlue); + add_message(String::formatted("*** {} set topic to \"{}\"", nick, topic), Color::MidBlue); } void IRCChannel::notify_nick_changed(const String& old_nick, const String& new_nick) @@ -137,7 +137,7 @@ void IRCChannel::notify_nick_changed(const String& old_nick, const String& new_n member.name = new_nick; m_member_model->update(); if (m_client.show_nick_change_messages()) - add_message(String::format("~ %s changed nickname to %s", old_nick.characters(), new_nick.characters()), Color::MidMagenta); + add_message(String::formatted("~ {} changed nickname to {}", old_nick, new_nick), Color::MidMagenta); return; } } diff --git a/Applications/IRCClient/IRCClient.cpp b/Applications/IRCClient/IRCClient.cpp index 56f0be4c71..b31aff409f 100644 --- a/Applications/IRCClient/IRCClient.cpp +++ b/Applications/IRCClient/IRCClient.cpp @@ -134,7 +134,7 @@ void IRCClient::receive_from_server() auto line = m_socket->read_line(PAGE_SIZE); if (line.is_null()) { if (!m_socket->is_connected()) { - out() << "IRCClient: Connection closed!"; + outln("IRCClient: Connection closed!"); exit(1); } ASSERT_NOT_REACHED(); diff --git a/Applications/IRCClient/IRCLogBuffer.cpp b/Applications/IRCClient/IRCLogBuffer.cpp index 458da22f32..826678a138 100644 --- a/Applications/IRCClient/IRCLogBuffer.cpp +++ b/Applications/IRCClient/IRCLogBuffer.cpp @@ -62,22 +62,22 @@ static String timestamp_string() { auto now = time(nullptr); auto* tm = localtime(&now); - return String::format("%02u:%02u:%02u ", tm->tm_hour, tm->tm_min, tm->tm_sec); + return String::formatted("{:02}:{:02}:{:02} ", tm->tm_hour, tm->tm_min, tm->tm_sec); } void IRCLogBuffer::add_message(char prefix, const String& name, const String& text, Color color) { - auto nick_string = String::format("<%c%s> ", prefix ? prefix : ' ', name.characters()); - auto html = String::format( - "%s" - "%s" - "%s", - timestamp_string().characters(), - escape_html_entities(nick_string).characters(), - escape_html_entities(text).characters()); + auto nick_string = String::formatted("<{}{}> ", prefix ? prefix : ' ', name.characters()); + auto html = String::formatted( + "{}" + "{}" + "{}", + timestamp_string(), + escape_html_entities(nick_string), + escape_html_entities(text)); auto wrapper = Web::DOM::create_element(*m_document, Web::HTML::TagNames::div); - wrapper->set_attribute(Web::HTML::AttributeNames::style, String::format("color: %s", color.to_string().characters())); + wrapper->set_attribute(Web::HTML::AttributeNames::style, String::formatted("color: {}", color.to_string())); wrapper->set_inner_html(html); m_container_element->append_child(wrapper); m_document->force_layout(); @@ -85,13 +85,13 @@ void IRCLogBuffer::add_message(char prefix, const String& name, const String& te void IRCLogBuffer::add_message(const String& text, Color color) { - auto html = String::format( - "%s" - "%s", - timestamp_string().characters(), - escape_html_entities(text).characters()); + auto html = String::formatted( + "{}" + "{}", + timestamp_string(), + escape_html_entities(text)); auto wrapper = Web::DOM::create_element(*m_document, Web::HTML::TagNames::div); - wrapper->set_attribute(Web::HTML::AttributeNames::style, String::format("color: %s", color.to_string().characters())); + wrapper->set_attribute(Web::HTML::AttributeNames::style, String::formatted("color: {}", color.to_string())); wrapper->set_inner_html(html); m_container_element->append_child(wrapper); m_document->force_layout(); diff --git a/Applications/IRCClient/IRCWindowListModel.cpp b/Applications/IRCClient/IRCWindowListModel.cpp index 096b6f27bc..7ee68c429e 100644 --- a/Applications/IRCClient/IRCWindowListModel.cpp +++ b/Applications/IRCClient/IRCWindowListModel.cpp @@ -65,7 +65,7 @@ GUI::Variant IRCWindowListModel::data(const GUI::ModelIndex& index, GUI::ModelRo case Column::Name: { auto& window = m_client->window_at(index.row()); if (window.unread_count()) - return String::format("%s (%d)", window.name().characters(), window.unread_count()); + return String::formatted("{} ({})", window.name(), window.unread_count()); return window.name(); } } diff --git a/Applications/IRCClient/main.cpp b/Applications/IRCClient/main.cpp index 8e50f35607..3e65c13cd0 100644 --- a/Applications/IRCClient/main.cpp +++ b/Applications/IRCClient/main.cpp @@ -38,7 +38,7 @@ int main(int argc, char** argv) } if (getuid() == 0) { - warn() << "Refusing to run as root"; + warnln("Refusing to run as root"); return 1; } @@ -54,17 +54,17 @@ int main(int argc, char** argv) url = URL::create_with_url_or_path(app->args()[0]); if (url.protocol().to_lowercase() == "ircs") { - warn() << "Secure IRC over SSL/TLS (ircs) is not supported"; + warnln("Secure IRC over SSL/TLS (ircs) is not supported"); return 1; } if (url.protocol().to_lowercase() != "irc") { - warn() << "Unsupported protocol"; + warnln("Unsupported protocol"); return 1; } if (url.host().is_empty()) { - warn() << "Invalid URL"; + warnln("Invalid URL"); return 1; }