Mana
Loading...
Searching...
No Matches
stringutils.h
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2007-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24#include <optional>
25#include <sstream>
26#include <string>
27#include <vector>
28
35std::string &trim(std::string &str);
36
43std::string &toLower(std::string &str);
44
51std::string &toUpper(std::string &str);
52
53
60unsigned int atox(const std::string &str);
61
68template<typename T> std::string toString(const T &arg)
69{
70 std::ostringstream ss;
71 ss << arg;
72 return ss.str();
73}
74
84const char *ipToString(int address);
85
89std::string strprintf(char const *, ...)
90#ifdef __GNUC__
91 /* This attribute is nice: it even works through gettext invokation. For
92 example, gcc will complain that strprintf(_("%s"), 42) is ill-formed. */
93 __attribute__((__format__(__printf__, 1, 2)))
94#endif
95;
96
105std::string &replaceCharacters(std::string &str,
106 std::string_view chars,
107 char replacement = '_');
108
115std::string &removeColors(std::string &msg);
116
120inline bool startsWith(std::string_view str, std::string_view prefix)
121{
122 return str.substr(0, prefix.size()) == prefix;
123}
124
128inline bool endsWith(std::string_view str, std::string_view suffix)
129{
130 return str.size() >= suffix.size() &&
131 str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
132}
133
137bool isWordSeparator(char chr);
138
139std::string findSameSubstring(const std::string &str1,
140 const std::string &str2);
141
148bool getBoolFromString(std::string text, bool def = false);
149
156template<typename T, typename Enable = void>
158
159template<typename T>
160void fromString(const char *str, T &value)
161{
162 FromString<T>()(str, value);
163}
164
165inline void fromString(const char *str, std::string &value)
166{
167 value = str;
168}
169
170inline void fromString(const char *str, std::string_view &value)
171{
172 value = str;
173}
174
175inline void fromString(const char *str, int &value)
176{
177 value = atoi(str);
178}
179
180inline void fromString(const char *str, unsigned &value)
181{
182 value = strtoul(str, nullptr, 10);
183}
184
185inline void fromString(const char *str, unsigned short &value)
186{
187 value = static_cast<unsigned short>(strtoul(str, nullptr, 10));
188}
189
190inline void fromString(const char *str, float &value)
191{
192 value = strtof(str, nullptr);
193}
194
195inline void fromString(const char *str, double &value)
196{
197 value = atof(str);
198}
199
200inline void fromString(const char *str, bool &value)
201{
202 value = getBoolFromString(str);
203}
204
205void fromString(const char *str, std::vector<int> &value);
206
207template<typename T>
208struct FromString<T, std::enable_if_t<std::is_enum_v<T>>>
209{
210 void operator() (const char *str, T &value)
211 {
212 fromString(str, reinterpret_cast<std::underlying_type_t<T>&>(value));
213 }
214};
215
216template<typename T>
217struct FromString<std::optional<T>>
218{
219 void operator() (const char *str, std::optional<T> &value)
220 {
221 fromString(str, value.emplace());
222 }
223};
224
228std::string autocomplete(const std::vector<std::string> &candidates,
229 std::string base);
230
234std::string normalize(const std::string &name);
235
242std::string getDirectoryFromURL(const std::string &url);
243
248std::string join(const std::vector<std::string> &strings, const char *separator);
unsigned int atox(const std::string &str)
Converts an ascii hexidecimal string to an integer.
bool isWordSeparator(char chr)
Tells wether the character is a word separator.
std::string & toUpper(std::string &str)
Converts the given string to upper case.
std::string & replaceCharacters(std::string &str, std::string_view chars, char replacement='_')
Replaces a set of characters with another character.
void fromString(const char *str, T &value)
std::string getDirectoryFromURL(const std::string &url)
Derives a directory from the given URL, stripping the schema and replacing certain invalid characters...
std::string & toLower(std::string &str)
Converts the given string to lower case.
std::string join(const std::vector< std::string > &strings, const char *separator)
Joins a vector of strings into one string, separated by the given separator.
std::string & trim(std::string &str)
Trims spaces off the end and the beginning of the given string.
std::string findSameSubstring(const std::string &str1, const std::string &str2)
bool startsWith(std::string_view str, std::string_view prefix)
Returns whether a string starts with a given prefix.
bool endsWith(std::string_view str, std::string_view suffix)
Returns whether a string ends with a given suffix.
std::string toString(const T &arg)
Converts the given value to a string using std::stringstream.
Definition stringutils.h:68
const char * ipToString(int address)
Converts the given IP address to a string.
std::string normalize(const std::string &name)
Normalize a string, which means lowercase and trim it.
std::string strprintf(char const *,...)
A safe version of sprintf that returns a std::string of the result.
std::string autocomplete(const std::vector< std::string > &candidates, std::string base)
Returns the most approaching string of base from candidates.
std::string & removeColors(std::string &msg)
Removes colors from a string.
bool getBoolFromString(std::string text, bool def=false)
Returns a bool value depending on the given string value.
This class can be partially specialized to provide custom string conversion.
void fromString(const char *str, FillMode &value)
Definition theme.cpp:640