From 9b78cc513d9e337d705766d67a31438318a96862 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 2 Sep 2024 10:20:07 +0200 Subject: [PATCH] charset: change Utf8ToLocale to produce std::string_view --- src/FileBrowserPage.cxx | 6 +++--- src/FileListPage.cxx | 14 ++++++-------- src/FileListPage.hxx | 2 +- src/LibraryPage.cxx | 2 +- src/charset.hxx | 37 +++++++++++++++++++++++++++++++------ src/screen_client.cxx | 2 +- 6 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/FileBrowserPage.cxx b/src/FileBrowserPage.cxx index 09f8a007..8eb2599a 100644 --- a/src/FileBrowserPage.cxx +++ b/src/FileBrowserPage.cxx @@ -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); } @@ -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 */ @@ -288,7 +288,7 @@ FileBrowserPage::GetTitle(std::span buffer) const noexcept return FmtTruncate(buffer, "{}: {}", /* translators: caption of the browser screen */ - _("Browse"), Utf8ToLocale(path).c_str()); + _("Browse"), (std::string_view)Utf8ToLocale{path}); } void diff --git a/src/FileListPage.cxx b/src/FileListPage.cxx index 0f435190..c2318536 100644 --- a/src/FileListPage.cxx +++ b/src/FileListPage.cxx @@ -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 @@ -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; } @@ -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); @@ -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; } @@ -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; } @@ -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; } diff --git a/src/FileListPage.hxx b/src/FileListPage.hxx index 8b6d24ce..341ac776 100644 --- a/src/FileListPage.hxx +++ b/src/FileListPage.hxx @@ -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; diff --git a/src/LibraryPage.cxx b/src/LibraryPage.cxx index 4b6624bb..5f90315a 100644 --- a/src/LibraryPage.cxx +++ b/src/LibraryPage.cxx @@ -47,7 +47,7 @@ MakePageTitle(std::span 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 { diff --git a/src/charset.hxx b/src/charset.hxx index 41d8b48b..c6e40195 100644 --- a/src/charset.hxx +++ b/src/charset.hxx @@ -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: @@ -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 }; /** @@ -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. */ diff --git a/src/screen_client.cxx b/src/screen_client.cxx index 46d58cfd..35745a0c 100644 --- a/src/screen_client.cxx +++ b/src/screen_client.cxx @@ -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")); }