Skip to content

Commit

Permalink
ListText: GetListItemText() returns std::string_view
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Aug 31, 2024
1 parent 0cff5fa commit adeb34b
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 69 deletions.
13 changes: 7 additions & 6 deletions src/FileListPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

#include <string.h>

using std::string_view_literals::operator""sv;

#define BUFSIZE 1024

#ifndef NCMPC_MINI
Expand Down Expand Up @@ -67,7 +69,7 @@ screen_browser_sync_highlights(FileList &fl, const MpdQueue &playlist) noexcept

#endif

const char *
std::string_view
FileListPage::GetListItemText(std::span<char> buffer,
unsigned idx) const noexcept
{
Expand All @@ -78,7 +80,7 @@ FileListPage::GetListItemText(std::span<char> buffer,
const auto *entity = entry.entity;

if( entity == nullptr )
return "..";
return ".."sv;

if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_DIRECTORY) {
const auto *dir = mpd_entity_get_directory(entity);
Expand All @@ -87,15 +89,14 @@ FileListPage::GetListItemText(std::span<char> buffer,
} else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
const auto *song = mpd_entity_get_song(entity);

strfsong(buffer, options.list_format.c_str(), song);
return buffer.data();
return strfsong(buffer, options.list_format.c_str(), song);
} else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST) {
const auto *playlist = mpd_entity_get_playlist(entity);
const char *name = GetUriFilename(mpd_playlist_get_path(playlist));
return utf8_to_locale(name, buffer);
}

return "Error: Unknown entry!";
return "Error: Unknown entry!"sv;
}

static bool
Expand Down Expand Up @@ -485,7 +486,7 @@ screen_browser_paint_directory(const Window window, unsigned width,

static void
screen_browser_paint_playlist(const Window window, unsigned width,
bool selected, const char *name) noexcept
bool selected, std::string_view name) noexcept
{
row_paint_text(window, width, Style::PLAYLIST, selected, name);
}
Expand Down
4 changes: 2 additions & 2 deletions src/FileListPage.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ private:
bool selected) const noexcept final;

/* virtual methods from class ListText */
const char *GetListItemText(std::span<char> buffer,
unsigned i) const noexcept override;
std::string_view GetListItemText(std::span<char> buffer,
unsigned i) const noexcept override;

public:
/* virtual methods from class Page */
Expand Down
8 changes: 4 additions & 4 deletions src/HelpPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ class HelpPage final : public ListPage, ListRenderer, ListText {
bool selected) const noexcept override;

/* virtual methods from class ListText */
const char *GetListItemText(std::span<char> buffer,
unsigned i) const noexcept override;
std::string_view GetListItemText(std::span<char> buffer,
unsigned i) const noexcept override;

/* virtual methods from class Page */
void Paint() const noexcept override;
Expand All @@ -234,7 +234,7 @@ class HelpPage final : public ListPage, ListRenderer, ListText {
}
};

const char *
std::string_view
HelpPage::GetListItemText(std::span<char>, unsigned i) const noexcept
{
const auto *row = &help_text[i];
Expand All @@ -247,7 +247,7 @@ HelpPage::GetListItemText(std::span<char>, unsigned i) const noexcept
if (row->command != Command::NONE)
return get_key_description(row->command);

return "";
return {};
}

static std::unique_ptr<Page>
Expand Down
38 changes: 20 additions & 18 deletions src/KeyDefPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <errno.h>
#include <string.h>

using std::string_view_literals::operator""sv;

class CommandKeysPage final : public ListPage, ListText {
ScreenManager &screen;
Page *const parent;
Expand Down Expand Up @@ -112,8 +114,8 @@ class CommandKeysPage final : public ListPage, ListText {

private:
/* virtual methods from class ListText */
const char *GetListItemText(std::span<char> buffer,
unsigned i) const noexcept override;
std::string_view GetListItemText(std::span<char> buffer,
unsigned i) const noexcept override;
};

/* TODO: rename to check_n_keys / subcmd_count_keys? */
Expand Down Expand Up @@ -202,25 +204,25 @@ CommandKeysPage::AddKey()
OverwriteKey(n_keys);
}

