Skip to content

Commit

Permalink
util/SPrintf: wrapper for snprintf()
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Sep 1, 2024
1 parent ef7c111 commit c754835
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
20 changes: 8 additions & 12 deletions src/KeyDefPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "GlobalBindings.hxx"
#include "screen_utils.hxx"
#include "Options.hxx"
#include "util/SPrintf.hxx"

#include <assert.h>
#include <errno.h>
Expand Down Expand Up @@ -211,18 +212,14 @@ CommandKeysPage::GetListItemText(std::span<char> buffer,
if (idx == GetLeavePosition())
return "[..]"sv;

if (idx == GetAddPosition()) {
std::size_t length = snprintf(buffer.data(), buffer.size(), "%d. %s", idx, _("Add new key"));
return {buffer.data(), length};
}
if (idx == GetAddPosition())
return SPrintf(buffer, "%d. %s", idx, _("Add new key"));

assert(IsKeyPosition(idx));

std::size_t length = snprintf(buffer.data(), buffer.size(),
"%d. %-20s (%d) ", idx,
GetLocalizedKeyName(binding->keys[PositionToKeyIndex(idx)]),
binding->keys[PositionToKeyIndex(idx)]);
return {buffer.data(), length};
return SPrintf(buffer, "%d. %-20s (%d) ", idx,
GetLocalizedKeyName(binding->keys[PositionToKeyIndex(idx)]),
binding->keys[PositionToKeyIndex(idx)]);
}

void
Expand Down Expand Up @@ -434,9 +431,8 @@ CommandListPage::GetListItemText(std::span<char> buffer,
if (len < get_cmds_max_name_width())
memset(buffer.data() + len, ' ', get_cmds_max_name_width() - len);

std::size_t length = snprintf(buffer.data() + get_cmds_max_name_width(),
buffer.size() - get_cmds_max_name_width(),
" - %s", my_gettext(get_command_definitions()[idx].description));
std::size_t length = SPrintf(buffer.subspan(get_cmds_max_name_width()),
" - %s", my_gettext(get_command_definitions()[idx].description)).size();

return {buffer.data(), get_cmds_max_name_width() + length};
}
Expand Down
27 changes: 10 additions & 17 deletions src/SearchPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "screen_utils.hxx"
#include "FileListPage.hxx"
#include "filelist.hxx"
#include "util/SPrintf.hxx"
#include "util/StringAPI.hxx"

#include <iterator>
Expand Down Expand Up @@ -129,24 +130,16 @@ class SearchHelpText final : public ListText {
unsigned idx) const noexcept override {
assert(idx < std::size(help_text));

if (idx == 0) {
std::size_t length =
snprintf(buffer.data(), buffer.size(),
" %s : %s",
GetGlobalKeyBindings().GetKeyNames(Command::SCREEN_SEARCH).c_str(),
"New search");
return {buffer.data(), length};
}
if (idx == 0)
return SPrintf(buffer, " %s : %s",
GetGlobalKeyBindings().GetKeyNames(Command::SCREEN_SEARCH).c_str(),
"New search");

if (idx == 1) {
std::size_t length =
snprintf(buffer.data(), buffer.size(),
" %s : %s [%s]",
GetGlobalKeyBindings().GetKeyNames(Command::SEARCH_MODE).c_str(),
get_key_description(Command::SEARCH_MODE),
my_gettext(mode[options.search_mode].label));
return {buffer.data(), length};
}
if (idx == 1)
return SPrintf(buffer, " %s : %s [%s]",
GetGlobalKeyBindings().GetKeyNames(Command::SEARCH_MODE).c_str(),
get_key_description(Command::SEARCH_MODE),
my_gettext(mode[options.search_mode].label));

return help_text[idx];
}
Expand Down
19 changes: 19 additions & 0 deletions src/util/SPrintf.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: BSD-2-Clause
// author: Max Kellermann <[email protected]>

#pragma once

#include <span>

#include <stdio.h>

template<typename... Args>
static inline std::string_view
SPrintf(std::span<char> buffer, const char *fmt, Args&&... args) noexcept
{
int length = snprintf(buffer.data(), buffer.size(), fmt, args...);
if (length < 0)
return {};

return {buffer.data(), static_cast<std::size_t>(length)};
}

0 comments on commit c754835

Please sign in to comment.