1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 08:34:57 +00:00

AK: Use new format functions.

This commit is contained in:
asynts 2020-10-07 14:02:42 +02:00 committed by Andreas Kling
parent 560c52989c
commit 1d96d5eea4
13 changed files with 74 additions and 91 deletions

View file

@ -465,7 +465,7 @@ void StandardFormatter::parse(TypeErasedFormatParams& params, FormatParser& pars
m_mode = Mode::Pointer; m_mode = Mode::Pointer;
if (!parser.is_eof()) if (!parser.is_eof())
dbg() << __PRETTY_FUNCTION__ << " did not consume '" << parser.remaining() << "'"; dbgln("{} did not consume '{}'", __PRETTY_FUNCTION__, parser.remaining());
ASSERT(parser.is_eof()); ASSERT(parser.is_eof());
} }

View file

@ -68,7 +68,7 @@ public:
String to_string() const 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<IPv4Address> from_string(const StringView& string) static Optional<IPv4Address> from_string(const StringView& string)

View file

@ -182,7 +182,7 @@ inline void JsonValue::serialize(Builder& builder) const
builder.append("\\\\"); builder.append("\\\\");
break; break;
default: default:
builder.appendf("%c", ch); builder.append(ch);
} }
} }
builder.append("\""); builder.append("\"");
@ -202,16 +202,16 @@ inline void JsonValue::serialize(Builder& builder) const
break; break;
#endif #endif
case Type::Int32: case Type::Int32:
builder.appendf("%d", as_i32()); builder.appendff("{}", as_i32());
break; break;
case Type::Int64: case Type::Int64:
builder.appendf("%lld", as_i64()); builder.appendff("{}", as_i64());
break; break;
case Type::UnsignedInt32: case Type::UnsignedInt32:
builder.appendf("%u", as_u32()); builder.appendff("{}", as_u32());
break; break;
case Type::UnsignedInt64: case Type::UnsignedInt64:
builder.appendf("%llu", as_u64()); builder.appendff("{}", as_u64());
break; break;
case Type::Null: case Type::Null:
builder.append("null"); builder.append("null");

View file

@ -62,7 +62,7 @@ public:
String to_string() const 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 bool is_zero() const

View file

@ -57,7 +57,7 @@ MappedFile::MappedFile(const StringView& file_name)
} }
#ifdef DEBUG_MAPPED_FILE #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 #endif
close(fd); close(fd);

View file