const char *
std::string_view
CommandKeysPage::GetListItemText(std::span<char> buffer,
unsigned idx) const noexcept
{
if (idx == GetLeavePosition())
return "[..]";
return "[..]"sv;

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

assert(IsKeyPosition(idx));

snprintf(buffer.data(), buffer.size(),
"%d. %-20s (%d) ", idx,
GetLocalizedKeyName(binding->keys[PositionToKeyIndex(idx)]),
binding->keys[PositionToKeyIndex(idx)]);
return buffer.data();
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};
}

void
Expand Down Expand Up @@ -352,8 +354,8 @@ class CommandListPage final : public ListPage, ListText {

private:
/* virtual methods from class ListText */
const char *GetListItemText(std::span<char> buffer,
unsigned i) const noexcept override;
std::string_view GetListItemText(std::span<char> buffer,
unsigned i) const noexcept override;
};

bool
Expand Down Expand Up @@ -407,7 +409,7 @@ CommandListPage::Save()
fclose(f);
}

const char *
std::string_view
CommandListPage::GetListItemText(std::span<char> buffer,
unsigned idx) const noexcept
{
Expand All @@ -432,11 +434,11 @@ CommandListPage::GetListItemText(std::span<char> buffer,
if (len < get_cmds_max_name_width())
memset(buffer.data() + len, ' ', get_cmds_max_name_width() - len);

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 = snprintf(buffer.data() + get_cmds_max_name_width(),
buffer.size() - get_cmds_max_name_width(),
" - %s", my_gettext(get_command_definitions()[idx].description));

return buffer.data();
return {buffer.data(), length};
}

void
Expand Down
5 changes: 3 additions & 2 deletions src/ListText.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
#pragma once

#include <span>
#include <string_view>

class ListText {
public:
/**
* @return the text in the locale charset
*/
[[gnu::pure]]
virtual const char *GetListItemText(std::span<char> buffer,
unsigned i) const noexcept = 0;
virtual std::string_view GetListItemText(std::span<char> buffer,
unsigned i) const noexcept = 0;
};
9 changes: 3 additions & 6 deletions src/ListWindow.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ ListWindow::Find(const ListText &text,
do {
while (i < GetLength()) {
char buffer[1024];
const char *label = text.GetListItemText(buffer, i);
assert(label != nullptr);
const std::string_view label = text.GetListItemText(buffer, i);

if (m(label)) {
MoveCursor(i);
Expand Down Expand Up @@ -110,8 +109,7 @@ ListWindow::ReverseFind(const ListText &text,
do {
while (i >= 0) {
char buffer[1024];
const char *label = text.GetListItemText(buffer, i);
assert(label != nullptr);
const std::string_view label = text.GetListItemText(buffer, i);

if (m(label)) {
MoveCursor(i);
Expand Down Expand Up @@ -144,8 +142,7 @@ ListWindow::Jump(const ListText &text, const char *str) noexcept

for (unsigned i = 0; i < GetLength(); i++) {
char buffer[1024];
const char *label = text.GetListItemText(buffer, i);
assert(label != nullptr);
const std::string_view label = text.GetListItemText(buffer, i);

if (m(label)) {
MoveCursor(i);
Expand Down
6 changes: 3 additions & 3 deletions src/QueuePage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ class QueuePage final : public ListPage, ListRenderer, ListText {
bool selected) const noexcept override;

/* virtual methods from class ListText */
const char *GetListItemText(std::span<char> buffer,
unsigned i) const noexcept override;
std::string_view GetListItemText(std::span<char> buffer,
unsigned i) const noexcept override;

public:
/* virtual methods from class Page */
Expand Down Expand Up @@ -179,7 +179,7 @@ QueuePage::RestoreSelection()
SaveSelection();
}

const char *
std::string_view
QueuePage::GetListItemText(std::span<char> buffer,
unsigned idx) const noexcept
{
Expand Down
28 changes: 15 additions & 13 deletions src/SearchPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,27 @@ class SearchPage final : public FileListPage {
class SearchHelpText final : public ListText {
public:
/* virtual methods from class ListText */
const char *GetListItemText(std::span<char> buffer,
unsigned idx) const noexcept override {
std::string_view GetListItemText(std::span<char> buffer,
unsigned idx) const noexcept override {
assert(idx < std::size(help_text));

if (idx == 0) {
snprintf(buffer.data(), buffer.size(),
" %s : %s",
GetGlobalKeyBindings().GetKeyNames(Command::SCREEN_SEARCH).c_str(),
"New search");
return buffer.data();
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 == 1) {
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();
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};
}

return help_text[idx];
Expand Down
8 changes: 4 additions & 4 deletions src/SongPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ class SongPage final : public ListPage, ListText {

private:
/* virtual methods from class ListText */
const char *GetListItemText(std::span<char> buffer,
unsigned i) const noexcept override;
std::string_view GetListItemText(std::span<char> buffer,
unsigned i) const noexcept override;
};

void
Expand All @@ -164,10 +164,10 @@ SongPage::Clear() noexcept
}
}

const char *
std::string_view
SongPage::GetListItemText(std::span<char>, unsigned idx) const noexcept
{
return lines[idx].c_str();
return lines[idx];
}

static std::unique_ptr<Page>
Expand Down
6 changes: 4 additions & 2 deletions src/TagListPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <assert.h>
#include <string.h>

using std::string_view_literals::operator""sv;

TagFilter
TagListPage::MakeCursorFilter() const noexcept
{
Expand All @@ -40,13 +42,13 @@ CompareUTF8(const std::string &a, const std::string &b)
return CollateUTF8(a.c_str(), b.c_str()) < 0;
}

const char *
std::string_view
TagListPage::GetListItemText(std::span<char> buffer,
unsigned idx) const noexcept
{
if (parent != nullptr) {
if (idx == 0)
return "..";
return ".."sv;

--idx;
}
Expand Down
4 changes: 2 additions & 2 deletions src/TagListPage.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,6 @@ public:
bool selected) const noexcept override;

/* virtual methods from class ListText */
const char *GetListItemText(std::span<char> buffer,
unsigned i) const noexcept override;
std::string_view GetListItemText(std::span<char> buffer,
unsigned i) const noexcept override;
};
5 changes: 2 additions & 3 deletions src/TextListRenderer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

static void
list_window_paint_row(const Window window, unsigned width, bool selected,
const char *text) noexcept
std::string_view text) noexcept
{
row_paint_text(window, width, Style::LIST,
selected, text);
Expand All @@ -20,8 +20,7 @@ TextListRenderer::PaintListItem(const Window window, unsigned i, unsigned,
unsigned width, bool selected) const noexcept
{
char buffer[1024];
const char *label = text.GetListItemText(buffer, i);
assert(label != nullptr);
const std::string_view label = text.GetListItemText(buffer, i);

list_window_paint_row(window, width, selected, label);
}
2 changes: 1 addition & 1 deletion src/TextPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ TextPage::Append(const char *str) noexcept
lw.SetLength(lines.size());
}

const char *
std::string_view
TextPage::GetListItemText(std::span<char> buffer, unsigned idx) const noexcept
{
assert(idx < lines.size());
Expand Down
4 changes: 2 additions & 2 deletions src/TextPage.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ public:

private:
/* virtual methods from class ListText */
const char *GetListItemText(std::span<char> buffer,
unsigned i) const noexcept override;
std::string_view GetListItemText(std::span<char> buffer,
unsigned i) const noexcept override;
};
2 changes: 1 addition & 1 deletion src/paint.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ row_clear_to_eol(const Window window, unsigned width, bool selected) noexcept
static inline void
row_paint_text(const Window window, unsigned width,
Style style, bool selected,
const char *text) noexcept
std::string_view text) noexcept
{
row_color(window, style, selected);

Expand Down

0 comments on commit adeb34b

Please sign in to comment.