reapack

Package manager for REAPER
Log | Files | Refs | Submodules | README | LICENSE

commit 8ab6aecb76b7c5896f78a4bc3440014655bcaa0b
parent cc18e799887b2cd2137a8f9c520c1076f959fe01
Author: cfillion <cfillion@users.noreply.github.com>
Date:   Thu, 27 Sep 2018 19:23:47 -0400

refactor thousand separator number formatting

Diffstat:
Msrc/browser.cpp | 15++++-----------
Msrc/progress.cpp | 9+++++----
Msrc/receipt.cpp | 8+-------
Msrc/string.cpp | 2+-
Msrc/string.hpp | 13++++++++++++-
Mtest/string.cpp | 4++++
6 files changed, 27 insertions(+), 24 deletions(-)

diff --git a/src/browser.cpp b/src/browser.cpp @@ -298,17 +298,10 @@ void Browser::fillSelectionMenu(Menu &menu) void Browser::updateDisplayLabel() { - ostringstream label; - String::imbueStream(label); - - label << m_list->visibleRowCount() << '/' << m_entries.size() << " package"; - - if(m_entries.size() != 1) - label << 's'; - - label << "..."; - - Win32::setWindowText(m_displayBtn, label.str().c_str()); + Win32::setWindowText(m_displayBtn, String::format("%s/%s package%s...", + String::number(m_list->visibleRowCount()).c_str(), + String::number(m_entries.size()).c_str(), m_entries.size() == 1 ? "" : "s" + ).c_str()); } void Browser::displayButton() diff --git a/src/progress.cpp b/src/progress.cpp @@ -88,12 +88,13 @@ void Progress::addTask(ThreadTask *task) void Progress::updateProgress() { - ostringstream position; - String::imbueStream(position); - position << min(m_done + 1, m_total) << " of " << m_total; + const string &position = String::format("%s of %s", + String::number(min(m_done + 1, m_total)).c_str(), + String::number(m_total).c_str() + ); char label[1024]; - snprintf(label, sizeof(label), m_current.c_str(), position.str().c_str()); + snprintf(label, sizeof(label), m_current.c_str(), position.c_str()); Win32::setWindowText(m_label, label); diff --git a/src/receipt.cpp b/src/receipt.cpp @@ -88,13 +88,7 @@ ReceiptPage Receipt::errorPage() const void ReceiptPage::setTitle(const char *title) { - ostringstream stream; - - // enable number formatting (ie. "1,234" instead of "1234") - String::imbueStream(stream); - - stream << title << " (" << m_size << ')'; - m_title = stream.str(); + m_title = String::format("%s (%s)", title, String::number(m_size).c_str()); } InstallTicket::InstallTicket(const Version *ver, const Registry::Entry &previous) diff --git a/src/string.cpp b/src/string.cpp @@ -65,7 +65,7 @@ string String::indent(const string &text) return output; } -void String::imbueStream(ostream &stream) +void String::ImplDetail::imbueStream(ostream &stream) { class NumPunct : public std::numpunct<char> { diff --git a/src/string.hpp b/src/string.hpp @@ -18,9 +18,14 @@ #ifndef REAPACK_STRING_HPP #define REAPACK_STRING_HPP +#include <sstream> #include <string> namespace String { + namespace ImplDetail { + void imbueStream(std::ostream &); + }; + #ifdef __GNUC__ __attribute__((format(printf, 1, 2))) #endif @@ -28,7 +33,13 @@ namespace String { std::string indent(const std::string &); - void imbueStream(std::ostream &); + template<typename T, typename = std::enable_if_t<std::is_arithmetic<T>::value>> + std::string number(const T v) { + std::ostringstream stream; + ImplDetail::imbueStream(stream); + stream << v; + return stream.str(); + }; } #endif diff --git a/test/string.cpp b/test/string.cpp @@ -23,3 +23,7 @@ TEST_CASE("indent string", M) { REQUIRE(actual == " line1\r\n line2"); } + +TEST_CASE("pretty-print numbers", M) { + REQUIRE(String::number(42'000'000) == "42,000,000"); +}