ManaPlus
stringutils.h
Go to the documentation of this file.
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 
40 std::string &trim(std::string &str);
41 
48 std::string &toLower(std::string &str);
49 
56 std::string &toUpper(std::string &str);
57 
58 
65 unsigned int atox(const std::string &str) A_WARN_UNUSED;
66 
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 
98 const char *ipToString(const uint32_t address) A_WARN_UNUSED;
99 
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 
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 
135 int compareStrI(const std::string &a, const std::string &b) A_WARN_UNUSED;
136 
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 
189 bool getBoolFromString(const std::string &text) A_WARN_UNUSED;
190 
191 void replaceSpecialChars(std::string &text);
192 
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 
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
#define restrict
Definition: localconsts.h:165
#define gnu_printf
Definition: localconsts.h:163
#define A_WARN_UNUSED
Definition: localconsts.h:161
#define A_NONNULL(...)
Definition: localconsts.h:168
#define constexpr
Definition: localconsts.h:48
uint32_t data
bool msg(InputEvent &event)
Definition: chat.cpp:39
bool url(InputEvent &event)
Definition: commands.cpp:64
union EAthena::ItemFlags __attribute__((packed))
int size()
Definition: emotedb.cpp:306
std::string toString(uint32_t num)
std::string removeColors(std::string msg)
void sanitizePath(std::string &path)
bool isWordSeparator(const signed char chr) A_CONST
Definition: stringutils.h:142
std::string packList(const std::list< std::string > &list)
void cutFirst(std::string &str1, const std::string &str2)
std::string combineDye3(std::string file, const std::string &dye)
const std::string findSameSubstringI(const std::string &str1, const std::string &str2)
std::string & toUpper(std::string &str)
Definition: stringutils.cpp:72
std::string removeSpriteIndex(std::string str)
std::string getFileName(const std::string &path)
unsigned int atox(const std::string &str)
Definition: stringutils.cpp:78
std::list< std::string > splitToStringList(const std::string &text, const char separator)
std::string & replaceAll(std::string &context, const std::string &from, const std::string &to)
void deleteCharLeft(std::string &str, unsigned *const pos)
void cutLast(std::string &str1, const std::string &str2)
std::string combineDye(std::string file, const std::string &dye)
void splitToIntVector(std::vector< int > &tokens, const std::string &text, const char separator)
bool parse2Int(const std::string &args, int &x, int &y)
std::string stringToHexPath(const std::string &str)
std::string & removeProtocol(std::string &url)
std::string toStringPrint(const unsigned int val)
std::string strprintf(const char *const format,...)
std::string getDateTimeString()
bool parse2Str(const std::string &args, std::string &str1, std::string &str2)
std::string & toLower(std::string &str)
Definition: stringutils.cpp:66
std::string removeToken(std::string &str, const std::string &token)
void splitToStringVector(StringVect &tokens, const std::string &text, const char separator)
int compareStrI(const std::string &a, const std::string &b)
std::string timeDiffToString(int timeDiff)
std::string getFileDir(const std::string &path)
std::string escapeString(std::string str)
void replaceRecursiveAll(std::string &context, const std::string &from, const char to)
unsigned int decodeStr(const std::string &str)
bool findCutFirst(std::string &str1, const std::string &str2)
void splitToIntSet(std::set< int > &tokens, const std::string &text, const char separator)
size_t findI(std::string str, std::string subStr)
bool getBoolFromString(const std::string &text)
void replaceItemLinks(std::string &msg)
void splitToStringSet(std::set< std::string > &tokens, const std::string &text, const char separator)
bool findLast(const std::string &str1, const std::string &str2)
std::string decodeLinkText(std::string data)
signed char parseBoolean(const std::string &value)
size_t rfindSepatator(const std::string &str1)
std::string timeToStr(const uint32_t time)
bool findCutLast(std::string &str1, const std::string &str2)
std::string combineDye2(std::string file, const std::string &dye)
const char * getSafeUtf8String(const std::string &text)
std::string pathJoin(std::string str1, const std::string &str2)
size_t findAny(const std::string &text, const std::string &chars, const size_t pos)
std::string urlJoin(std::string str1, const std::string &str2)
const std::string encodeStr(unsigned int value, const unsigned int size)
bool isDigit(const std::string &str)
std::string normalize(const std::string &name)
std::list< int > splitToIntList(const std::string &text, const char separator)
void secureChatCommand(std::string &str)
std::string extractNameFromSprite(std::string str)
bool strStartWith(const std::string &str, const std::string &start)
void replaceSpecialChars(std::string &text)
const char * ipToString(const uint32_t address)
Definition: stringutils.cpp:86
const std::string findSameSubstring(const std::string &str1, const std::string &str2)
uint32_t parseNumber(const std::string &str)
std::string encodeLinkText(std::string data)
std::string getDateString()
bool findFirst(const std::string &str1, const std::string &str2)
std::string & trim(std::string &str)
Definition: stringutils.cpp:47
std::list< std::string > unpackList(const std::string &str)
std::vector< std::string > StringVect
Definition: stringvector.h:29