aboutsummaryrefslogtreecommitdiff
path: root/src/common/Utilities
diff options
context:
space:
mode:
authorShauren <shauren.trinity@gmail.com>2024-10-16 18:58:45 +0200
committerShauren <shauren.trinity@gmail.com>2024-10-16 18:58:45 +0200
commit1a41281e37efdc4be602066dc3fdba12b24d1d0f (patch)
tree1cca422ce96b71de18b686527bd1e0383b5166a9 /src/common/Utilities
parentc3b06d7797af2d13499680515ca94fc883cd343a (diff)
Core/Misc: Replace std::ostringstream based formatting with Trinity::StringFormat where possible in common and database projects
Diffstat (limited to 'src/common/Utilities')
-rw-r--r--src/common/Utilities/Util.cpp105
1 files changed, 33 insertions, 72 deletions
diff --git a/src/common/Utilities/Util.cpp b/src/common/Utilities/Util.cpp
index 9730c169832..ddf649ff663 100644
--- a/src/common/Utilities/Util.cpp
+++ b/src/common/Utilities/Util.cpp
@@ -24,8 +24,6 @@
#include <boost/core/demangle.hpp>
#include <utf8.h>
#include <algorithm>
-#include <iomanip>
-#include <sstream>
#include <string>
#include <cctype>
#include <cstdarg>
@@ -131,86 +129,49 @@ std::string secsToTimeString(uint64 timeInSecs, TimeFormat timeFormat, bool hour
return Trinity::StringFormat("0:{:02}", secs);
}
- std::ostringstream ss;
- if (days)
+ std::string result;
+ if (timeFormat == TimeFormat::ShortText)
{
- ss << days;
- switch (timeFormat)
+ std::back_insert_iterator<std::string> itr = std::back_inserter(result);
+ if (days)
+ Trinity::StringFormatTo(itr, "{}d", days);
+ if (hours || hoursOnly)
+ Trinity::StringFormatTo(itr, "{}h", hours);
+ if (!hoursOnly)
{
- case TimeFormat::ShortText:
- ss << "d";
- break;
- case TimeFormat::FullText:
- if (days == 1)
- ss << " Day ";
- else
- ss << " Days ";
- break;
- default:
- return "<Unknown time format>";
+ if (minutes)
+ Trinity::StringFormatTo(itr, "{}m", minutes);
+ if (secs || result.empty())
+ Trinity::StringFormatTo(itr, "{}s", secs);
}
}
-
- if (hours || hoursOnly)
+ else if (timeFormat == TimeFormat::FullText)
{
- ss << hours;
- switch (timeFormat)
+ auto formatTimeField = [](std::string& result, uint64 value, std::string_view label)
{
- case TimeFormat::ShortText:
- ss << "h";
- break;
- case TimeFormat::FullText:
- if (hours <= 1)
- ss << " Hour ";
- else
- ss << " Hours ";
- break;
- default:
- return "<Unknown time format>";
- }
- }
- if (!hoursOnly)
- {
- if (minutes)
- {
- ss << minutes;
- switch (timeFormat)
- {
- case TimeFormat::ShortText:
- ss << "m";
- break;
- case TimeFormat::FullText:
- if (minutes == 1)
- ss << " Minute ";
- else
- ss << " Minutes ";
- break;
- default:
- return "<Unknown time format>";
- }
- }
-
- if (secs || (!days && !hours && !minutes))
+ if (!result.empty())
+ result.append(1, ' ');
+ Trinity::StringFormatTo(std::back_inserter(result), "{} {}", value, label);
+ if (value != 1)
+ result.append(1, 's');
+ };
+ if (days)
+ formatTimeField(result, days, "Day");
+ if (hours || hoursOnly)
+ formatTimeField(result, hours, "Hour");
+ if (!hoursOnly)
{
- ss << secs;
- switch (timeFormat)
- {
- case TimeFormat::ShortText:
- ss << "s";
- break;
- case TimeFormat::FullText:
- if (secs <= 1)
- ss << " Second.";
- else
- ss << " Seconds.";
- break;
- default:
- return "<Unknown time format>";
- }
+ if (minutes)
+ formatTimeField(result, minutes, "Minute");
+ if (secs || result.empty())
+ formatTimeField(result, secs, "Second");
}
+ result.append(1, '.');
}
+ else
+ result = "<Unknown time format>";
- return ss.str();
+ return result;
}
Optional<int64> MoneyStringToMoney(std::string const& moneyString)