mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-22 02:19:44 +00:00
add util::format_count
This commit is contained in:
parent
d857e7326d
commit
d4deb34722
2 changed files with 44 additions and 24 deletions
|
|
@ -1,11 +1,11 @@
|
||||||
#include "stringutil.hpp"
|
#include "stringutil.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <charconv>
|
#include <charconv>
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <locale>
|
#include <locale>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
|
|
@ -511,22 +511,22 @@ int util::replaceAll(
|
||||||
return replace_all(str, from, to);
|
return replace_all(str, from, to);
|
||||||
}
|
}
|
||||||
|
|
||||||
double util::parse_double(const std::string& str) {
|
double util::parse_double(const std::string& str) {
|
||||||
const char* begin = str.data();
|
const char* begin = str.data();
|
||||||
const char* end = begin + str.size();
|
const char* end = begin + str.size();
|
||||||
while (begin != end && std::isspace(static_cast<unsigned char>(*begin))) {
|
while (begin != end && std::isspace(static_cast<unsigned char>(*begin))) {
|
||||||
begin++;
|
begin++;
|
||||||
}
|
}
|
||||||
if (begin != end && *begin == '+') {
|
if (begin != end && *begin == '+') {
|
||||||
begin++;
|
begin++;
|
||||||
}
|
}
|
||||||
double d = 0.0;
|
double d = 0.0;
|
||||||
const auto result = std::from_chars(begin, end, d);
|
const auto result = std::from_chars(begin, end, d);
|
||||||
if (result.ec != std::errc()) {
|
if (result.ec != std::errc()) {
|
||||||
throw std::runtime_error("invalid number format");
|
throw std::runtime_error("invalid number format");
|
||||||
}
|
}
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
double util::parse_double(const std::string& str, size_t offset, size_t len) {
|
double util::parse_double(const std::string& str, size_t offset, size_t len) {
|
||||||
return parse_double(str.substr(offset, len));
|
return parse_double(str.substr(offset, len));
|
||||||
|
|
@ -667,6 +667,25 @@ std::string util::format_data_size(size_t size) {
|
||||||
postfixes[group];
|
postfixes[group];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string util::format_count(size_t count) {
|
||||||
|
if (count < 1000) {
|
||||||
|
return std::to_string(count);
|
||||||
|
}
|
||||||
|
const std::string postfixes[] {
|
||||||
|
"", " K", " M", " B"
|
||||||
|
};
|
||||||
|
int group = 0;
|
||||||
|
size_t remainder = 0;
|
||||||
|
while (count >= 1000) {
|
||||||
|
group++;
|
||||||
|
remainder = count % 1000;
|
||||||
|
count /= 1000;
|
||||||
|
}
|
||||||
|
return std::to_string(count) + "." +
|
||||||
|
std::to_string(static_cast<int>(round(remainder / 1000.0f))) +
|
||||||
|
postfixes[group];
|
||||||
|
}
|
||||||
|
|
||||||
std::pair<std::string, std::string> util::split_at(
|
std::pair<std::string, std::string> util::split_at(
|
||||||
std::string_view view, char c
|
std::string_view view, char c
|
||||||
) {
|
) {
|
||||||
|
|
|
||||||
|
|
@ -118,6 +118,7 @@ namespace util {
|
||||||
std::vector<std::wstring> split(const std::wstring& str, char delimiter);
|
std::vector<std::wstring> split(const std::wstring& str, char delimiter);
|
||||||
|
|
||||||
std::string format_data_size(size_t size);
|
std::string format_data_size(size_t size);
|
||||||
|
std::string format_count(size_t size);
|
||||||
|
|
||||||
std::pair<std::string, std::string> split_at(std::string_view view, char c);
|
std::pair<std::string, std::string> split_at(std::string_view view, char c);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue