GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/stringutils.h Lines: 0 4 0.0 %
Date: 2018-07-14 Branches: 0 20 0.0 %

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