kima2/src/core/utils.cpp

70 lines
1.9 KiB
C++

#include "utils.h"
#include <algorithm>
#include <clocale>
#include <fmt/format.h>
#include <iomanip>
#include <numeric>
#include <iostream>
using namespace fmt;
std::string formatCentAsEuroString(const int cent, int width)
{
/*std::stringstream currStream;
try {
std::locale myLocale("de_DE.utf8");
currStream.imbue(myLocale);
std::cout << ">>> " << fmt::format(myLocale, "{:6.2Lf}", 1.12345) << '\n';
currStream << std::right << std::setw(width) << std::showbase
<< std::put_money(cent, false);
} catch (std::runtime_error &err) {
currStream << std::fixed << std::setw(width >= 4 ? width - 4 : width)
<< std::setprecision(2) << cent / 100.0L << " €";
}
return currStream.str();*/
#if defined(_WIN64) || defined(_WIN32)
std::locale myLocale;
#else
std::locale myLocale{"de_DE.utf8"};
#endif
return fmt::format(myLocale, "{:{}.2Lf} €", cent / 100.0L, width);
}
std::string &ltrim(std::string &str, const std::string &chars)
{
str.erase(0, str.find_first_not_of(chars));
return str;
}
std::string &rtrim(std::string &str, const std::string &chars)
{
str.erase(str.find_last_not_of(chars) + 1);
return str;
}
std::string &trim(std::string &str, const std::string &chars)
{
return ltrim(rtrim(str, chars), chars);
}
bool case_insensitive_match(std::string s1, std::string s2)
{
// convert s1 and s2 into lower case strings
transform(s1.begin(), s1.end(), s1.begin(), ::tolower);
transform(s2.begin(), s2.end(), s2.begin(), ::tolower);
if (s1.compare(s2) == 0)
return true; // The strings are same
return false; // not matched
}
bool isNumber(const std::string &str)
{
return !str.empty() && std::find_if(str.begin(), str.end(), [](unsigned char c) {
return !std::isdigit(c);
}) == str.end();
}