Skip to content

Commit

Permalink
charset: change Utf8ToLocale to produce std::string_view
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Sep 2, 2024
1 parent f4150e7 commit 9b78cc5
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/FileBrowserPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ FileBrowserPage::HandleSave(struct mpdclient &c) noexcept

if(defaultname)
playlist_save(screen, c, nullptr,
Utf8ToLocale(defaultname).c_str());
Utf8ToLocaleZ{defaultname}.c_str());
else
playlist_save(screen, c, nullptr, nullptr);
}
Expand Down Expand Up @@ -244,7 +244,7 @@ FileBrowserPage::HandleDelete(struct mpdclient &c) noexcept
char prompt[256];
snprintf(prompt, sizeof(prompt),
_("Delete playlist %s?"),
Utf8ToLocale(GetUriFilename(mpd_playlist_get_path(playlist))).c_str());
Utf8ToLocaleZ{GetUriFilename(mpd_playlist_get_path(playlist))}.c_str());
bool confirmed = screen_get_yesno(screen, prompt, false);
if (!confirmed) {
/* translators: a dialog was aborted by the user */
Expand Down Expand Up @@ -288,7 +288,7 @@ FileBrowserPage::GetTitle(std::span<char> buffer) const noexcept

return FmtTruncate(buffer, "{}: {}",
/* translators: caption of the browser screen */
_("Browse"), Utf8ToLocale(path).c_str());
_("Browse"), (std::string_view)Utf8ToLocale{path});
}

void
Expand Down
14 changes: 6 additions & 8 deletions src/FileListPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ load_playlist(struct mpdclient *c, const struct mpd_playlist *playlist)
if (mpd_run_load(connection, mpd_playlist_get_path(playlist))) {
const char *name = GetUriFilename(mpd_playlist_get_path(playlist));
screen_status_printf(_("Loading playlist '%s'"),
Utf8ToLocale(name).c_str());
Utf8ToLocaleZ{name}.c_str());

c->events |= MPD_IDLE_QUEUE;
} else
Expand Down Expand Up @@ -239,7 +239,7 @@ browser_select_entry(struct mpdclient &c, FileListEntry &entry,
return false;

screen_status_fmt(_("Adding '{}' to queue"),
Utf8ToLocale(mpd_directory_get_path(dir)).c_str());
(std::string_view)Utf8ToLocale{mpd_directory_get_path(dir)});
return true;
}

Expand Down Expand Up @@ -472,7 +472,7 @@ FileListPage::OnCommand(struct mpdclient &c, Command cmd)

void
screen_browser_paint_directory(const Window window, unsigned width,
bool selected, const char *name) noexcept
bool selected, std::string_view name) noexcept
{
row_color(window, Style::DIRECTORY, selected);

Expand Down Expand Up @@ -502,7 +502,7 @@ FileListPage::PaintListItem(const Window window, unsigned i,
const auto &entry = (*filelist)[i];
const struct mpd_entity *entity = entry.entity;
if (entity == nullptr) {
screen_browser_paint_directory(window, width, selected, "..");
screen_browser_paint_directory(window, width, selected, ".."sv);
return;
}

Expand All @@ -516,8 +516,7 @@ FileListPage::PaintListItem(const Window window, unsigned i,
case MPD_ENTITY_TYPE_DIRECTORY: {
const auto *directory = mpd_entity_get_directory(entity);
const char *name = GetUriFilename(mpd_directory_get_path(directory));
screen_browser_paint_directory(window, width, selected,
Utf8ToLocale(name).c_str());
screen_browser_paint_directory(window, width, selected, Utf8ToLocale{name});
break;
}

Expand All @@ -530,8 +529,7 @@ FileListPage::PaintListItem(const Window window, unsigned i,
case MPD_ENTITY_TYPE_PLAYLIST: {
const auto *playlist = mpd_entity_get_playlist(entity);
const char *name = GetUriFilename(mpd_playlist_get_path(playlist));
screen_browser_paint_playlist(window, width, selected,
Utf8ToLocale(name).c_str());
screen_browser_paint_playlist(window, width, selected, Utf8ToLocale{name});
break;
}

Expand Down
2 changes: 1 addition & 1 deletion src/FileListPage.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,4 @@ screen_browser_sync_highlights(FileList &, const MpdQueue &) noexcept

void
screen_browser_paint_directory(Window window, unsigned width,
bool selected, const char *name) noexcept;
bool selected, std::string_view name) noexcept;
2 changes: 1 addition & 1 deletion src/LibraryPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ MakePageTitle(std::span<char> buffer, std::string_view prefix,
return prefix;

return FmtTruncate(buffer, "{}: {}"sv, prefix,
Utf8ToLocale{ToString(filter)}.c_str());
(std::string_view)Utf8ToLocale{ToString(filter)});
}

class SongListPage final : public FileListPage {
Expand Down
37 changes: 31 additions & 6 deletions src/charset.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Utf8ToLocale {
#ifdef HAVE_ICONV
const std::string value;
#else
const char *const value;
const std::string_view value;
#endif

public:
Expand All @@ -44,18 +44,21 @@ public:
Utf8ToLocale &operator=(const Utf8ToLocale &) = delete;
#else
[[nodiscard]]
explicit Utf8ToLocale(const char *src) noexcept
explicit Utf8ToLocale(std::string_view src) noexcept
:value(src) {}
#endif

[[nodiscard]]
operator std::string_view() const noexcept {
return value;
}

#ifdef HAVE_ICONV
[[nodiscard]] [[gnu::pure]]
const char *c_str() const noexcept {
#ifdef HAVE_ICONV
return value.c_str();
#else
return value;
#endif
}
#endif
};

/**
Expand Down Expand Up @@ -98,10 +101,32 @@ public:

#ifdef HAVE_ICONV

using Utf8ToLocaleZ = Utf8ToLocale;
using LocaleToUtf8Z = LocaleToUtf8;

#else

/**
* Like #Utf8ToLocaleZ, but return a null-terminated string.
*/
class Utf8ToLocaleZ {
const char *const value;

public:
[[nodiscard]]
explicit constexpr Utf8ToLocaleZ(const char *src) noexcept
:value(src) {}

[[nodiscard]]
explicit constexpr Utf8ToLocaleZ(const std::string &src) noexcept
:Utf8ToLocaleZ(src.c_str()) {}

[[nodiscard]] [[gnu::pure]]
constexpr const char *c_str() const noexcept {
return value;
}
};

/**
* Like #LocaleToUtf8Z, but return a null-terminated string.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/screen_client.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ screen_database_update(struct mpdclient &c, const char *path)

if (path != nullptr && *path != 0) {
screen_status_printf(_("Database update of %s started"),
Utf8ToLocale(path).c_str());
Utf8ToLocaleZ{path}.c_str());
} else
screen_status_message(_("Database update started"));
}

0 comments on commit 9b78cc5

Please sign in to comment.