diff options
Diffstat (limited to 'src/common/string_util.cpp')
| -rw-r--r-- | src/common/string_util.cpp | 66 |
1 files changed, 11 insertions, 55 deletions
diff --git a/src/common/string_util.cpp b/src/common/string_util.cpp index 731d1db34..959f278aa 100644 --- a/src/common/string_util.cpp +++ b/src/common/string_util.cpp | |||
| @@ -4,11 +4,10 @@ | |||
| 4 | 4 | ||
| 5 | #include <algorithm> | 5 | #include <algorithm> |
| 6 | #include <cctype> | 6 | #include <cctype> |
| 7 | #include <cerrno> | ||
| 8 | #include <codecvt> | 7 | #include <codecvt> |
| 9 | #include <cstdio> | ||
| 10 | #include <cstdlib> | 8 | #include <cstdlib> |
| 11 | #include <cstring> | 9 | #include <locale> |
| 10 | #include <sstream> | ||
| 12 | #include "common/common_paths.h" | 11 | #include "common/common_paths.h" |
| 13 | #include "common/logging/log.h" | 12 | #include "common/logging/log.h" |
| 14 | #include "common/string_util.h" | 13 | #include "common/string_util.h" |
| @@ -33,24 +32,6 @@ std::string ToUpper(std::string str) { | |||
| 33 | return str; | 32 | return str; |
| 34 | } | 33 | } |
| 35 | 34 | ||
| 36 | // For Debugging. Read out an u8 array. | ||
| 37 | std::string ArrayToString(const u8* data, std::size_t size, int line_len, bool spaces) { | ||
| 38 | std::ostringstream oss; | ||
| 39 | oss << std::setfill('0') << std::hex; | ||
| 40 | |||
| 41 | for (int line = 0; size; ++data, --size) { | ||
| 42 | oss << std::setw(2) << (int)*data; | ||
| 43 | |||
| 44 | if (line_len == ++line) { | ||
| 45 | oss << '\n'; | ||
| 46 | line = 0; | ||
| 47 | } else if (spaces) | ||
| 48 | oss << ' '; | ||
| 49 | } | ||
| 50 | |||
| 51 | return oss.str(); | ||
| 52 | } | ||
| 53 | |||
| 54 | std::string StringFromBuffer(const std::vector<u8>& data) { | 35 | std::string StringFromBuffer(const std::vector<u8>& data) { |
| 55 | return std::string(data.begin(), std::find(data.begin(), data.end(), '\0')); | 36 | return std::string(data.begin(), std::find(data.begin(), data.end(), '\0')); |
| 56 | } | 37 | } |
| @@ -75,40 +56,6 @@ std::string StripQuotes(const std::string& s) { | |||
| 75 | return s; | 56 | return s; |
| 76 | } | 57 | } |
| 77 | 58 | ||
| 78 | bool TryParse(const std::string& str, u32* const output) { | ||
| 79 | char* endptr = nullptr; | ||
| 80 | |||
| 81 | // Reset errno to a value other than ERANGE | ||
| 82 | errno = 0; | ||
| 83 | |||
| 84 | unsigned long value = strtoul(str.c_str(), &endptr, 0); | ||
| 85 | |||
| 86 | if (!endptr || *endptr) | ||
| 87 | return false; | ||
| 88 | |||
| 89 | if (errno == ERANGE) | ||
| 90 | return false; | ||
| 91 | |||
| 92 | #if ULONG_MAX > UINT_MAX | ||
| 93 | if (value >= 0x100000000ull && value <= 0xFFFFFFFF00000000ull) | ||
| 94 | return false; | ||
| 95 | #endif | ||
| 96 | |||
| 97 | *output = static_cast<u32>(value); | ||
| 98 | return true; | ||
| 99 | } | ||
| 100 | |||
| 101 | bool TryParse(const std::string& str, bool* const output) { | ||
| 102 | if ("1" == str || "true" == ToLower(str)) | ||
| 103 | *output = true; | ||
| 104 | else if ("0" == str || "false" == ToLower(str)) | ||
| 105 | *output = false; | ||
| 106 | else | ||
| 107 | return false; | ||
| 108 | |||
| 109 | return true; | ||
| 110 | } | ||
| 111 | |||
| 112 | std::string StringFromBool(bool value) { | 59 | std::string StringFromBool(bool value) { |
| 113 | return value ? "True" : "False"; | 60 | return value ? "True" : "False"; |
| 114 | } | 61 | } |
| @@ -267,6 +214,15 @@ std::string StringFromFixedZeroTerminatedBuffer(const char* buffer, std::size_t | |||
| 267 | return std::string(buffer, len); | 214 | return std::string(buffer, len); |
| 268 | } | 215 | } |
| 269 | 216 | ||
| 217 | std::u16string UTF16StringFromFixedZeroTerminatedBuffer(std::u16string_view buffer, | ||
| 218 | std::size_t max_len) { | ||
| 219 | std::size_t len = 0; | ||
| 220 | while (len < max_len && buffer[len] != '\0') | ||
| 221 | ++len; | ||
| 222 | |||
| 223 | return std::u16string(buffer.begin(), buffer.begin() + len); | ||
| 224 | } | ||
| 225 | |||
| 270 | const char* TrimSourcePath(const char* path, const char* root) { | 226 | const char* TrimSourcePath(const char* path, const char* root) { |
| 271 | const char* p = path; | 227 | const char* p = path; |
| 272 | 228 | ||