@ -35,13 +35,13 @@ namespace AK {
static String number_string_with_one_decimal(u64 number, u32 unit, const char* suffix) static String number_string_with_one_decimal(u64 number, u32 unit, const char* suffix)
{ {
int decimal = (number % unit) * 10 / unit; 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) static inline String human_readable_size(size_t size)
{ {
if (size < 1 * KiB) if (size < 1 * KiB)
return String::format("%zu B", size); return String::formatted("{} B", size);
if (size < 1 * MiB) if (size < 1 * MiB)
return number_string_with_one_decimal(size, KiB, "KiB"); return number_string_with_one_decimal(size, KiB, "KiB");
if (size < 1 * GiB) if (size < 1 * GiB)

View file

@ -401,7 +401,7 @@ struct PrintfImpl {
} }
ALWAYS_INLINE int format_unrecognized(char format_op, const char* fmt, const ModifierState&, ArgumentListRefT) const 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; return 0;
} }

View file

@ -40,7 +40,7 @@
static String shbuf_shm_name(int shbuf_id) static String shbuf_shm_name(int shbuf_id)
{ {
return String::format("/serenity-shm:%d", shbuf_id); return String::formatted("/serenity-shm:{}", shbuf_id);
} }
# endif # endif

View file

@ -42,7 +42,7 @@ void dump_all_stringimpls()
{ {
unsigned i = 0; unsigned i = 0;
for (auto& it : *g_all_live_stringimpls) { for (auto& it : *g_all_live_stringimpls) {
dbgprintf("%u: \"%s\"\n", i, (*it).characters()); dbgln("{}: \"{}\"", i, *it);
++i; ++i;
} }
} }

View file

@ -28,34 +28,43 @@
#define AK_TEST_SUITE #define AK_TEST_SUITE
#define ASSERT(x) \ extern "C" __attribute__((noreturn)) void abort() noexcept;
do { \
if (!(x)) \ namespace AK {
fprintf(stderr, "\033[31;1mFAIL\033[0m: %s:%d: ASSERT(%s) failed\n", __FILE__, __LINE__, #x); \
template<typename... Parameters>
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) } while (false)
#define RELEASE_ASSERT(x) \ #define RELEASE_ASSERT(x) \
do { \ do { \
if (!(x)) \ if (!(x)) \
fprintf(stderr, "\033[31;1mFAIL\033[0m: %s:%d: RELEASE_ASSERT(%s) failed\n", __FILE__, __LINE__, #x); \ ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: RELEASE_ASSERT({}) failed", __FILE__, __LINE__, #x); \
} while (false) } while (false)
#define ASSERT_NOT_REACHED() \ #define ASSERT_NOT_REACHED() \
do { \ do { \
fprintf(stderr, "\033[31;1mFAIL\033[0m: %s:%d: ASSERT_NOT_REACHED() called\n", __FILE__, __LINE__); \ ::AK::warnln("\033[31;1mFAIL\033[0m: {}:{}: ASSERT_NOT_REACHED() called", __FILE__, __LINE__); \
abort(); \ ::abort(); \
} while (false) } while (false)
#define TODO() \ #define TODO() \
do { \ do { \
fprintf(stderr, "\033[31;1mFAIL\033[0m: %s:%d: TODO() called\n", __FILE__, __LINE__); \ ::AK::warnln(stderr, "\033[31;1mFAIL\033[0m: {}:{}: TODO() called", __FILE__, __LINE__); \
abort(); \ ::abort(); \
} while (false) } while (false)
#include <stdio.h> #include <AK/Format.h>
#include <AK/Function.h> #include <AK/Function.h>
#include <AK/LogStream.h>
#include <AK/NonnullRefPtrVector.h> #include <AK/NonnullRefPtrVector.h>
#include <AK/String.h> #include <AK/String.h>
@ -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); const auto& matching_tests = find_cases(search_string, !do_benchmarks_only, !do_tests_only);
if (do_list_cases) { if (do_list_cases) {
out() << "Available cases for " << suite_name << ":"; outln("Available cases for {}:", suite_name);
for (const auto& test : matching_tests) { for (const auto& test : matching_tests) {
out() << " " << test.name(); outln(" {}", test.name());
} }
} else { } 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); run(matching_tests);
} }
@ -216,37 +225,13 @@ void TestSuite::run(const NonnullRefPtrVector<TestCase>& tests)
} }
} }
dbg() << "Finished " << test_count << " tests and " << benchmark_count << " benchmarks in " << global_timer.elapsed_milliseconds() << " ms (" dbgln("Finished {} tests and {} benchmarks in {}ms ({}ms tests, {}ms benchmarks, {}ms other).",
<< m_testtime << " tests, " << m_benchtime << " benchmarks, " << (global_timer.elapsed_milliseconds() - (m_testtime + m_benchtime)) << " other)"; test_count,
} benchmark_count,
global_timer.elapsed_milliseconds(),
// Use SFINAE to print if we can. m_testtime,
// This trick is good enough for TestSuite.h, but not flexible enough to be put into LogStream.h. m_benchtime,
template<typename Stream, typename LHS, typename RHS, typename = void> global_timer.elapsed_milliseconds() - (m_testtime + m_benchtime));
struct MaybeStream {
static const Stream& call(const Stream& stream, const LHS&, const RHS&)
{
return stream;
}
};
template<typename Stream, typename LHS, typename RHS>
struct MaybeStream<Stream, LHS, RHS, AK::Void<decltype(*reinterpret_cast<const Stream*>(0) << "" << *reinterpret_cast<const LHS*>(0) << "" << *reinterpret_cast<const RHS*>(0) << "")>> {
static const Stream& call(const Stream& stream, const LHS& lhs, const RHS& rhs)
{
return stream << ": LHS=\"" << lhs << "\", RHS=\"" << rhs << "\"";
}
};
template<typename Stream, typename LHS, typename RHS>
static const Stream& maybe_print_rhs_lhs(const Stream& stream, const LHS& lhs, const RHS& rhs)
{
return MaybeStream<Stream, LHS, RHS>::call(stream, lhs, rhs);
}
template<typename Stream, typename LHS, typename RHS>
static const Stream& force_print_rhs_lhs(const Stream& stream, const LHS& lhs, const RHS& rhs)
{
using _ = decltype(*reinterpret_cast<const Stream*>(0) << "" << *reinterpret_cast<const LHS*>(0) << "" << *reinterpret_cast<const RHS*>(0) << "");
(void)sizeof(_);
return MaybeStream<Stream, LHS, RHS>::call(stream, lhs, rhs);
} }
} }
@ -292,26 +277,26 @@ using AK::TestSuite;
TestSuite::release(); \ TestSuite::release(); \
} }
#define EXPECT_EQ(a, b) \ #define EXPECT_EQ(a, b) \
do { \ do { \
auto lhs = (a); \ auto lhs = (a); \
auto rhs = (b); \ auto rhs = (b); \
if (lhs != rhs) \ if (lhs != rhs) \
AK::maybe_print_rhs_lhs(warn() << "\033[31;1mFAIL\033[0m: " __FILE__ ":" << __LINE__ << ": EXPECT_EQ(" #a ", " #b ") failed", 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) } while (false)
// If you're stuck and `EXPECT_EQ` seems to refuse to print anything useful, // 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. // try this: It'll spit out a nice compiler error telling you why it doesn't print.
#define EXPECT_EQ_FORCE(a, b) \ #define EXPECT_EQ_FORCE(a, b) \
do { \ do { \
auto lhs = (a); \ auto lhs = (a); \
auto rhs = (b); \ auto rhs = (b); \
if (lhs != rhs) \ if (lhs != rhs) \
AK::force_print_rhs_lhs(warn() << "\033[31;1mFAIL\033[0m: " __FILE__ ":" << __LINE__ << ": EXPECT_EQ(" #a ", " #b ") failed", lhs, rhs); \ warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT_EQ({}, {}) failed with lhs={} and rhs={}", __FILE__, __LINE__, #a, #b, lhs, rhs); \
} while (false) } while (false)
#define EXPECT(x) \ #define EXPECT(x) \
do { \ do { \
if (!(x)) \ if (!(x)) \
warn() << "\033[31;1mFAIL\033[0m: " __FILE__ ":" << __LINE__ << ": EXPECT(" #x ") failed"; \ warnln("\033[31;1mFAIL\033[0m: {}:{}: EXPECT({}) failed", __FILE__, __LINE__, #x); \
} while (false) } while (false)

View file

@ -306,7 +306,7 @@ URL URL::complete_url(const String& string) const
return {}; return {};
if (string.starts_with("//")) { 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()) if (url.is_valid())
return url; return url;
} }

View file

@ -96,7 +96,7 @@ String urlencode(const StringView& input)
for (char ch : input) { for (char ch : input) {
if (in_userinfo_set((u8)ch)) { if (in_userinfo_set((u8)ch)) {
builder.append('%'); builder.append('%');
builder.appendf("%02X", (u8)ch); builder.appendff("{:02X}", ch);
} else { } else {
builder.append(ch); builder.append(ch);
} }

View file

@ -197,13 +197,11 @@ u32 Utf8CodepointIterator::operator*() const
int code_point_length_in_bytes = 0; 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); 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) { if (!first_byte_makes_sense)
dbg() << "First byte doesn't make sense, bytes: " << StringView((const char*)m_ptr, m_length); dbgln("First byte doesn't make sense, bytes: {}", StringView { (const char*)m_ptr, (size_t)m_length });
}
ASSERT(first_byte_makes_sense); ASSERT(first_byte_makes_sense);
if (code_point_length_in_bytes > m_length) { 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; 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); ASSERT(code_point_length_in_bytes <= m_length);
for (int offset = 1; offset < code_point_length_in_bytes; offset++) { for (int offset = 1; offset < code_point_length_in_bytes; offset++) {