1 // Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project
2 // Licensed under GPLv2 or any later version
3 // Refer to the license.txt file included.
4 
5 #include <algorithm>
6 #include <cctype>
7 #include <codecvt>
8 #include <cstdlib>
9 #include <locale>
10 #include <sstream>
11 #include "common/common_paths.h"
12 #include "common/logging/log.h"
13 #include "common/string_util.h"
14 
15 #ifdef _WIN32
16 #include <windows.h>
17 #endif
18 
19 namespace Common {
20 
21 /// Make a string lowercase
ToLower(std::string str)22 std::string ToLower(std::string str) {
23     std::transform(str.begin(), str.end(), str.begin(),
24                    [](unsigned char c) { return std::tolower(c); });
25     return str;
26 }
27 
28 /// Make a string uppercase
ToUpper(std::string str)29 std::string ToUpper(std::string str) {
30     std::transform(str.begin(), str.end(), str.begin(),
31                    [](unsigned char c) { return std::toupper(c); });
32     return str;
33 }
34 
35 // Turns "  hej " into "hej". Also handles tabs.
StripSpaces(const std::string & str)36 std::string StripSpaces(const std::string& str) {
37     const std::size_t s = str.find_first_not_of(" \t\r\n");
38 
39     if (str.npos != s)
40         return str.substr(s, str.find_last_not_of(" \t\r\n") - s + 1);
41     else
42         return "";
43 }
44 
45 // "\"hello\"" is turned to "hello"
46 // This one assumes that the string has already been space stripped in both
47 // ends, as done by StripSpaces above, for example.
StripQuotes(const std::string & s)48 std::string StripQuotes(const std::string& s) {
49     if (s.size() && '\"' == s[0] && '\"' == *s.rbegin())
50         return s.substr(1, s.size() - 2);
51     else
52         return s;
53 }
54 
StringFromBool(bool value)55 std::string StringFromBool(bool value) {
56     return value ? "True" : "False";
57 }
58 
SplitPath(const std::string & full_path,std::string * _pPath,std::string * _pFilename,std::string * _pExtension)59 bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename,
60                std::string* _pExtension) {
61     if (full_path.empty())
62         return false;
63 
64     std::size_t dir_end = full_path.find_last_of("/"
65 // windows needs the : included for something like just "C:" to be considered a directory
66 #ifdef _WIN32
67                                                  ":"
68 #endif
69     );
70     if (std::string::npos == dir_end)
71         dir_end = 0;
72     else
73         dir_end += 1;
74 
75     std::size_t fname_end = full_path.rfind('.');
76     if (fname_end < dir_end || std::string::npos == fname_end)
77         fname_end = full_path.size();
78 
79     if (_pPath)
80         *_pPath = full_path.substr(0, dir_end);
81 
82     if (_pFilename)
83         *_pFilename = full_path.substr(dir_end, fname_end - dir_end);
84 
85     if (_pExtension)
86         *_pExtension = full_path.substr(fname_end);
87 
88     return true;
89 }
90 
BuildCompleteFilename(std::string & _CompleteFilename,const std::string & _Path,const std::string & _Filename)91 void BuildCompleteFilename(std::string& _CompleteFilename, const std::string& _Path,
92                            const std::string& _Filename) {
93     _CompleteFilename = _Path;
94 
95     // check for seperator
96     if (DIR_SEP_CHR != *_CompleteFilename.rbegin())
97         _CompleteFilename += DIR_SEP_CHR;
98 
99     // add the filename
100     _CompleteFilename += _Filename;
101 }
102 
SplitString(const std::string & str,const char delim,std::vector<std::string> & output)103 void SplitString(const std::string& str, const char delim, std::vector<std::string>& output) {
104     std::istringstream iss(str);
105     output.resize(1);
106 
107     while (std::getline(iss, *output.rbegin(), delim)) {
108         output.emplace_back();
109     }
110 
111     output.pop_back();
112 }
113 
TabsToSpaces(int tab_size,std::string in)114 std::string TabsToSpaces(int tab_size, std::string in) {
115     std::size_t i = 0;
116 
117     while ((i = in.find('\t')) != std::string::npos) {
118         in.replace(i, 1, tab_size, ' ');
119     }
120 
121     return in;
122 }
123 
ReplaceAll(std::string result,const std::string & src,const std::string & dest)124 std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest) {
125     std::size_t pos = 0;
126 
127     if (src == dest)
128         return result;
129 
130     while ((pos = result.find(src, pos)) != std::string::npos) {
131         result.replace(pos, src.size(), dest);
132         pos += dest.length();
133     }
134 
135     return result;
136 }
137 
UTF16ToUTF8(const std::u16string & input)138 std::string UTF16ToUTF8(const std::u16string& input) {
139     std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
140     return convert.to_bytes(input);
141 }
142 
UTF8ToUTF16(const std::string & input)143 std::u16string UTF8ToUTF16(const std::string& input) {
144     std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
145     return convert.from_bytes(input);
146 }
147 
148 #ifdef _WIN32
CPToUTF16(u32 code_page,const std::string & input)149 static std::wstring CPToUTF16(u32 code_page, const std::string& input) {
150     const auto size =
151         MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()), nullptr, 0);
152 
153     if (size == 0) {
154         return L"";
155     }
156 
157     std::wstring output(size, L'\0');
158 
159     if (size != MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()),
160                                     &output[0], static_cast<int>(output.size()))) {
161         output.clear();
162     }
163 
164     return output;
165 }
166 
UTF16ToUTF8(const std::wstring & input)167 std::string UTF16ToUTF8(const std::wstring& input) {
168     const auto size = WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()),
169                                           nullptr, 0, nullptr, nullptr);
170     if (size == 0) {
171         return "";
172     }
173 
174     std::string output(size, '\0');
175 
176     if (size != WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()),
177                                     &output[0], static_cast<int>(output.size()), nullptr,
178                                     nullptr)) {
179         output.clear();
180     }
181 
182     return output;
183 }
184 
UTF8ToUTF16W(const std::string & input)185 std::wstring UTF8ToUTF16W(const std::string& input) {
186     return CPToUTF16(CP_UTF8, input);
187 }
188 
189 #endif
190 
StringFromFixedZeroTerminatedBuffer(const char * buffer,std::size_t max_len)191 std::string StringFromFixedZeroTerminatedBuffer(const char* buffer, std::size_t max_len) {
192     std::size_t len = 0;
193     while (len < max_len && buffer[len] != '\0')
194         ++len;
195 
196     return std::string(buffer, len);
197 }
198 } // namespace Common
199