From 1d96d5eea44ea4813d947cca9bd84cf10733d927 Mon Sep 17 00:00:00 2001 From: asynts Date: Wed, 7 Oct 2020 14:02:42 +0200 Subject: [PATCH] AK: Use new format functions. --- AK/Format.cpp | 2 +- AK/IPv4Address.h | 2 +- AK/JsonObject.h | 10 ++-- AK/MACAddress.h | 2 +- AK/MappedFile.cpp | 2 +- AK/NumberFormat.h | 4 +- AK/PrintfImplementation.h | 2 +- AK/SharedBuffer.cpp | 2 +- AK/StringImpl.cpp | 2 +- AK/TestSuite.h | 123 +++++++++++++++++--------------------- AK/URL.cpp | 2 +- AK/URLParser.cpp | 2 +- AK/Utf8View.cpp | 10 ++-- 13 files changed, 74 insertions(+), 91 deletions(-) diff --git a/AK/Format.cpp b/AK/Format.cpp index 45188c752be..3f0f725a2e8 100644 --- a/AK/Format.cpp +++ b/AK/Format.cpp @@ -465,7 +465,7 @@ void StandardFormatter::parse(TypeErasedFormatParams& params, FormatParser& pars m_mode = Mode::Pointer; if (!parser.is_eof()) - dbg() << __PRETTY_FUNCTION__ << " did not consume '" << parser.remaining() << "'"; + dbgln("{} did not consume '{}'", __PRETTY_FUNCTION__, parser.remaining()); ASSERT(parser.is_eof()); } diff --git a/AK/IPv4Address.h b/AK/IPv4Address.h index 8beb3ba0e16..22da7c65784 100644 --- a/AK/IPv4Address.h +++ b/AK/IPv4Address.h @@ -68,7 +68,7 @@ public: String to_string() const { - return String::format("%u.%u.%u.%u", m_data[0], m_data[1], m_data[2], m_data[3]); + return String::formatted("{}.{}.{}.{}", m_data[0], m_data[1], m_data[2], m_data[3]); } static Optional from_string(const StringView& string) diff --git a/AK/JsonObject.h b/AK/JsonObject.h index 2fe396c026d..0cdff4fa548 100644 --- a/AK/JsonObject.h +++ b/AK/JsonObject.h @@ -182,7 +182,7 @@ inline void JsonValue::serialize(Builder& builder) const builder.append("\\\\"); break; default: - builder.appendf("%c", ch); + builder.append(ch); } } builder.append("\""); @@ -202,16 +202,16 @@ inline void JsonValue::serialize(Builder& builder) const break; #endif case Type::Int32: - builder.appendf("%d", as_i32()); + builder.appendff("{}", as_i32()); break; case Type::Int64: - builder.appendf("%lld", as_i64()); + builder.appendff("{}", as_i64()); break; case Type::UnsignedInt32: - builder.appendf("%u", as_u32()); + builder.appendff("{}", as_u32()); break; case Type::UnsignedInt64: - builder.appendf("%llu", as_u64()); + builder.appendff("{}", as_u64()); break; case Type::Null: builder.append("null"); diff --git a/AK/MACAddress.h b/AK/MACAddress.h index e511da23f19..dd0977ed8ec 100644 --- a/AK/MACAddress.h +++ b/AK/MACAddress.h @@ -62,7 +62,7 @@ public: String to_string() const { - return String::format("%02x:%02x:%02x:%02x:%02x:%02x", m_data[0], m_data[1], m_data[2], m_data[3], m_data[4], m_data[5]); + return String::formatted("{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}", m_data[0], m_data[1], m_data[2], m_data[3], m_data[4], m_data[5]); } bool is_zero() const diff --git a/AK/MappedFile.cpp b/AK/MappedFile.cpp index 3224e68665c..9153475e7a2 100644 --- a/AK/MappedFile.cpp +++ b/AK/MappedFile.cpp @@ -57,7 +57,7 @@ MappedFile::MappedFile(const StringView& file_name) } #ifdef DEBUG_MAPPED_FILE - dbgprintf("MappedFile{%s} := { fd=%d, m_size=%zu, m_map=%p }\n", file_name.to_string().characters(), fd, m_size, m_map); + dbgln("MappedFile(\"{}\") := ( fd={}, m_size={}, m_map={} )", file_name, fd, m_size, m_map); #endif close(fd); diff --git a/AK/NumberFormat.h b/AK/NumberFormat.h index 0f172ed3bb0..0d30ec88f32 100644 --- a/AK/NumberFormat.h +++ b/AK/NumberFormat.h @@ -35,13 +35,13 @@ namespace AK { static String number_string_with_one_decimal(u64 number, u32 unit, const char* suffix) { int decimal = (number % unit) * 10 / unit; - return String::format("%llu.%d %s", number / unit, decimal, suffix); + return String::formatted("{}.{} {}", number / unit, decimal, suffix); } static inline String human_readable_size(size_t size) { if (size < 1 * KiB) - return String::format("%zu B", size); + return String::formatted("{} B", size); if (size < 1 * MiB) return number_string_with_one_decimal(size, KiB, "KiB"); if (size < 1 * GiB) diff --git a/AK/PrintfImplementation.h b/AK/PrintfImplementation.h index fc4179feef5..d3d1545e62a 100644 --- a/AK/PrintfImplementation.h +++ b/AK/PrintfImplementation.h @@ -401,7 +401,7 @@ struct PrintfImpl { } ALWAYS_INLINE int format_unrecognized(char format_op, const char* fmt, const ModifierState&, ArgumentListRefT) const { - dbg() << "printf_internal: Unimplemented format specifier " << format_op << " (fmt: " << fmt << ")"; + dbgln("printf_internal: Unimplemented format specifier {} (fmt: {})", format_op, fmt); return 0; } diff --git a/AK/SharedBuffer.cpp b/AK/SharedBuffer.cpp index 61782fcb28e..04229f595e3 100644 --- a/AK/SharedBuffer.cpp +++ b/AK/SharedBuffer.cpp @@ -40,7 +40,7 @@ static String shbuf_shm_name(int shbuf_id) { - return String::format("/serenity-shm:%d", shbuf_id); + return String::formatted("/serenity-shm:{}", shbuf_id); } # endif diff --git a/AK/StringImpl.cpp b/AK/StringImpl.cpp index c07b9f3c9e7..eae881764b7 100644 --- a/AK/StringImpl.cpp +++ b/AK/StringImpl.cpp @@ -42,7 +42,7 @@ void dump_all_stringimpls() { unsigned i = 0; for (auto& it : *g_all_live_stringimpls) { - dbgprintf("%u: \"%s\"\n", i, (*it).characters()); + dbgln("{}: \"{}\"", i, *it); ++i; } } diff --git a/AK/TestSuite.h b/AK/TestSuite.h index 1b2baa58525..f13b0a5fbff 100644 --- a/AK/TestSuite.h +++ b/AK/TestSuite.h @@ -28,34 +28,43 @@ #define AK_TEST_SUITE -#define ASSERT(x) \ - do { \ - if (!(x)) \ - fprintf(stderr, "\033[31;1mFAIL\033[0m: %s:%d: ASSERT(%s) failed\n", __FILE__, __LINE__, #x); \ +extern "C" __attribute__((noreturn)) void abort() noexcept; + +namespace AK { + +template +void warnln(const char* fmtstr, const Parameters&...); + +} + +using AK::warnln; + +#define ASSERT(x) \ + do { \ + if (!(x)) \ + ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: ASSERT({}) failed", __FILE__, __LINE__, #x); \ } while (false) -#define RELEASE_ASSERT(x) \ - do { \ - if (!(x)) \ - fprintf(stderr, "\033[31;1mFAIL\033[0m: %s:%d: RELEASE_ASSERT(%s) failed\n", __FILE__, __LINE__, #x); \ +#define RELEASE_ASSERT(x) \ + do { \ + if (!(x)) \ + ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: RELEASE_ASSERT({}) failed", __FILE__, __LINE__, #x); \ } while (false) -#define ASSERT_NOT_REACHED() \ - do { \ - fprintf(stderr, "\033[31;1mFAIL\033[0m: %s:%d: ASSERT_NOT_REACHED() called\n", __FILE__, __LINE__); \ - abort(); \ +#define ASSERT_NOT_REACHED() \ + do { \ + ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: ASSERT_NOT_REACHED() called", __FILE__, __LINE__); \ + ::abort(); \ } while (false) -#define TODO() \ - do { \ - fprintf(stderr, "\033[31;1mFAIL\033[0m: %s:%d: TODO() called\n", __FILE__, __LINE__); \ - abort(); \ +#define TODO() \ + do { \ + ::AK::warnln(stderr, "\033[31;1mFAIL\033[0m: {}:{}: TODO() called", __FILE__, __LINE__); \ + ::abort(); \ } while (false) -#include - +#include #include -#include #include #include @@ -159,12 +168,12 @@ void TestSuite::main(const String& suite_name, int argc, char** argv) const auto& matching_tests = find_cases(search_string, !do_benchmarks_only, !do_tests_only); if (do_list_cases) { - out() << "Available cases for " << suite_name << ":"; + outln("Available cases for {}:", suite_name); for (const auto& test : matching_tests) { - out() << " " << test.name(); + outln(" {}", test.name()); } } else { - out() << "Running " << matching_tests.size() << " cases out of " << m_cases.size() << "."; + outln("Running {} cases out of {}.", matching_tests.size(), m_cases.size()); run(matching_tests); } @@ -216,37 +225,13 @@ void TestSuite::run(const NonnullRefPtrVector& tests) } } - dbg() << "Finished " << test_count << " tests and " << benchmark_count << " benchmarks in " << global_timer.elapsed_milliseconds() << " ms (" - << m_testtime << " tests, " << m_benchtime << " benchmarks, " << (global_timer.elapsed_milliseconds() - (m_testtime + m_benchtime)) << " other)"; -} - -// Use SFINAE to print if we can. -// This trick is good enough for TestSuite.h, but not flexible enough to be put into LogStream.h. -template -struct MaybeStream { - static const Stream& call(const Stream& stream, const LHS&, const RHS&) - { - return stream; - } -}; -template -struct MaybeStream(0) << "" << *reinterpret_cast(0) << "" << *reinterpret_cast(0) << "")>> { - static const Stream& call(const Stream& stream, const LHS& lhs, const RHS& rhs) - { - return stream << ": LHS=\"" << lhs << "\", RHS=\"" << rhs << "\""; - } -}; -template -static const Stream& maybe_print_rhs_lhs(const Stream& stream, const LHS& lhs, const RHS& rhs) -{ - return MaybeStream::call(stream, lhs, rhs); -} -template -static const Stream& force_print_rhs_lhs(const Stream& stream, const LHS& lhs, const RHS& rhs) -{ - using _ = decltype(*reinterpret_cast(0) << "" << *reinterpret_cast(0) << "" << *reinterpret_cast(0) << ""); - (void)sizeof(_); - return MaybeStream::call(stream, lhs, rhs); + dbgln("Finished {} tests and {} benchmarks in {}ms ({}ms tests, {}ms benchmarks, {}ms other).", + test_count, + benchmark_count, + global_timer.elapsed_milliseconds(), + m_testtime, + m_benchtime, + global_timer.elapsed_milliseconds() - (m_testtime + m_benchtime)); } } @@ -292,26 +277,26 @@ using AK::TestSuite; TestSuite::release(); \ } -#define EXPECT_EQ(a, b) \ - do { \ - auto lhs = (a); \ - auto rhs = (b); \ - if (lhs != rhs) \ - AK::maybe_print_rhs_lhs(warn() << "\033[31;1mFAIL\033[0m: " __FILE__ ":" << __LINE__ << ": EXPECT_EQ(" #a ", " #b ") failed", lhs, rhs); \ +#define EXPECT_EQ(a, b) \ + do { \ + auto lhs = (a); \ + auto rhs = (b); \ + if (lhs != rhs) \ + warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT_EQ({}, {}) failed with lhs={} and rhs={}", __FILE__, __LINE__, #a, #b, FormatIfSupported { lhs }, FormatIfSupported { rhs }); \ } while (false) // If you're stuck and `EXPECT_EQ` seems to refuse to print anything useful, // try this: It'll spit out a nice compiler error telling you why it doesn't print. -#define EXPECT_EQ_FORCE(a, b) \ - do { \ - auto lhs = (a); \ - auto rhs = (b); \ - if (lhs != rhs) \ - AK::force_print_rhs_lhs(warn() << "\033[31;1mFAIL\033[0m: " __FILE__ ":" << __LINE__ << ": EXPECT_EQ(" #a ", " #b ") failed", lhs, rhs); \ +#define EXPECT_EQ_FORCE(a, b) \ + do { \ + auto lhs = (a); \ + auto rhs = (b); \ + if (lhs != rhs) \ + warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT_EQ({}, {}) failed with lhs={} and rhs={}", __FILE__, __LINE__, #a, #b, lhs, rhs); \ } while (false) -#define EXPECT(x) \ - do { \ - if (!(x)) \ - warn() << "\033[31;1mFAIL\033[0m: " __FILE__ ":" << __LINE__ << ": EXPECT(" #x ") failed"; \ +#define EXPECT(x) \ + do { \ + if (!(x)) \ + warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT({}) failed", __FILE__, __LINE__, #x); \ } while (false) diff --git a/AK/URL.cpp b/AK/URL.cpp index 042bd16c17c..1c42904264b 100644 --- a/AK/URL.cpp +++ b/AK/URL.cpp @@ -306,7 +306,7 @@ URL URL::complete_url(const String& string) const return {}; if (string.starts_with("//")) { - URL url(String::format("%s:%s", m_protocol.characters(), string.characters())); + URL url(String::formatted("{}:{}", m_protocol, string)); if (url.is_valid()) return url; } diff --git a/AK/URLParser.cpp b/AK/URLParser.cpp index 13292721069..1a003344668 100644 --- a/AK/URLParser.cpp +++ b/AK/URLParser.cpp @@ -96,7 +96,7 @@ String urlencode(const StringView& input) for (char ch : input) { if (in_userinfo_set((u8)ch)) { builder.append('%'); - builder.appendf("%02X", (u8)ch); + builder.appendff("{:02X}", ch); } else { builder.append(ch); } diff --git a/AK/Utf8View.cpp b/AK/Utf8View.cpp index f94d11ac697..0cec7a2f5df 100644 --- a/AK/Utf8View.cpp +++ b/AK/Utf8View.cpp @@ -197,13 +197,11 @@ u32 Utf8CodepointIterator::operator*() const int code_point_length_in_bytes = 0; bool first_byte_makes_sense = decode_first_byte(m_ptr[0], code_point_length_in_bytes, code_point_value_so_far); - if (!first_byte_makes_sense) { - dbg() << "First byte doesn't make sense, bytes: " << StringView((const char*)m_ptr, m_length); - } + if (!first_byte_makes_sense) + dbgln("First byte doesn't make sense, bytes: {}", StringView { (const char*)m_ptr, (size_t)m_length }); ASSERT(first_byte_makes_sense); - if (code_point_length_in_bytes > m_length) { - dbg() << "Not enough bytes (need " << code_point_length_in_bytes << ", have " << m_length << "), first byte is: " << m_ptr[0] << " " << (const char*)m_ptr; - } + if (code_point_length_in_bytes > m_length) + dbgln("Not enough bytes (need {}, have {}), first byte is: {:#02x}, '{}'", code_point_length_in_bytes, m_length, m_ptr[0], (const char*)m_ptr); ASSERT(code_point_length_in_bytes <= m_length); for (int offset = 1; offset < code_point_length_in_bytes; offset++) {