From 97d5c7c45c5ba106f031affb3c061cfa5a603307 Mon Sep 17 00:00:00 2001 From: wirbel Date: Sun, 8 Aug 2021 13:35:51 +0200 Subject: [PATCH] fix typo Precisision -> Precision and unify string prints --- Helpers.cpp | 30 +++++++++++------------------- Helpers.h | 2 +- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/Helpers.cpp b/Helpers.cpp index 73a12fc..e1f9f61 100644 --- a/Helpers.cpp +++ b/Helpers.cpp @@ -49,34 +49,26 @@ std::string BackFill(std::string s, size_t n) { return result; } +template std::string ToString(std::string format, T aVar) { + char buf[256]; + snprintf(buf, 256-1, format.c_str(), aVar); + return buf; +}; + std::string IntToStr(int64_t n, size_t Zeros) { - std::string result = std::to_string((long long) n); - if (Zeros > result.size()) - return std::string(Zeros - result.size(), '0') + result; - return result; + return ToString("%0" + std::to_string(Zeros) + "lld", (long long) n); } std::string IntToHex(uint64_t n, size_t Digits) { - std::stringstream ss; - ss << std::uppercase << std::hex << (unsigned long long) n; - - if (Digits > ss.str().size()) - return "0x" + std::string(Digits - ss.str().size(), '0') + ss.str(); - return "0x" + ss.str(); + return ToString("0x%.0" + std::to_string(Digits) + "llX", (long long) n); } -std::string FloatToStr(double d, int Precisision) { - char buf[257]; - std::string format; - format = "%." + std::to_string(Precisision) + "f"; - snprintf(buf, 256, format.c_str(), d); - return buf; +std::string FloatToStr(double d, int Precision) { + return ToString("%." + std::to_string(Precision) + "f", d); } std::string ExpToStr(double d) { - char buf[256]; - sprintf(buf, "%e", d); - return std::string(buf); + return ToString("%.3e", d); } std::string VdrSource(std::string s) { diff --git a/Helpers.h b/Helpers.h index 0e1d70c..5e609b1 100644 --- a/Helpers.h +++ b/Helpers.h @@ -34,7 +34,7 @@ std::string FrontFill(std::string s, size_t n); std::string BackFill(std::string s, size_t n); std::string IntToStr(int64_t n, size_t Zeros = 0); std::string IntToHex(uint64_t n, size_t Digits = 0); -std::string FloatToStr(double d, int Precisision = 1); +std::string FloatToStr(double d, int Precision = 1); std::string ExpToStr(double d); std::string VdrSource(std::string s); void Sleep(size_t msec);