diff options
Diffstat (limited to 'src/common/string_util.h')
| -rw-r--r-- | src/common/string_util.h | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/src/common/string_util.h b/src/common/string_util.h new file mode 100644 index 000000000..fcbae4715 --- /dev/null +++ b/src/common/string_util.h | |||
| @@ -0,0 +1,111 @@ | |||
| 1 | // Copyright 2013 Dolphin Emulator Project | ||
| 2 | // Licensed under GPLv2 | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #ifndef _STRINGUTIL_H_ | ||
| 6 | #define _STRINGUTIL_H_ | ||
| 7 | |||
| 8 | #include <stdarg.h> | ||
| 9 | |||
| 10 | #include <vector> | ||
| 11 | #include <string> | ||
| 12 | #include <sstream> | ||
| 13 | #include <iomanip> | ||
| 14 | |||
| 15 | #include "common.h" | ||
| 16 | |||
| 17 | std::string StringFromFormat(const char* format, ...); | ||
| 18 | // Cheap! | ||
| 19 | bool CharArrayFromFormatV(char* out, int outsize, const char* format, va_list args); | ||
| 20 | |||
| 21 | template<size_t Count> | ||
| 22 | inline void CharArrayFromFormat(char (& out)[Count], const char* format, ...) | ||
| 23 | { | ||
| 24 | va_list args; | ||
| 25 | va_start(args, format); | ||
| 26 | CharArrayFromFormatV(out, Count, format, args); | ||
| 27 | va_end(args); | ||
| 28 | } | ||
| 29 | |||
| 30 | // Good | ||
| 31 | std::string ArrayToString(const u8 *data, u32 size, int line_len = 20, bool spaces = true); | ||
| 32 | |||
| 33 | std::string StripSpaces(const std::string &s); | ||
| 34 | std::string StripQuotes(const std::string &s); | ||
| 35 | |||
| 36 | // Thousand separator. Turns 12345678 into 12,345,678 | ||
| 37 | template <typename I> | ||
| 38 | std::string ThousandSeparate(I value, int spaces = 0) | ||
| 39 | { | ||
| 40 | std::ostringstream oss; | ||
| 41 | |||
| 42 | // std::locale("") seems to be broken on many platforms | ||
| 43 | #if defined _WIN32 || (defined __linux__ && !defined __clang__) | ||
| 44 | oss.imbue(std::locale("")); | ||
| 45 | #endif | ||
| 46 | oss << std::setw(spaces) << value; | ||
| 47 | |||
| 48 | return oss.str(); | ||
| 49 | } | ||
| 50 | |||
| 51 | std::string StringFromInt(int value); | ||
| 52 | std::string StringFromBool(bool value); | ||
| 53 | |||
| 54 | bool TryParse(const std::string &str, bool *output); | ||
| 55 | bool TryParse(const std::string &str, u32 *output); | ||
| 56 | |||
| 57 | template <typename N> | ||
| 58 | static bool TryParse(const std::string &str, N *const output) | ||
| 59 | { | ||
| 60 | std::istringstream iss(str); | ||
| 61 | |||
| 62 | N tmp = 0; | ||
| 63 | if (iss >> tmp) | ||
| 64 | { | ||
| 65 | *output = tmp; | ||
| 66 | return true; | ||
| 67 | } | ||
| 68 | else | ||
| 69 | return false; | ||
| 70 | } | ||
| 71 | |||
| 72 | // TODO: kill this | ||
| 73 | bool AsciiToHex(const char* _szValue, u32& result); | ||
| 74 | |||
| 75 | std::string TabsToSpaces(int tab_size, const std::string &in); | ||
| 76 | |||
| 77 | void SplitString(const std::string& str, char delim, std::vector<std::string>& output); | ||
| 78 | |||
| 79 | // "C:/Windows/winhelp.exe" to "C:/Windows/", "winhelp", ".exe" | ||
| 80 | bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename, std::string* _pExtension); | ||
| 81 | |||
| 82 | void BuildCompleteFilename(std::string& _CompleteFilename, const std::string& _Path, const std::string& _Filename); | ||
| 83 | std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest); | ||
| 84 | std::string UriDecode(const std::string & sSrc); | ||
| 85 | std::string UriEncode(const std::string & sSrc); | ||
| 86 | |||
| 87 | std::string CP1252ToUTF8(const std::string& str); | ||
| 88 | std::string SHIFTJISToUTF8(const std::string& str); | ||
| 89 | std::string UTF16ToUTF8(const std::wstring& str); | ||
| 90 | |||
| 91 | #ifdef _WIN32 | ||
| 92 | |||
| 93 | std::wstring UTF8ToUTF16(const std::string& str); | ||
| 94 | |||
| 95 | #ifdef _UNICODE | ||
| 96 | inline std::string TStrToUTF8(const std::wstring& str) | ||
| 97 | { return UTF16ToUTF8(str); } | ||
| 98 | |||
| 99 | inline std::wstring UTF8ToTStr(const std::string& str) | ||
| 100 | { return UTF8ToUTF16(str); } | ||
| 101 | #else | ||
| 102 | inline std::string TStrToUTF8(const std::string& str) | ||
| 103 | { return str; } | ||
| 104 | |||
| 105 | inline std::string UTF8ToTStr(const std::string& str) | ||
| 106 | { return str; } | ||
| 107 | #endif | ||
| 108 | |||
| 109 | #endif | ||
| 110 | |||
| 111 | #endif // _STRINGUTIL_H_ | ||