aboutsummaryrefslogtreecommitdiff
path: root/src/base
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-09-07 03:34:56 +0800
committerYuqian Yang <crupest@crupest.life>2025-09-07 03:34:56 +0800
commit20123151d12a0b01453ab6a36c84e4d3e5ea9504 (patch)
tree27543f3e5bf6430298c94c38bad6ecc83dafdd47 /src/base
parent227118866190a7fe17b42e8c589c475781c69f33 (diff)
downloadcru-20123151d12a0b01453ab6a36c84e4d3e5ea9504.tar.gz
cru-20123151d12a0b01453ab6a36c84e4d3e5ea9504.tar.bz2
cru-20123151d12a0b01453ab6a36c84e4d3e5ea9504.zip
Remove some usage of my format.
Diffstat (limited to 'src/base')
-rw-r--r--src/base/Exception.cpp15
-rw-r--r--src/base/io/Stream.cpp13
-rw-r--r--src/base/platform/unix/PosixSpawnSubProcess.cpp54
-rw-r--r--src/base/platform/unix/UnixFileStream.cpp7
4 files changed, 53 insertions, 36 deletions
diff --git a/src/base/Exception.cpp b/src/base/Exception.cpp
index 2bf66fc6..571c115f 100644
--- a/src/base/Exception.cpp
+++ b/src/base/Exception.cpp
@@ -17,9 +17,20 @@ Exception::~Exception() {}
const char* Exception::what() const noexcept { return message_.c_str(); }
-void Exception::AppendMessage(StringView additional_message) {
+void Exception::AppendMessage(std::string_view additional_message) {
message_ += " ";
- message_ += additional_message.ToUtf8();
+ message_ += additional_message;
+}
+
+void Exception::AppendMessage(
+ std::optional<std::string_view> additional_message) {
+ if (additional_message) AppendMessage(*additional_message);
+}
+
+void Exception::SetMessage(StringView message) { SetMessage(message.ToUtf8()); }
+
+void Exception::AppendMessage(StringView additional_message) {
+ AppendMessage(std::string_view(additional_message.ToUtf8()));
}
void Exception::AppendMessage(std::optional<StringView> additional_message) {
diff --git a/src/base/io/Stream.cpp b/src/base/io/Stream.cpp
index 1944ea7e..9b7ef856 100644
--- a/src/base/io/Stream.cpp
+++ b/src/base/io/Stream.cpp
@@ -1,15 +1,18 @@
#include "cru/base/io/Stream.h"
#include "cru/base/Exception.h"
-#include "cru/base/Format.h"
+#include <format>
#include <utility>
namespace cru::io {
StreamOperationNotSupportedException::StreamOperationNotSupportedException(
- String operation)
- : operation_(std::move(operation)) {
- SetMessage(Format(u"Stream operation {} not supported.", operation_));
-}
+ StringView operation)
+ : StreamOperationNotSupportedException(operation.ToUtf8()) {}
+
+StreamOperationNotSupportedException::StreamOperationNotSupportedException(
+ std::string operation)
+ : Exception(std::format("Stream operation {} not supported.", operation)),
+ operation_(std::move(operation)) {}
void StreamOperationNotSupportedException::CheckSeek(bool seekable) {
if (!seekable) throw StreamOperationNotSupportedException(u"seek");
diff --git a/src/base/platform/unix/PosixSpawnSubProcess.cpp b/src/base/platform/unix/PosixSpawnSubProcess.cpp
index 4471c39c..f7d2e855 100644
--- a/src/base/platform/unix/PosixSpawnSubProcess.cpp
+++ b/src/base/platform/unix/PosixSpawnSubProcess.cpp
@@ -1,6 +1,5 @@
#include "cru/base/platform/unix/PosixSpawnSubProcess.h"
#include "cru/base/Exception.h"
-#include "cru/base/Format.h"
#include "cru/base/Guard.h"
#include "cru/base/String.h"
#include "cru/base/SubProcess.h"
@@ -10,7 +9,9 @@
#include <spawn.h>
#include <sys/wait.h>
#include <unistd.h>
+#include <format>
#include <memory>
+#include <string_view>
#include <unordered_map>
namespace cru::platform::unix {
@@ -35,7 +36,7 @@ char** CreateCstrArray(const std::vector<String>& argv) {
char** CreateCstrArray(const std::unordered_map<String, String>& envp) {
std::vector<String> str_array;
for (auto& [key, value] : envp) {
- str_array.push_back(cru::Format(u"{}={}", key, value));
+ str_array.push_back(key + u"=" + value);
}
return CreateCstrArray(str_array);
}
@@ -54,7 +55,7 @@ void DestroyCstrArray(char** argv) {
void PosixSpawnSubProcessImpl::PlatformCreateProcess(
const SubProcessStartInfo& start_info) {
- auto check_error = [](int error, String message) {
+ auto check_error = [](int error, std::string message) {
if (error == 0) return;
std::unique_ptr<ErrnoException> inner(new ErrnoException(error));
throw SubProcessFailedToStartException(std::move(message),
@@ -63,49 +64,52 @@ void PosixSpawnSubProcessImpl::PlatformCreateProcess(
posix_spawn_file_actions_t file_actions;
check_error(posix_spawn_file_actions_init(&file_actions),
- u"Failed to call posix_spawn_file_actions_init.");
+ "Failed to call posix_spawn_file_actions_init.");
Guard file_actions_guard(
[&file_actions] { posix_spawn_file_actions_destroy(&file_actions); });
auto add_dup2 = [&check_error, &file_actions](int from, int to,
- StringView name) {
+ std::string_view name) {
check_error(
posix_spawn_file_actions_adddup2(&file_actions, from, to),
- Format(u"Failed to call posix_spawn_file_actions_adddup2 on {}.",
- name));
+ std::format("Failed to call posix_spawn_file_actions_adddup2 on {}.",
+ name));
};
auto add_close = [&check_error, &file_actions](
- UniDirectionalUnixPipeResult& pipe, StringView name) {
- check_error(posix_spawn_file_actions_addclose(&file_actions, pipe.read),
- Format(u"Failed to call posix_spawn_file_actions_addclose on "
- u"read end of pipe {}.",
- name));
- check_error(posix_spawn_file_actions_addclose(&file_actions, pipe.write),
- Format(u"Failed to call posix_spawn_file_actions_addclose on "
- u"write end of pipe {}.",
- name));
+ UniDirectionalUnixPipeResult& pipe,
+ std::string_view name) {
+ check_error(
+ posix_spawn_file_actions_addclose(&file_actions, pipe.read),
+ std::format("Failed to call posix_spawn_file_actions_addclose on "
+ "read end of pipe {}.",
+ name));
+ check_error(
+ posix_spawn_file_actions_addclose(&file_actions, pipe.write),
+ std::format("Failed to call posix_spawn_file_actions_addclose on "
+ "write end of pipe {}.",
+ name));
};
auto my_stdin = OpenUniDirectionalPipe();
auto my_stdout = OpenUniDirectionalPipe();
auto my_stderr = OpenUniDirectionalPipe();
- add_dup2(my_stdin.read, STDIN_FILENO, u"stdin");
- add_dup2(my_stdout.write, STDOUT_FILENO, u"stdout");
- add_dup2(my_stderr.write, STDERR_FILENO, u"stderr");
- add_close(my_stdin, u"stdin");
- add_close(my_stdout, u"stdout");
- add_close(my_stderr, u"stderr");
+ add_dup2(my_stdin.read, STDIN_FILENO, "stdin");
+ add_dup2(my_stdout.write, STDOUT_FILENO, "stdout");
+ add_dup2(my_stderr.write, STDERR_FILENO, "stderr");
+ add_close(my_stdin, "stdin");
+ add_close(my_stdout, "stdout");
+ add_close(my_stderr, "stderr");
posix_spawnattr_t attr;
check_error(posix_spawnattr_init(&attr),
- u"Failed to call posix_spawnattr_init.");
+ "Failed to call posix_spawnattr_init.");
Guard attr_guard([&attr] { posix_spawnattr_destroy(&attr); });
#ifdef __APPLE__
check_error(posix_spawnattr_setflags(&attr, POSIX_SPAWN_CLOEXEC_DEFAULT),
- u"Failed to set flag POSIX_SPAWN_CLOEXEC_DEFAULT (osx).");
+ "Failed to set flag POSIX_SPAWN_CLOEXEC_DEFAULT (osx).");
#endif
auto exe = start_info.program.ToUtf8();
@@ -121,7 +125,7 @@ void PosixSpawnSubProcessImpl::PlatformCreateProcess(
check_error(
posix_spawnp(&pid_, exe.c_str(), &file_actions, &attr, argv, envp),
- u"Failed to call posix_spawnp.");
+ "Failed to call posix_spawnp.");
stdin_stream_ = std::make_unique<UnixFileStream>(std::move(my_stdin.write),
false, false, true);
diff --git a/src/base/platform/unix/UnixFileStream.cpp b/src/base/platform/unix/UnixFileStream.cpp
index e1a6080b..0772c279 100644
--- a/src/base/platform/unix/UnixFileStream.cpp
+++ b/src/base/platform/unix/UnixFileStream.cpp
@@ -1,11 +1,11 @@
#include "cru/base/platform/unix/UnixFileStream.h"
#include "cru/base/Exception.h"
-#include "cru/base/Format.h"
#include "cru/base/io/Stream.h"
#include <fcntl.h>
#include <sys/fcntl.h>
#include <unistd.h>
+#include <format>
namespace cru::platform::unix {
using namespace cru::io;
@@ -41,9 +41,8 @@ int MapSeekOrigin(Stream::SeekOrigin origin) {
UnixFileStream::UnixFileStream(const char *path, int oflag, mode_t mode) {
file_descriptor_ = UnixFileDescriptor(::open(path, oflag, mode));
if (file_descriptor_ == -1) {
- throw ErrnoException(
- Format(u"Failed to open file {} with oflag {}, mode {}.",
- String::FromUtf8(path), oflag, mode));
+ throw ErrnoException(std::format(
+ "Failed to open file {} with oflag {}, mode {}.", path, oflag, mode));
}
SetSupportedOperations(