summaryrefslogtreecommitdiff
path: root/src/stringutils.h
blob: f7218c05f7b25f5cdc5d2b91a4b4a1d81d4962ec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 *  The ManaPlus Client
 *  Copyright (C) 2007-2009  The Mana World Development Team
 *  Copyright (C) 2009-2010  The Mana Developers
 *  Copyright (C) 2011-2014  The ManaPlus Developers
 *
 *  This file is part of The ManaPlus Client.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef STRINGUTILS_H
#define STRINGUTILS_H

#include "stringvector.h"

#include <sstream>
#include <list>
#include <set>

#include "localconsts.h"

/**
 * Trims spaces off the end and the beginning of the given string.
 *
 * @param str the string to trim spaces off
 * @return a reference to the trimmed string
 */
std::string &trim(std::string &str);

/**
 * Converts the given string to lower case.
 *
 * @param str the string to convert to lower case
 * @return a reference to the given string converted to lower case
 */
std::string &toLower(std::string &str);

/**
 * Converts the given string to upper case.
 *
 * @param str the string to convert to upper case
 * @return a reference to the given string converted to upper case
 */
std::string &toUpper(std::string &str);


/**
 * Converts an ascii hexidecimal string to an integer
 *
 * @param str the hex string to convert to an int
 * @return the integer representation of the hex string
 */
unsigned int atox(const std::string &str) A_WARN_UNUSED;

template<typename T> std::string toString(const T &arg) A_WARN_UNUSED;

/**
 * Converts the given value to a string using std::stringstream.
 *
 * @param arg the value to convert to a string
 * @return the string representation of arg
 */
template<typename T> std::string toString(const T &arg)
{
    std::stringstream ss;
    ss << arg;
    return ss.str();
}

std::string toStringPrint(const unsigned int val);

/**
 * Converts the given IP address to a string.
 *
 * The returned string is statically allocated, and shouldn't be freed. It is
 * changed upon the next use of this method.
 *
 * @param address the address to convert to a string
 * @return the string representation of the address
 */
const char *ipToString(const uint32_t address) A_WARN_UNUSED;

/**
 * A safe version of sprintf that returns a std::string of the result.
 */
std::string strprintf(const char *const format, ...) A_WARN_UNUSED
#ifdef __GNUC__
    /* This attribute is nice: it even works through gettext invokation. For
       example, gcc will complain that strprintf(_("%s"), 42) is ill-formed. */
#ifdef __OpenBSD__
    __attribute__((__format__(printf, 1, 2)))
#else
    __attribute__((__format__(gnu_printf, 1, 2)))
#endif
#endif
;

/**
 * Removes colors from a string
 *
 * @param msg the string to remove the colors from
 * @return string without colors
 */
std::string removeColors(std::string msg) A_WARN_UNUSED;

const std::string findSameSubstring(const std::string &restrict str1,
                                    const std::string &restrict str2);

const std::string findSameSubstringI(const std::string &restrict str1,
                                     const std::string &restrict str2);

/**
 * Compares the two strings case-insensitively.
 *
 * @param a the first string in the comparison
 * @param b the second string in the comparison
 * @return 0 if the strings are equal, positive if the first is greater,
 *           negative if the second is greater
 */
int compareStrI(const std::string &a, const std::string &b) A_WARN_UNUSED;

/**
 * Tells wether the character is a word separator.
 */
bool isWordSeparator(const signed char chr) A_WARN_UNUSED;

size_t findI(std::string str, std::string subStr) A_WARN_UNUSED;

size_t findI(std::string text, const StringVect &list) A_WARN_UNUSED;

const std::string encodeStr(unsigned int value,
                            const unsigned int size = 0) A_WARN_UNUSED;

unsigned int decodeStr(const std::string &str) A_WARN_UNUSED;

std::string extractNameFromSprite(std::string str) A_WARN_UNUSED;

std::string removeSpriteIndex(std::string str) A_WARN_UNUSED;

std::string getFileName(const std::string &path) A_WARN_UNUSED;

std::string getFileDir(const std::string &path) A_WARN_UNUSED;

std::string& replaceAll(std::string& context, const std::string &restrict from,
                        const std::string &restrict to);

/**
 * Returns a bool value depending on the given string value.
 *
 * @param text the string used to get the bool value
 * @return a boolean value..
 */
bool getBoolFromString(const std::string &text) A_WARN_UNUSED;

void replaceSpecialChars(std::string &text);

/**
 * Normalize a string, which means lowercase and trim it.
 */
std::string normalize(const std::string &name) A_WARN_UNUSED;

void splitToIntSet(std::set<int> &tokens, const std::string &text,
                   const char separator);

std::list<int> splitToIntList(const std::string &text,
                              const char separator) A_WARN_UNUSED;

std::list<std::string> splitToStringList(const std::string &text,
                                         const char separator) A_WARN_UNUSED;

void splitToStringVector(StringVect &tokens,
                         const std::string &text, const char separator);

void splitToStringSet(std::set<std::string> &tokens,
                      const std::string &text, const char separator);

void splitToIntVector(std::vector<int> &tokens,
                      const std::string &text, const char separator);

std::string combineDye(std::string file, const std::string &dye) A_WARN_UNUSED;

std::string combineDye2(std::string file,
                        const std::string &dye) A_WARN_UNUSED;

std::string combineDye3(std::string file,
                        const std::string &dye) A_WARN_UNUSED;

std::string packList(const std::list<std::string> &list) A_WARN_UNUSED;

std::list<std::string> unpackList(const std::string &str) A_WARN_UNUSED;

std::string stringToHexPath(const std::string &str) A_WARN_UNUSED;

void deleteCharLeft(std::string &str, unsigned *const pos);

bool findLast(const std::string &restrict str1,
              const std::string &restrict str2) A_WARN_UNUSED;

bool findFirst(const std::string &restrict str1,
               const std::string &restrict str2) A_WARN_UNUSED;

bool findCutLast(std::string &restrict str1,
                 const std::string &restrict str2) A_WARN_UNUSED;

bool findCutFirst(std::string &restrict str1,
                  const std::string &restrict str2) A_WARN_UNUSED;

std::string &removeProtocol(std::string &url);

bool strStartWith(const std::string &restrict str,
                  const std::string &restrict start) A_WARN_UNUSED;

std::string getDateString() A_WARN_UNUSED;

signed char parseBoolean(const std::string &value);

std::string encodeLinkText(std::string data);

std::string decodeLinkText(std::string data);

bool isDigit(const std::string &str);

void secureChatCommand(std::string &str);

bool isMatch(const std::string &str, const std::string &exp);

#endif  // STRINGUTILS_H