summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/memory_util.cpp3
-rw-r--r--src/common/string_util.cpp70
-rw-r--r--src/common/string_util.h14
-rw-r--r--src/common/timer.cpp13
4 files changed, 9 insertions, 91 deletions
diff --git a/src/common/memory_util.cpp b/src/common/memory_util.cpp
index 79b7215d3..4d1ec8fb9 100644
--- a/src/common/memory_util.cpp
+++ b/src/common/memory_util.cpp
@@ -167,8 +167,7 @@ std::string MemUsage() {
167 return "MemUsage Error"; 167 return "MemUsage Error";
168 168
169 if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc))) 169 if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc)))
170 Ret = Common::StringFromFormat( 170 Ret = fmt::format("{} K", Common::ThousandSeparate(pmc.WorkingSetSize / 1024, 7));
171 "%s K", Common::ThousandSeparate(pmc.WorkingSetSize / 1024, 7).c_str());
172 171
173 CloseHandle(hProcess); 172 CloseHandle(hProcess);
174 return Ret; 173 return Ret;
diff --git a/src/common/string_util.cpp b/src/common/string_util.cpp
index 96c52e3ba..1d952874d 100644
--- a/src/common/string_util.cpp
+++ b/src/common/string_util.cpp
@@ -46,76 +46,6 @@ bool AsciiToHex(const char* _szValue, u32& result) {
46 return true; 46 return true;
47} 47}
48 48
49bool CharArrayFromFormatV(char* out, int outsize, const char* format, va_list args) {
50 int writtenCount;
51
52#ifdef _MSC_VER
53 // You would think *printf are simple, right? Iterate on each character,
54 // if it's a format specifier handle it properly, etc.
55 //
56 // Nooooo. Not according to the C standard.
57 //
58 // According to the C99 standard (7.19.6.1 "The fprintf function")
59 // The format shall be a multibyte character sequence
60 //
61 // Because some character encodings might have '%' signs in the middle of
62 // a multibyte sequence (SJIS for example only specifies that the first
63 // byte of a 2 byte sequence is "high", the second byte can be anything),
64 // printf functions have to decode the multibyte sequences and try their
65 // best to not screw up.
66 //
67 // Unfortunately, on Windows, the locale for most languages is not UTF-8
68 // as we would need. Notably, for zh_TW, Windows chooses EUC-CN as the
69 // locale, and completely fails when trying to decode UTF-8 as EUC-CN.
70 //
71 // On the other hand, the fix is simple: because we use UTF-8, no such
72 // multibyte handling is required as we can simply assume that no '%' char
73 // will be present in the middle of a multibyte sequence.
74 //
75 // This is why we lookup an ANSI (cp1252) locale here and use _vsnprintf_l.
76 static locale_t c_locale = nullptr;
77 if (!c_locale)
78 c_locale = _create_locale(LC_ALL, ".1252");
79 writtenCount = _vsnprintf_l(out, outsize, format, c_locale, args);
80#else
81 writtenCount = vsnprintf(out, outsize, format, args);
82#endif
83
84 if (writtenCount > 0 && writtenCount < outsize) {
85 out[writtenCount] = '\0';
86 return true;
87 } else {
88 out[outsize - 1] = '\0';
89 return false;
90 }
91}
92
93std::string StringFromFormat(const char* format, ...) {
94 va_list args;
95 char* buf = nullptr;
96#ifdef _WIN32
97 int required = 0;
98
99 va_start(args, format);
100 required = _vscprintf(format, args);
101 buf = new char[required + 1];
102 CharArrayFromFormatV(buf, required + 1, format, args);
103 va_end(args);
104
105 std::string temp = buf;
106 delete[] buf;
107#else
108 va_start(args, format);
109 if (vasprintf(&buf, format, args) < 0)
110 NGLOG_ERROR(Common, "Unable to allocate memory for string");
111 va_end(args);
112
113 std::string temp = buf;
114 free(buf);
115#endif
116 return temp;
117}
118
119// For Debugging. Read out an u8 array. 49// For Debugging. Read out an u8 array.
120std::string ArrayToString(const u8* data, size_t size, int line_len, bool spaces) { 50std::string ArrayToString(const u8* data, size_t size, int line_len, bool spaces) {
121 std::ostringstream oss; 51 std::ostringstream oss;
diff --git a/src/common/string_util.h b/src/common/string_util.h
index ec0c31a24..65e4ea5d3 100644
--- a/src/common/string_util.h
+++ b/src/common/string_util.h
@@ -4,7 +4,6 @@
4 4
5#pragma once 5#pragma once
6 6
7#include <cstdarg>
8#include <cstddef> 7#include <cstddef>
9#include <iomanip> 8#include <iomanip>
10#include <sstream> 9#include <sstream>
@@ -20,19 +19,6 @@ std::string ToLower(std::string str);
20/// Make a string uppercase 19/// Make a string uppercase
21std::string ToUpper(std::string str); 20std::string ToUpper(std::string str);
22 21
23std::string StringFromFormat(const char* format, ...);
24// Cheap!
25bool CharArrayFromFormatV(char* out, int outsize, const char* format, va_list args);
26
27template <size_t Count>
28inline void CharArrayFromFormat(char (&out)[Count], const char* format, ...) {
29 va_list args;
30 va_start(args, format);
31 CharArrayFromFormatV(out, Count, format, args);
32 va_end(args);
33}
34
35// Good
36std::string ArrayToString(const u8* data, size_t size, int line_len = 20, bool spaces = true); 22std::string ArrayToString(const u8* data, size_t size, int line_len = 20, bool spaces = true);
37 23
38std::string StripSpaces(const std::string& s); 24std::string StripSpaces(const std::string& s);
diff --git a/src/common/timer.cpp b/src/common/timer.cpp
index c9803109e..f0c5b1a43 100644
--- a/src/common/timer.cpp
+++ b/src/common/timer.cpp
@@ -2,7 +2,10 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <time.h> 5#include <ctime>
6
7#include <fmt/format.h>
8
6#ifdef _WIN32 9#ifdef _WIN32
7#include <windows.h> 10#include <windows.h>
8// windows.h needs to be included before other windows headers 11// windows.h needs to be included before other windows headers
@@ -104,8 +107,8 @@ std::string Timer::GetTimeElapsedFormatted() const {
104 // Hours 107 // Hours
105 u32 Hours = Minutes / 60; 108 u32 Hours = Minutes / 60;
106 109
107 std::string TmpStr = StringFromFormat("%02i:%02i:%02i:%03i", Hours, Minutes % 60, Seconds % 60, 110 std::string TmpStr = fmt::format("{:02}:{:02}:{:02}:{:03}", Hours, Minutes % 60, Seconds % 60,
108 Milliseconds % 1000); 111 Milliseconds % 1000);
109 return TmpStr; 112 return TmpStr;
110} 113}
111 114
@@ -165,11 +168,11 @@ std::string Timer::GetTimeFormatted() {
165#ifdef _WIN32 168#ifdef _WIN32
166 struct timeb tp; 169 struct timeb tp;
167 (void)::ftime(&tp); 170 (void)::ftime(&tp);
168 return StringFromFormat("%s:%03i", tmp, tp.millitm); 171 return fmt::format("{}:{:03}", tmp, tp.millitm);
169#else 172#else
170 struct timeval t; 173 struct timeval t;
171 (void)gettimeofday(&t, nullptr); 174 (void)gettimeofday(&t, nullptr);
172 return StringFromFormat("%s:%03d", tmp, (int)(t.tv_usec / 1000)); 175 return fmt::format("{}:{:03}", tmp, static_cast<int>(t.tv_usec / 1000));
173#endif 176#endif
174} 177}
175 178