summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-01-30 16:28:51 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-01-30 16:28:51 +0100
commit027d26cd5d6dbfceef0c2ade42a51068dcdcf43f (patch)
tree1c829b0a51edaf4c44b3d0b2a4c383b28fd2a563
parente9b948103d45b4e9b6498f62e70dd64152dea3e6 (diff)
downloadserenity-027d26cd5d6dbfceef0c2ade42a51068dcdcf43f.zip
Add a String::format() and use that in place of ksprintf() in the Kernel.
You're never gonna be right 100% of the time when guessing how much buffer space you need. This avoids having to make that type of decision in a bunch of cases. :^)
-rw-r--r--AK/AKString.h2
-rw-r--r--AK/String.cpp12
-rw-r--r--AK/StringBuilder.cpp11
-rw-r--r--AK/StringBuilder.h2
-rw-r--r--Kernel/ELFLoader.cpp8
-rw-r--r--Kernel/FileDescriptor.cpp14
-rw-r--r--Kernel/MasterPTY.cpp4
-rw-r--r--Kernel/ProcFileSystem.cpp4
-rw-r--r--Kernel/SlavePTY.cpp4
-rw-r--r--Kernel/VirtualConsole.cpp4
-rw-r--r--WindowServer/WSWindowManager.cpp9
11 files changed, 40 insertions, 34 deletions
diff --git a/AK/AKString.h b/AK/AKString.h
index 54225a3176..654ba91468 100644
--- a/AK/AKString.h
+++ b/AK/AKString.h
@@ -95,6 +95,8 @@ public:
ByteBuffer to_byte_buffer() const;
+ static String format(const char*, ...);
+
private:
RetainPtr<StringImpl> m_impl;
};
diff --git a/AK/String.cpp b/AK/String.cpp
index 3da0ab6b99..638296f7d4 100644
--- a/AK/String.cpp
+++ b/AK/String.cpp
@@ -1,5 +1,7 @@
#include "AKString.h"
#include "StdLibExtras.h"
+#include "StringBuilder.h"
+#include <LibC/stdarg.h>
namespace AK {
@@ -92,4 +94,14 @@ unsigned String::toUInt(bool& ok) const
return value;
}
+String String::format(const char* fmt, ...)
+{
+ StringBuilder builder;
+ va_list ap;
+ va_start(ap, fmt);
+ builder.appendvf(fmt, ap);
+ va_end(ap);
+ return builder.build();
+}
+
}
diff --git a/AK/StringBuilder.cpp b/AK/StringBuilder.cpp
index a8c2cbcfbb..790ea55440 100644
--- a/AK/StringBuilder.cpp
+++ b/AK/StringBuilder.cpp
@@ -41,13 +41,18 @@ void StringBuilder::append(char ch)
m_length += 1;
}
-void StringBuilder::appendf(const char* fmt, ...)
+void StringBuilder::appendvf(const char* fmt, va_list ap)
{
- va_list ap;
- va_start(ap, fmt);
printfInternal([this] (char*&, char ch) {
append(ch);
}, nullptr, fmt, ap);
+}
+
+void StringBuilder::appendf(const char* fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ appendvf(fmt, ap);
va_end(ap);
}
diff --git a/AK/StringBuilder.h b/AK/StringBuilder.h
index 60c7569f0f..a01f5504c4 100644
--- a/AK/StringBuilder.h
+++ b/AK/StringBuilder.h
@@ -2,6 +2,7 @@
#include "AKString.h"
#include "Vector.h"
+#include <LibC/stdarg.h>
namespace AK {
@@ -14,6 +15,7 @@ public:
void append(char);
void append(const char*, size_t);
void appendf(const char*, ...);
+ void appendvf(const char*, va_list);
String build();
ByteBuffer to_byte_buffer();
diff --git a/Kernel/ELFLoader.cpp b/Kernel/ELFLoader.cpp
index 324e2dff4b..35e11f0768 100644
--- a/Kernel/ELFLoader.cpp
+++ b/Kernel/ELFLoader.cpp
@@ -205,15 +205,11 @@ char* ELFLoader::symbol_ptr(const char* name)
bool ELFLoader::allocate_section(LinearAddress laddr, size_t size, size_t alignment, bool is_readable, bool is_writable)
{
ASSERT(alloc_section_hook);
- char namebuf[16];
- ksprintf(namebuf, "elf-alloc-%s%s", is_readable ? "r" : "", is_writable ? "w" : "");
- return alloc_section_hook(laddr, size, alignment, is_readable, is_writable, namebuf);
+ return alloc_section_hook(laddr, size, alignment, is_readable, is_writable, String::format("elf-alloc-%s%s", is_readable ? "r" : "", is_writable ? "w" : ""));
}
bool ELFLoader::map_section(LinearAddress laddr, size_t size, size_t alignment, size_t offset_in_image, bool is_readable, bool is_writable)
{
ASSERT(alloc_section_hook);
- char namebuf[16];
- ksprintf(namebuf, "elf-map-%s%s", is_readable ? "r" : "", is_writable ? "w" : "");
- return map_section_hook(laddr, size, alignment, offset_in_image, is_readable, is_writable, namebuf);
+ return map_section_hook(laddr, size, alignment, offset_in_image, is_readable, is_writable, String::format("elf-map-%s%s", is_readable ? "r" : "", is_writable ? "w" : ""));
}
diff --git a/Kernel/FileDescriptor.cpp b/Kernel/FileDescriptor.cpp
index bb7a6f4bcc..527eda23a7 100644
--- a/Kernel/FileDescriptor.cpp
+++ b/Kernel/FileDescriptor.cpp
@@ -292,16 +292,10 @@ String FileDescriptor::absolute_path()
Stopwatch sw("absolute_path");
if (is_tty())
return tty()->tty_name();
- if (is_fifo()) {
- char buf[32];
- ksprintf(buf, "fifo:%x", m_fifo.ptr());
- return buf;
- }
- if (is_character_device()) {
- char buf[128];
- ksprintf(buf, "device:%u,%u (%s)", m_device->major(), m_device->minor(), m_device->class_name());
- return buf;
- }
+ if (is_fifo())
+ return String::format("fifo:%x", m_fifo.ptr());
+ if (is_character_device())
+ return String::format("device:%u,%u (%s)", m_device->major(), m_device->minor(), m_device->class_name());
ASSERT(m_inode);
return VFS::the().absolute_path(*m_inode);
}
diff --git a/Kernel/MasterPTY.cpp b/Kernel/MasterPTY.cpp
index 5ee7fee65a..5a688072c4 100644
--- a/Kernel/MasterPTY.cpp
+++ b/Kernel/MasterPTY.cpp
@@ -14,9 +14,7 @@ MasterPTY::~MasterPTY()
String MasterPTY::pts_name() const
{
- char buffer[32];
- ksprintf(buffer, "/dev/pts/%u", m_index);
- return buffer;
+ return String::format("/dev/pts/%u", m_index);
}
ssize_t MasterPTY::read(Process&, byte* buffer, size_t size)
diff --git a/Kernel/ProcFileSystem.cpp b/Kernel/ProcFileSystem.cpp
index aa985b2970..52a1942f97 100644
--- a/Kernel/ProcFileSystem.cpp
+++ b/Kernel/ProcFileSystem.cpp
@@ -153,9 +153,7 @@ ByteBuffer procfs$pid_cwd(Process& process)
void ProcFS::add_process(Process& process)
{
InterruptDisabler disabler;
- char buf[16];
- ksprintf(buf, "%d", process.pid());
- auto dir = add_file(create_directory(buf));
+ auto dir = add_file(create_directory(String::format("%d", process.pid())));
m_pid2inode.set(process.pid(), dir.index());
add_file(create_generated_file("vm", [&process] (SynthFSInode&) { return procfs$pid_vm(process); }), dir.index());
add_file(create_generated_file("vmo", [&process] (SynthFSInode&) { return procfs$pid_vmo(process); }), dir.index());
diff --git a/Kernel/SlavePTY.cpp b/Kernel/SlavePTY.cpp
index 6185f590bc..5e0c66541f 100644
--- a/Kernel/SlavePTY.cpp
+++ b/Kernel/SlavePTY.cpp
@@ -19,9 +19,7 @@ SlavePTY::~SlavePTY()
String SlavePTY::tty_name() const
{
- char buffer[32];
- ksprintf(buffer, "/dev/pts/%u", m_index);
- return buffer;
+ return String::format("/dev/pts/%u", m_index);
}
void SlavePTY::on_master_write(const byte* buffer, size_t size)
diff --git a/Kernel/VirtualConsole.cpp b/Kernel/VirtualConsole.cpp
index 55604c71ac..1e3227d02b 100644
--- a/Kernel/VirtualConsole.cpp
+++ b/Kernel/VirtualConsole.cpp
@@ -508,9 +508,7 @@ void VirtualConsole::on_tty_write(const byte* data, size_t size)
String VirtualConsole::tty_name() const
{
- char buf[16];
- ksprintf(buf, "/dev/tty%u", m_index);
- return String(buf);
+ return String::format("/dev/tty%u", m_index);
}
void VirtualConsole::set_vga_start_row(word row)
diff --git a/WindowServer/WSWindowManager.cpp b/WindowServer/WSWindowManager.cpp
index 4679b9afe9..b6f99fa3ff 100644
--- a/WindowServer/WSWindowManager.cpp
+++ b/WindowServer/WSWindowManager.cpp
@@ -191,9 +191,12 @@ void WSWindowManager::paint_window_frame(WSWindow& window)
m_back_painter->draw_text(titleBarTitleRect, window.title(), Painter::TextAlignment::CenterLeft, title_color);
Color metadata_color(96, 96, 96);
- char buffer[64];
- ksprintf(buffer, "%d:%d", window.pid(), window.window_id());
- m_back_painter->draw_text(titleBarTitleRect, buffer, Painter::TextAlignment::CenterRight, metadata_color);
+ m_back_painter->draw_text(
+ titleBarTitleRect,
+ String::format("%d:%d", window.pid(), window.window_id()),
+ Painter::TextAlignment::CenterRight,
+ metadata_color
+ );
}
void WSWindowManager::add_window(WSWindow& window)