From 3ddde1cbe5f9c3c89699b6e46a47ee7c8dbfa551 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 1 Sep 2024 06:58:16 +0200 Subject: [PATCH] time_format: pass buffer as std::span --- src/FileListPage.cxx | 3 +-- src/QueuePage.cxx | 3 +-- src/SongPage.cxx | 21 +++++++-------------- src/SongRowPaint.cxx | 3 +-- src/StatusBar.cxx | 9 ++------- src/strfsong.cxx | 3 +-- src/time_format.cxx | 31 ++++++++++++++----------------- src/time_format.hxx | 11 ++++------- 8 files changed, 31 insertions(+), 53 deletions(-) diff --git a/src/FileListPage.cxx b/src/FileListPage.cxx index 83097d4e..785aa479 100644 --- a/src/FileListPage.cxx +++ b/src/FileListPage.cxx @@ -571,8 +571,7 @@ FileListPage::PaintStatusBarOverride(const Window window) const noexcept } char duration_string[32]; - format_duration_short(duration_string, sizeof(duration_string), - duration); + format_duration_short(duration_string, duration); const unsigned duration_width = strlen(duration_string); SelectStyle(window, Style::STATUS_TIME); diff --git a/src/QueuePage.cxx b/src/QueuePage.cxx index 4b9f303a..a7f4ed37 100644 --- a/src/QueuePage.cxx +++ b/src/QueuePage.cxx @@ -437,8 +437,7 @@ QueuePage::PaintStatusBarOverride(const Window window) const noexcept } char duration_string[32]; - format_duration_short(duration_string, sizeof(duration_string), - duration); + format_duration_short(duration_string, duration); const unsigned duration_width = strlen(duration_string); SelectStyle(window, Style::STATUS_TIME); diff --git a/src/SongPage.cxx b/src/SongPage.cxx index 3c0b700f..6f570a50 100644 --- a/src/SongPage.cxx +++ b/src/SongPage.cxx @@ -305,8 +305,7 @@ SongPage::AddSong(const struct mpd_song *song) noexcept /* create time string and add it */ if (mpd_song_get_duration(song) > 0) { char length[16]; - format_duration_short(length, sizeof(length), - mpd_song_get_duration(song)); + format_duration_short(length, mpd_song_get_duration(song)); const char *value = length; @@ -314,18 +313,15 @@ SongPage::AddSong(const struct mpd_song *song) noexcept if (mpd_song_get_end(song) > 0) { char start[16], end[16]; - format_duration_short(start, sizeof(start), - mpd_song_get_start(song)); - format_duration_short(end, sizeof(end), - mpd_song_get_end(song)); + format_duration_short(start, mpd_song_get_start(song)); + format_duration_short(end, mpd_song_get_end(song)); snprintf(buffer, sizeof(buffer), "%s [%s-%s]\n", length, start, end); value = buffer; } else if (mpd_song_get_start(song) > 0) { char start[16]; - format_duration_short(start, sizeof(start), - mpd_song_get_start(song)); + format_duration_short(start, mpd_song_get_start(song)); snprintf(buffer, sizeof(buffer), "%s [%s-]\n", length, start); @@ -374,16 +370,13 @@ SongPage::AddStats(struct mpd_connection *connection) noexcept mpd_stats_get_number_of_songs(mpd_stats)); AppendStatsLine(STATS_SONGS, buf); - format_duration_long(buf, sizeof(buf), - mpd_stats_get_db_play_time(mpd_stats)); + format_duration_long(buf, mpd_stats_get_db_play_time(mpd_stats)); AppendStatsLine(STATS_DBPLAYTIME, buf); - format_duration_long(buf, sizeof(buf), - mpd_stats_get_play_time(mpd_stats)); + format_duration_long(buf, mpd_stats_get_play_time(mpd_stats)); AppendStatsLine(STATS_PLAYTIME, buf); - format_duration_long(buf, sizeof(buf), - mpd_stats_get_uptime(mpd_stats)); + format_duration_long(buf, mpd_stats_get_uptime(mpd_stats)); AppendStatsLine(STATS_UPTIME, buf); const time_t t = mpd_stats_get_db_update_time(mpd_stats); diff --git a/src/SongRowPaint.cxx b/src/SongRowPaint.cxx index ef4ed57f..0da2ff01 100644 --- a/src/SongRowPaint.cxx +++ b/src/SongRowPaint.cxx @@ -28,8 +28,7 @@ paint_song_row(const Window window, [[maybe_unused]] int y, unsigned width, #ifndef NCMPC_MINI if (options.second_column && mpd_song_get_duration(song) > 0) { char duration[32]; - format_duration_short(duration, sizeof(duration), - mpd_song_get_duration(song)); + format_duration_short(duration, mpd_song_get_duration(song)); width -= strlen(duration) + 1; window.MoveCursor({(int)width, y}); window.Char(' '); diff --git a/src/StatusBar.cxx b/src/StatusBar.cxx index a8c70681..dd9b13e9 100644 --- a/src/StatusBar.cxx +++ b/src/StatusBar.cxx @@ -106,19 +106,14 @@ FormatCurrentSongTime(char *buffer, size_t size, } /* write out the time */ - format_duration_short(elapsed_string, - sizeof(elapsed_string), - elapsed_time); + format_duration_short(elapsed_string, elapsed_time); if (total_time == 0) { snprintf(buffer, size, " [%s]", elapsed_string); return; } - format_duration_short(duration_string, - sizeof(duration_string), - total_time); - + format_duration_short(duration_string, total_time); snprintf(buffer, size, " [%s/%s]", elapsed_string, duration_string); } diff --git a/src/strfsong.cxx b/src/strfsong.cxx index 5f47f543..aaec4ab9 100644 --- a/src/strfsong.cxx +++ b/src/strfsong.cxx @@ -214,8 +214,7 @@ _strfsong(char *const s0, char *const end, unsigned duration = mpd_song_get_duration(song); if (duration > 0) { - format_duration_short(buffer, sizeof(buffer), - duration); + format_duration_short(buffer, duration); value = buffer; } } diff --git a/src/time_format.cxx b/src/time_format.cxx index 0856011b..16e036bf 100644 --- a/src/time_format.cxx +++ b/src/time_format.cxx @@ -7,54 +7,51 @@ #include void -format_duration_short(char *buffer, size_t length, unsigned duration) noexcept +format_duration_short(std::span buffer, unsigned duration) noexcept { if (duration < 3600) - snprintf(buffer, length, + snprintf(buffer.data(), buffer.size(), "%i:%02i", duration / 60, duration % 60); else - snprintf(buffer, length, + snprintf(buffer.data(), buffer.size(), "%i:%02i:%02i", duration / 3600, (duration % 3600) / 60, duration % 60); } void -format_duration_long(char *p, size_t length, unsigned long duration) noexcept +format_duration_long(std::span buffer, unsigned long duration) noexcept { unsigned bytes_written = 0; if (duration / 31536000 > 0) { if (duration / 31536000 == 1) - bytes_written = snprintf(p, length, "%d %s, ", 1, _("year")); + bytes_written = snprintf(buffer.data(), buffer.size(), "%d %s, ", 1, _("year")); else - bytes_written = snprintf(p, length, "%lu %s, ", duration / 31536000, _("years")); + bytes_written = snprintf(buffer.data(), buffer.size(), "%lu %s, ", duration / 31536000, _("years")); duration %= 31536000; - length -= bytes_written; - p += bytes_written; + buffer = buffer.subspan(bytes_written); } if (duration / 604800 > 0) { if (duration / 604800 == 1) - bytes_written = snprintf(p, length, "%d %s, ", + bytes_written = snprintf(buffer.data(), buffer.size(), "%d %s, ", 1, _("week")); else - bytes_written = snprintf(p, length, "%lu %s, ", + bytes_written = snprintf(buffer.data(), buffer.size(), "%lu %s, ", duration / 604800, _("weeks")); duration %= 604800; - length -= bytes_written; - p += bytes_written; + buffer = buffer.subspan(bytes_written); } if (duration / 86400 > 0) { if (duration / 86400 == 1) - bytes_written = snprintf(p, length, "%d %s, ", + bytes_written = snprintf(buffer.data(), buffer.size(), "%d %s, ", 1, _("day")); else - bytes_written = snprintf(p, length, "%lu %s, ", + bytes_written = snprintf(buffer.data(), buffer.size(), "%lu %s, ", duration / 86400, _("days")); duration %= 86400; - length -= bytes_written; - p += bytes_written; + buffer = buffer.subspan(bytes_written); } - snprintf(p, length, "%02lu:%02lu:%02lu", duration / 3600, + snprintf(buffer.data(), buffer.size(), "%02lu:%02lu:%02lu", duration / 3600, duration % 3600 / 60, duration % 3600 % 60); } diff --git a/src/time_format.hxx b/src/time_format.hxx index d8e027aa..93489757 100644 --- a/src/time_format.hxx +++ b/src/time_format.hxx @@ -1,15 +1,12 @@ // SPDX-License-Identifier: GPL-2.0-or-later // Copyright The Music Player Daemon Project -#ifndef TIME_FORMAT_H -#define TIME_FORMAT_H +#pragma once -#include +#include void -format_duration_short(char *buffer, size_t length, unsigned duration) noexcept; +format_duration_short(std::span buffer, unsigned duration) noexcept; void -format_duration_long(char *buffer, size_t length, unsigned long duration) noexcept; - -#endif +format_duration_long(std::span buffer, unsigned long duration) noexcept;