Skip to content

Commit 1edeba9

Browse files
committed
prefer using std::format over snprintf
1 parent 7d0a670 commit 1edeba9

2 files changed

Lines changed: 13 additions & 22 deletions

File tree

api/mem/vmap.hpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -224,29 +224,23 @@ namespace os::mem {
224224
template <typename Fl>
225225
inline std::string Mapping<Fl>::to_string() const {
226226
using namespace util::literals;
227-
char buffer[1024];
228-
int len = snprintf(buffer, sizeof(buffer),
229-
"%p -> %p, size %s, flags %#x",
230-
(void*) lin,
231-
(void*) phys,
232-
util::Byte_r(size).to_string().c_str(),
233-
(int) flags);
234-
235-
const bool isseq = __builtin_popcount(page_sizes) == 1;
227+
228+
std::string s = std::format( "lin {} -> phys {}, size {}, flags {:#x}",
229+
lin, phys, util::Byte_r(size).to_string(), static_cast<unsigned>(flags));
230+
231+
const bool isseq = std::popcount(page_sizes) == 1;
236232
if (isseq) {
237-
len += snprintf(buffer + len, sizeof(buffer) - len,
238-
" (%lu pages á %s)",
239-
size / page_sizes,
240-
util::Byte_r(page_sizes).to_string().c_str());
233+
const auto pages = size / page_sizes;
234+
s += std::format(" ({} pages á {})", pages, util::Byte_r(page_sizes).to_string());
241235
} else {
242-
len += snprintf(buffer + len, sizeof(buffer) - len,
243-
" (page sizes: %s)", page_sizes_str(page_sizes).c_str());
236+
s += std::format(" (page sizes: {})", page_sizes_str(page_sizes));
244237
}
245-
return std::string(buffer, len);
238+
return s;
246239
}
247240

248241
inline std::string page_sizes_str(size_t bits) {
249242
using namespace util::literals;
243+
250244
if (bits == 0) return "None";
251245

252246
std::string out;

src/kernel/elf.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,9 @@ static const uintptr_t ELF_START = reinterpret_cast<uintptr_t>(&_ELF_START_);
6161
extern "C" char *
6262
__cxa_demangle(const char *name, char *buf, size_t *n, int *status);
6363

64-
template <typename N>
65-
static std::string to_hex_string(N n)
66-
{
67-
char buffer[16];
68-
int len = snprintf(buffer, sizeof(buffer), "%#x", n);
69-
return std::string(buffer, len);
64+
template <std::integral N>
65+
[[nodiscard]] static std::string to_hex_string(N n) {
66+
return std::format("{:#x}", n);
7067
}
7168

7269
static ElfEhdr& elf_header() {

0 commit comments

Comments
 (0)