1 |
|
|
/* |
2 |
|
|
* The ManaPlus Client |
3 |
|
|
* Copyright (C) 2007-2009 The Mana World Development Team |
4 |
|
|
* Copyright (C) 2009-2010 The Mana Developers |
5 |
|
|
* Copyright (C) 2011-2019 The ManaPlus Developers |
6 |
|
|
* Copyright (C) 2019-2021 Andrei Karas |
7 |
|
|
* |
8 |
|
|
* This file is part of The ManaPlus Client. |
9 |
|
|
* |
10 |
|
|
* This program is free software; you can redistribute it and/or modify |
11 |
|
|
* it under the terms of the GNU General Public License as published by |
12 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
13 |
|
|
* any later version. |
14 |
|
|
* |
15 |
|
|
* This program is distributed in the hope that it will be useful, |
16 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
|
|
* GNU General Public License for more details. |
19 |
|
|
* |
20 |
|
|
* You should have received a copy of the GNU General Public License |
21 |
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
22 |
|
|
*/ |
23 |
|
|
|
24 |
|
|
#ifndef UTILS_STRINGUTILS_H |
25 |
|
|
#define UTILS_STRINGUTILS_H |
26 |
|
|
|
27 |
|
|
#include "utils/stringvector.h" |
28 |
|
|
|
29 |
|
|
#include <list> |
30 |
|
|
#include <set> |
31 |
|
|
|
32 |
|
|
#include "localconsts.h" |
33 |
|
|
|
34 |
|
|
/** |
35 |
|
|
* Trims spaces off the end and the beginning of the given string. |
36 |
|
|
* |
37 |
|
|
* @param str the string to trim spaces off |
38 |
|
|
* @return a reference to the trimmed string |
39 |
|
|
*/ |
40 |
|
|
std::string &trim(std::string &str); |
41 |
|
|
|
42 |
|
|
/** |
43 |
|
|
* Converts the given string to lower case. |
44 |
|
|
* |
45 |
|
|
* @param str the string to convert to lower case |
46 |
|
|
* @return a reference to the given string converted to lower case |
47 |
|
|
*/ |
48 |
|
|
std::string &toLower(std::string &str); |
49 |
|
|
|
50 |
|
|
/** |
51 |
|
|
* Converts the given string to upper case. |
52 |
|
|
* |
53 |
|
|
* @param str the string to convert to upper case |
54 |
|
|
* @return a reference to the given string converted to upper case |
55 |
|
|
*/ |
56 |
|
|
std::string &toUpper(std::string &str); |
57 |
|
|
|
58 |
|
|
|
59 |
|
|
/** |
60 |
|
|
* Converts an ascii hexidecimal string to an integer |
61 |
|
|
* |
62 |
|
|
* @param str the hex string to convert to an int |
63 |
|
|
* @return the integer representation of the hex string |
64 |
|
|
*/ |
65 |
|
|
unsigned int atox(const std::string &str) A_WARN_UNUSED; |
66 |
|
|
|
67 |
|
|
/** |
68 |
|
|
* Converts the given value to a string. |
69 |
|
|
* |
70 |
|
|
* @param num the value to convert to a string |
71 |
|
|
* @return the string representation of arg |
72 |
|
|
*/ |
73 |
|
|
std::string toString(uint32_t num); |
74 |
|
|
|
75 |
|
|
std::string toString(uint64_t num); |
76 |
|
|
|
77 |
|
|
std::string toString(unsigned char num); |
78 |
|
|
|
79 |
|
|
std::string toString(int32_t num); |
80 |
|
|
|
81 |
|
|
std::string toString(uint16_t num); |
82 |
|
|
|
83 |
|
|
std::string toString(const float num); |
84 |
|
|
|
85 |
|
|
std::string toString(const double num); |
86 |
|
|
|
87 |
|
|
std::string toStringPrint(const unsigned int val); |
88 |
|
|
|
89 |
|
|
/** |
90 |
|
|
* Converts the given IP address to a string. |
91 |
|
|
* |
92 |
|
|
* The returned string is statically allocated, and shouldn't be freed. It is |
93 |
|
|
* changed upon the next use of this method. |
94 |
|
|
* |
95 |
|
|
* @param address the address to convert to a string |
96 |
|
|
* @return the string representation of the address |
97 |
|
|
*/ |
98 |
|
|
const char *ipToString(const uint32_t address) A_WARN_UNUSED; |
99 |
|
|
|
100 |
|
|
/** |
101 |
|
|
* A safe version of sprintf that returns a std::string of the result. |
102 |
|
|
*/ |
103 |
|
|
std::string strprintf(const char *const format, ...) A_NONNULL(1) A_WARN_UNUSED |
104 |
|
|
#ifdef __GNUC__ |
105 |
|
|
#ifdef __OpenBSD__ |
106 |
|
|
__attribute__((__format__(printf, 1, 2))) |
107 |
|
|
#else // __OpenBSD__ |
108 |
|
|
__attribute__((__format__(gnu_printf, 1, 2))) |
109 |
|
|
#endif // __OpenBSD__ |
110 |
|
|
#endif // __GNUC__ |
111 |
|
|
; |
112 |
|
|
|
113 |
|
|
/** |
114 |
|
|
* Removes colors from a string |
115 |
|
|
* |
116 |
|
|
* @param msg the string to remove the colors from |
117 |
|
|
* @return string without colors |
118 |
|
|
*/ |
119 |
|
|
std::string removeColors(std::string msg) A_WARN_UNUSED; |
120 |
|
|
|
121 |
|
|
const std::string findSameSubstring(const std::string &restrict str1, |
122 |
|
|
const std::string &restrict str2); |
123 |
|
|
|
124 |
|
|
const std::string findSameSubstringI(const std::string &restrict str1, |
125 |
|
|
const std::string &restrict str2); |
126 |
|
|
|
127 |
|
|
/** |
128 |
|
|
* Compares the two strings case-insensitively. |
129 |
|
|
* |
130 |
|
|
* @param a the first string in the comparison |
131 |
|
|
* @param b the second string in the comparison |
132 |
|
|
* @return 0 if the strings are equal, positive if the first is greater, |
133 |
|
|
* negative if the second is greater |
134 |
|
|
*/ |
135 |
|
|
int compareStrI(const std::string &a, const std::string &b) A_WARN_UNUSED; |
136 |
|
|
|
137 |
|
|
/** |
138 |
|
|
* Tells wether the character is a word separator. |
139 |
|
|
*/ |
140 |
|
|
constexpr inline bool isWordSeparator(const signed char chr) |
141 |
|
|
A_CONST A_WARN_UNUSED; |
142 |
|
|
constexpr inline bool isWordSeparator(const signed char chr) |
143 |
|
|
{ |
144 |
|
|
return chr == ' ' || |
145 |
|
|
chr == ',' || |
146 |
|
|
chr == '.' || |
147 |
|
|
chr == '"'; |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
size_t findI(std::string str, std::string subStr) A_WARN_UNUSED; |
151 |
|
|
|
152 |
|
|
size_t findI(std::string text, const StringVect &list) A_WARN_UNUSED; |
153 |
|
|
|
154 |
|
|
size_t findAny(const std::string &restrict text, |
155 |
|
|
const std::string &restrict chars, |
156 |
|
|
const size_t pos) A_WARN_UNUSED; |
157 |
|
|
|
158 |
|
|
const std::string encodeStr(unsigned int value, |
159 |
|
|
const unsigned int size) A_WARN_UNUSED; |
160 |
|
|
|
161 |
|
|
unsigned int decodeStr(const std::string &str) A_WARN_UNUSED; |
162 |
|
|
|
163 |
|
|
std::string extractNameFromSprite(std::string str) A_WARN_UNUSED; |
164 |
|
|
|
165 |
|
|
std::string removeSpriteIndex(std::string str) A_WARN_UNUSED; |
166 |
|
|
|
167 |
|
|
const char* getSafeUtf8String(const std::string &text) A_WARN_UNUSED; |
168 |
|
|
|
169 |
|
|
void getSafeUtf8String(std::string text, char *const buf); |
170 |
|
|
|
171 |
|
|
std::string getFileName(const std::string &path) A_WARN_UNUSED; |
172 |
|
|
|
173 |
|
|
std::string getFileDir(const std::string &path) A_WARN_UNUSED; |
174 |
|
|
|
175 |
|
|
std::string& replaceAll(std::string& context, |
176 |
|
|
const std::string &restrict from, |
177 |
|
|
const std::string &restrict to); |
178 |
|
|
|
179 |
|
|
void replaceRecursiveAll(std::string& context, |
180 |
|
|
const std::string &restrict from, |
181 |
|
|
const char to); |
182 |
|
|
|
183 |
|
|
/** |
184 |
|
|
* Returns a bool value depending on the given string value. |
185 |
|
|
* |
186 |
|
|
* @param text the string used to get the bool value |
187 |
|
|
* @return a boolean value.. |
188 |
|
|
*/ |
189 |
|
|
bool getBoolFromString(const std::string &text) A_WARN_UNUSED; |
190 |
|
|
|
191 |
|
|
void replaceSpecialChars(std::string &text); |
192 |
|
|
|
193 |
|
|
/** |
194 |
|
|
* Normalize a string, which means lowercase and trim it. |
195 |
|
|
*/ |
196 |
|
|
std::string normalize(const std::string &name) A_WARN_UNUSED; |
197 |
|
|
|
198 |
|
|
void splitToIntSet(std::set<int> &tokens, const std::string &text, |
199 |
|
|
const char separator); |
200 |
|
|
|
201 |
|
|
std::list<int> splitToIntList(const std::string &text, |
202 |
|
|
const char separator) A_WARN_UNUSED; |
203 |
|
|
|
204 |
|
|
std::list<std::string> splitToStringList(const std::string &text, |
205 |
|
|
const char separator) A_WARN_UNUSED; |
206 |
|
|
|
207 |
|
|
void splitToStringVector(StringVect &tokens, |
208 |
|
|
const std::string &text, const char separator); |
209 |
|
|
|
210 |
|
|
void splitToStringSet(std::set<std::string> &tokens, |
211 |
|
|
const std::string &text, const char separator); |
212 |
|
|
|
213 |
|
|
void splitToIntVector(STD_VECTOR<int> &tokens, |
214 |
|
|
const std::string &text, const char separator); |
215 |
|
|
|
216 |
|
|
std::string combineDye(std::string file, const std::string &dye) A_WARN_UNUSED; |
217 |
|
|
|
218 |
|
|
std::string combineDye2(std::string file, |
219 |
|
|
const std::string &dye) A_WARN_UNUSED; |
220 |
|
|
|
221 |
|
|
std::string combineDye3(std::string file, |
222 |
|
|
const std::string &dye) A_WARN_UNUSED; |
223 |
|
|
|
224 |
|
|
std::string packList(const std::list<std::string> &list) A_WARN_UNUSED; |
225 |
|
|
|
226 |
|
|
std::list<std::string> unpackList(const std::string &str) A_WARN_UNUSED; |
227 |
|
|
|
228 |
|
|
std::string stringToHexPath(const std::string &str) A_WARN_UNUSED; |
229 |
|
|
|
230 |
|
|
void deleteCharLeft(std::string &str, unsigned *const pos); |
231 |
|
|
|
232 |
|
|
bool findLast(const std::string &restrict str1, |
233 |
|
|
const std::string &restrict str2) A_WARN_UNUSED; |
234 |
|
|
|
235 |
|
|
bool findFirst(const std::string &restrict str1, |
236 |
|
|
const std::string &restrict str2) A_WARN_UNUSED; |
237 |
|
|
|
238 |
|
|
bool findCutLast(std::string &restrict str1, |
239 |
|
|
const std::string &restrict str2) A_WARN_UNUSED; |
240 |
|
|
|
241 |
|
|
void cutLast(std::string &restrict str1, |
242 |
|
|
const std::string &restrict str2); |
243 |
|
|
|
244 |
|
|
bool findCutFirst(std::string &restrict str1, |
245 |
|
|
const std::string &restrict str2); |
246 |
|
|
|
247 |
|
|
void cutFirst(std::string &restrict str1, |
248 |
|
|
const std::string &restrict str2); |
249 |
|
|
|
250 |
|
|
std::string &removeProtocol(std::string &url); |
251 |
|
|
|
252 |
|
|
bool strStartWith(const std::string &restrict str, |
253 |
|
|
const std::string &restrict start) A_WARN_UNUSED; |
254 |
|
|
|
255 |
|
|
std::string getDateString() A_WARN_UNUSED; |
256 |
|
|
|
257 |
|
|
std::string getDateTimeString() A_WARN_UNUSED; |
258 |
|
|
|
259 |
|
|
signed char parseBoolean(const std::string &value); |
260 |
|
|
|
261 |
|
|
std::string encodeLinkText(std::string data); |
262 |
|
|
|
263 |
|
|
std::string decodeLinkText(std::string data); |
264 |
|
|
|
265 |
|
|
bool isDigit(const std::string &str); |
266 |
|
|
|
267 |
|
|
void secureChatCommand(std::string &str); |
268 |
|
|
|
269 |
|
|
bool parse2Int(const std::string &args, int &x, int &y); |
270 |
|
|
|
271 |
|
|
bool parse2Str(const std::string &args, std::string &str1, std::string &str2); |
272 |
|
|
|
273 |
|
|
uint32_t parseNumber(const std::string &str); |
274 |
|
|
|
275 |
|
|
std::string removeToken(std::string &str, const std::string &token); |
276 |
|
|
|
277 |
|
|
std::string timeToStr(const uint32_t time); |
278 |
|
|
|
279 |
|
|
std::string timeDiffToString(int timeDiff); |
280 |
|
|
|
281 |
|
|
void replaceItemLinks(std::string &msg); |
282 |
|
|
|
283 |
|
|
std::string escapeString(std::string str); |
284 |
|
|
|
285 |
|
|
void sanitizePath(std::string &path); |
286 |
|
|
|
287 |
|
|
std::string pathJoin(std::string str1, |
288 |
|
|
const std::string &str2); |
289 |
|
|
|
290 |
|
|
std::string pathJoin(std::string str1, |
291 |
|
|
const std::string &str2, |
292 |
|
|
const std::string &str3); |
293 |
|
|
|
294 |
|
|
std::string urlJoin(std::string str1, |
295 |
|
|
const std::string &str2); |
296 |
|
|
|
297 |
|
|
size_t rfindSepatator(const std::string &str1); |
298 |
|
|
|
299 |
|
|
#endif // UTILS_STRINGUTILS_H |