diff --git a/BAN/include/BAN/String.h b/BAN/include/BAN/String.h index 6c51be488e..b06b1aa2f8 100644 --- a/BAN/include/BAN/String.h +++ b/BAN/include/BAN/String.h @@ -26,10 +26,15 @@ namespace BAN ~String() { clear(); } template - static String formatted(const char* format, const Args&... args) + static BAN::ErrorOr formatted(const char* format, Args&&... args) { + size_type length = 0; + BAN::Formatter::print([&](char) { length++; }, format, BAN::forward(args)...); + String result; - BAN::Formatter::print([&](char c){ MUST(result.push_back(c)); }, format, args...); + TRY(result.reserve(length)); + BAN::Formatter::print([&](char c){ MUST(result.push_back(c)); }, format, BAN::forward(args)...); + return result; } diff --git a/kernel/kernel/Device/FramebufferDevice.cpp b/kernel/kernel/Device/FramebufferDevice.cpp index f6ffe36693..831d73652d 100644 --- a/kernel/kernel/Device/FramebufferDevice.cpp +++ b/kernel/kernel/Device/FramebufferDevice.cpp @@ -41,7 +41,7 @@ namespace Kernel FramebufferDevice::FramebufferDevice(mode_t mode, uid_t uid, gid_t gid, dev_t rdev, paddr_t paddr, uint32_t width, uint32_t height, uint32_t pitch, uint8_t bpp) : CharacterDevice(mode, uid, gid) - , m_name(BAN::String::formatted("fb{}", minor(rdev))) + , m_name(MUST(BAN::String::formatted("fb{}", minor(rdev)))) , m_rdev(rdev) , m_video_memory_paddr(paddr) , m_width(width) diff --git a/kernel/kernel/FS/ProcFS/FileSystem.cpp b/kernel/kernel/FS/ProcFS/FileSystem.cpp index 1fa82a9eac..d6b0c96792 100644 --- a/kernel/kernel/FS/ProcFS/FileSystem.cpp +++ b/kernel/kernel/FS/ProcFS/FileSystem.cpp @@ -29,7 +29,7 @@ namespace Kernel BAN::ErrorOr ProcFileSystem::on_process_create(Process& process) { - auto path = BAN::String::formatted("{}", process.pid()); + auto path = TRY(BAN::String::formatted("{}", process.pid())); auto inode = TRY(ProcPidInode::create_new(process, *this, 0555, process.credentials().ruid(), process.credentials().rgid())); TRY(static_cast(root_inode().ptr())->link_inode(*inode, path)); return {}; @@ -37,7 +37,7 @@ namespace Kernel void ProcFileSystem::on_process_delete(Process& process) { - auto path = BAN::String::formatted("{}", process.pid()); + auto path = MUST(BAN::String::formatted("{}", process.pid())); auto inode = MUST(root_inode()->find_inode(path)); static_cast(inode.ptr())->cleanup(); diff --git a/kernel/kernel/Input/PS2/Device.cpp b/kernel/kernel/Input/PS2/Device.cpp index b5181d6ad9..37d102187a 100644 --- a/kernel/kernel/Input/PS2/Device.cpp +++ b/kernel/kernel/Input/PS2/Device.cpp @@ -12,7 +12,7 @@ namespace Kernel::Input PS2Device::PS2Device(PS2Controller& controller) : CharacterDevice(0440, 0, 901) , m_rdev(makedev(DeviceNumber::Input, DevFileSystem::get().get_next_input_device())) - , m_name(BAN::String::formatted("input{}", minor(m_rdev))) + , m_name(MUST(BAN::String::formatted("input{}", minor(m_rdev)))) , m_controller(controller) { } diff --git a/kernel/kernel/Storage/Partition.cpp b/kernel/kernel/Storage/Partition.cpp index 119b25494c..54e343e1b8 100644 --- a/kernel/kernel/Storage/Partition.cpp +++ b/kernel/kernel/Storage/Partition.cpp @@ -21,7 +21,7 @@ namespace Kernel , m_first_block(first_block) , m_last_block(last_block) , m_attributes(attr) - , m_name(BAN::String::formatted("{}{}", name_prefix, index)) + , m_name(MUST(BAN::String::formatted("{}{}", name_prefix, index))) , m_rdev(makedev(major(device->rdev()), index)) { memcpy(m_label, label, sizeof(m_label)); diff --git a/kernel/kernel/Terminal/Serial.cpp b/kernel/kernel/Terminal/Serial.cpp index e60b8021dc..2c9c41d39c 100644 --- a/kernel/kernel/Terminal/Serial.cpp +++ b/kernel/kernel/Terminal/Serial.cpp @@ -179,11 +179,10 @@ namespace Kernel SerialTTY::SerialTTY(Serial serial) : TTY(0600, 0, 0) + , m_name(MUST(BAN::String::formatted("ttyS{}", minor(rdev())))) , m_serial(serial) , m_rdev(next_rdev()) - { - m_name = BAN::String::formatted("ttyS{}", minor(rdev())); - } + {} BAN::ErrorOr> SerialTTY::create(Serial serial) { diff --git a/kernel/kernel/Terminal/VirtualTTY.cpp b/kernel/kernel/Terminal/VirtualTTY.cpp index 014a099a69..b4a4987079 100644 --- a/kernel/kernel/Terminal/VirtualTTY.cpp +++ b/kernel/kernel/Terminal/VirtualTTY.cpp @@ -43,11 +43,10 @@ namespace Kernel VirtualTTY::VirtualTTY(TerminalDriver* driver) : TTY(0600, 0, 0) + , m_name(MUST(BAN::String::formatted("tty{}", minor(rdev())))) , m_terminal_driver(driver) , m_rdev(next_rdev()) { - m_name = BAN::String::formatted("tty{}", minor(rdev())); - m_width = m_terminal_driver->width(); m_height = m_terminal_driver->height(); diff --git a/userspace/Shell/main.cpp b/userspace/Shell/main.cpp index b921198d71..b36c30651c 100644 --- a/userspace/Shell/main.cpp +++ b/userspace/Shell/main.cpp @@ -41,7 +41,7 @@ BAN::Optional parse_dollar(BAN::StringView command, size_t& i) if (command[i] == '?') { i++; - return BAN::String::formatted("{}", last_return); + return MUST(BAN::String::formatted("{}", last_return)); } if (isalnum(command[i])) { diff --git a/userspace/WindowServer/main.cpp b/userspace/WindowServer/main.cpp index c111597d6d..ffa50b668d 100644 --- a/userspace/WindowServer/main.cpp +++ b/userspace/WindowServer/main.cpp @@ -52,7 +52,7 @@ Config parse_config() return config; } - auto config_path = BAN::String::formatted("{}/.config/WindowServer.conf", home_env); + auto config_path = MUST(BAN::String::formatted("{}/.config/WindowServer.conf", home_env)); FILE* fconfig = fopen(config_path.data(), "r"); if (!fconfig) { diff --git a/userspace/ls/main.cpp b/userspace/ls/main.cpp index 0a743bf2a1..59f06696a3 100644 --- a/userspace/ls/main.cpp +++ b/userspace/ls/main.cpp @@ -72,14 +72,14 @@ BAN::String build_access_string(mode_t mode) BAN::String build_hard_links_string(nlink_t links) { - return BAN::String::formatted("{}", links); + return MUST(BAN::String::formatted("{}", links)); } BAN::String build_owner_name_string(uid_t uid) { struct passwd* passwd = getpwuid(uid); if (passwd == nullptr) - return BAN::String::formatted("{}", uid); + return MUST(BAN::String::formatted("{}", uid)); return BAN::String(BAN::StringView(passwd->pw_name)); } @@ -87,13 +87,13 @@ BAN::String build_owner_group_string(gid_t gid) { struct group* grp = getgrgid(gid); if (grp == nullptr) - return BAN::String::formatted("{}", gid); + return MUST(BAN::String::formatted("{}", gid)); return BAN::String(BAN::StringView(grp->gr_name)); } BAN::String build_size_string(off_t size) { - return BAN::String::formatted("{}", size); + return MUST(BAN::String::formatted("{}", size)); } BAN::String build_month_string(BAN::Time time) @@ -104,15 +104,15 @@ BAN::String build_month_string(BAN::Time time) BAN::String build_day_string(BAN::Time time) { - return BAN::String::formatted("{}", time.day); + return MUST(BAN::String::formatted("{}", time.day)); } BAN::String build_time_string(BAN::Time time) { static uint32_t current_year = ({ timespec real_time; clock_gettime(CLOCK_REALTIME, &real_time); BAN::from_unix_time(real_time.tv_sec).year; }); if (time.year != current_year) - return BAN::String::formatted("{}", time.year); - return BAN::String::formatted("{2}:{2}", time.hour, time.minute); + return MUST(BAN::String::formatted("{}", time.year)); + return MUST(BAN::String::formatted("{2}:{2}", time.hour, time.minute)); } int list_directory(const BAN::String& path, config_t config) @@ -213,7 +213,7 @@ int list_directory(const BAN::String& path, config_t config) GET_ENTRY_STRING(day, time); GET_ENTRY_STRING(time, time); - full_entry.full_name = BAN::String::formatted("{}{}\e[m", entry_color(entry.st.st_mode), entry.name); + full_entry.full_name = MUST(BAN::String::formatted("{}{}\e[m", entry_color(entry.st.st_mode), entry.name)); MUST(full_entries.push_back(BAN::move(full_entry))); }