Skip to content

Commit

Permalink
time_format: pass buffer as std::span
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Sep 1, 2024
1 parent 4d45c60 commit 3ddde1c
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 53 deletions.
3 changes: 1 addition & 2 deletions src/FileListPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/QueuePage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 7 additions & 14 deletions src/SongPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -305,27 +305,23 @@ 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;

char buffer[64];

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);
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/SongRowPaint.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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(' ');
Expand Down
9 changes: 2 additions & 7 deletions src/StatusBar.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
3 changes: 1 addition & 2 deletions src/strfsong.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
31 changes: 14 additions & 17 deletions src/time_format.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,51 @@
#include <stdio.h>

void
format_duration_short(char *buffer, size_t length, unsigned duration) noexcept
format_duration_short(std::span<char> 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<char> 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);
}
11 changes: 4 additions & 7 deletions src/time_format.hxx
Original file line number Diff line number Diff line change
@@ -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 <stddef.h>
#include <span>

void
format_duration_short(char *buffer, size_t length, unsigned duration) noexcept;
format_duration_short(std::span<char> buffer, unsigned duration) noexcept;

void
format_duration_long(char *buffer, size_t length, unsigned long duration) noexcept;

#endif
format_duration_long(std::span<char> buffer, unsigned long duration) noexcept;

0 comments on commit 3ddde1c

Please sign in to comment.