Cuberite
A lightweight, fast and extensible game server for Minecraft
StringUtils.h
Go to the documentation of this file.
1 
2 // StringUtils.h
3 
4 // Interfaces to various string helper functions
5 
6 
7 
8 
9 #pragma once
10 
11 
12 
13 typedef std::string AString;
14 typedef std::vector<AString> AStringVector;
15 typedef std::list<AString> AStringList;
16 
18 typedef std::map<AString, AString> AStringMap;
19 
20 
21 
22 
23 
26 extern AString & Printf(AString & a_Dst, const char * format, fmt::ArgList args);
27 FMT_VARIADIC(AString &, Printf, AString &, const char *)
28 
29 
31 extern AString Printf(const char * format, fmt::ArgList args);
32 FMT_VARIADIC(AString, Printf, const char *)
33 
36 template <typename... Args>
37 extern AString & AppendPrintf(AString & a_Dst, const char * format, const Args & ... args)
38 {
39  a_Dst += Printf(format, args...);
40  return a_Dst;
41 }
42 
45 extern AStringVector StringSplit(const AString & str, const AString & delim);
46 
50 extern AStringVector StringSplitWithQuotes(const AString & str, const AString & delim);
51 
53 AString StringJoin(const AStringVector & a_Strings, const AString & a_Delimiter);
54 
57 extern AStringVector StringSplitAndTrim(const AString & str, const AString & delim);
58 
61 extern AString TrimString(const AString & str); // tolua_export
62 
65 extern AString & InPlaceUppercase(AString & s);
66 
69 extern AString & InPlaceLowercase(AString & s);
70 
72 extern AString StrToUpper(const AString & s);
73 
75 extern AString StrToLower(const AString & s);
76 
79 extern int NoCaseCompare(const AString & s1, const AString & s2); // tolua_export
80 
82 extern size_t RateCompareString(const AString & s1, const AString & s2);
83 
85 extern void ReplaceString(AString & iHayStack, const AString & iNeedle, const AString & iReplaceWith); // tolua_export
86 
88 extern AString & RawBEToUTF8(const char * a_RawData, size_t a_NumShorts, AString & a_UTF8);
89 
91 extern AString UnicodeCharToUtf8(unsigned a_UnicodeChar);
92 
94 extern std::u16string UTF8ToRawBEUTF16(const AString & a_String);
95 
97 extern AString & CreateHexDump(AString & a_Out, const void * a_Data, size_t a_Size, size_t a_BytesPerLine);
98 
100 extern AString EscapeString(const AString & a_Message); // tolua_export
101 
103 extern AString StripColorCodes(const AString & a_Message); // tolua_export
104 
108 extern std::pair<bool, AString> URLDecode(const AString & a_String); // Exported to Lua as cUrlParser::UrlDecode()
109 
111 extern AString URLEncode(const AString & a_Text);
112 
114 extern AString ReplaceAllCharOccurrences(const AString & a_String, char a_From, char a_To); // Needn't export to Lua, since Lua doesn't have chars anyway
115 
117 extern AString Base64Decode(const AString & a_Base64String); // Exported manually due to embedded NULs and extra parameter
118 
120 extern AString Base64Encode(const AString & a_Input); // Exported manually due to embedded NULs and extra parameter
121 
123 extern short GetBEShort(const char * a_Mem);
124 
126 extern unsigned short GetBEUShort(const char * a_Mem);
127 
129 extern int GetBEInt(const char * a_Mem);
130 
132 extern void SetBEInt(char * a_Mem, Int32 a_Value);
133 
137 extern bool SplitZeroTerminatedStrings(const AString & a_Strings, AStringVector & a_Output);
138 
143 extern AStringVector MergeStringVectors(const AStringVector & a_Strings1, const AStringVector & a_Strings2);
144 
147 extern AString StringsConcat(const AStringVector & a_Strings, char a_Separator);
148 
150 extern bool StringToFloat(const AString & a_String, float & a_Num);
151 
153 bool IsOnlyWhitespace(const AString & a_String);
154 
155 
156 
157 
158 
160 template <class T>
161 bool StringToInteger(const AString & a_str, T & a_Num)
162 {
163  size_t i = 0;
164  bool positive = true;
165  T result = 0;
166  if (a_str[0] == '+')
167  {
168  i++;
169  }
170  else if (a_str[0] == '-')
171  {
172  i++;
173  positive = false;
174  }
175  if (positive)
176  {
177  for (size_t size = a_str.size(); i < size; i++)
178  {
179  if ((a_str[i] < '0') || (a_str[i] > '9'))
180  {
181  return false;
182  }
183  if (std::numeric_limits<T>::max() / 10 < result)
184  {
185  return false;
186  }
187  result *= 10;
188  T digit = static_cast<T>(a_str[i] - '0');
189  if (std::numeric_limits<T>::max() - digit < result)
190  {
191  return false;
192  }
193  result += digit;
194  }
195  }
196  else
197  {
198  // Unsigned result cannot be signed!
199  if (!std::numeric_limits<T>::is_signed)
200  {
201  return false;
202  }
203 
204  for (size_t size = a_str.size(); i < size; i++)
205  {
206  if ((a_str[i] < '0') || (a_str[i] > '9'))
207  {
208  return false;
209  }
210  if (std::numeric_limits<T>::min() / 10 > result)
211  {
212  return false;
213  }
214  result *= 10;
215  T digit = static_cast<T>(a_str[i] - '0');
216  if (std::numeric_limits<T>::min() + digit > result)
217  {
218  return false;
219  }
220  result -= digit;
221  }
222  }
223  a_Num = result;
224  return true;
225 }
226 
227 
228 
229 
230 
233 template <typename T>
234 T GetStringMapInteger(const AStringMap & a_Map, const AString & a_Key, T a_Default)
235 {
236  // Try to locate the key:
237  auto itr = a_Map.find(a_Key);
238  if (itr == a_Map.end())
239  {
240  return a_Default;
241  }
242 
243  // Try to convert the value to a number:
244  T res = a_Default;
245  if (!StringToInteger<T>(itr->second, res))
246  {
247  return a_Default;
248  }
249  return res;
250 }
251 
252 
253 
254 
255 
256 // If you have any other string helper functions, declare them here
257 
258 
259 
260 
AStringVector StringSplit(const AString &str, const AString &delim)
Split the string at any of the listed delimiters.
Definition: StringUtils.cpp:76
AString TrimString(const AString &str)
Trims whitespace at both ends of the string.
AStringVector StringSplitAndTrim(const AString &str, const AString &delim)
Split the string at any of the listed delimiters and trim each value.
size_t RateCompareString(const AString &s1, const AString &s2)
Case-insensitive string comparison that returns a rating of equal-ness between [0 - s1...
bool StringToInteger(const AString &a_str, T &a_Num)
Parses any integer type.
Definition: StringUtils.h:161
std::u16string UTF8ToRawBEUTF16(const AString &a_String)
Converts a UTF-8 string into a UTF-16 BE string.
AStringVector MergeStringVectors(const AStringVector &a_Strings1, const AStringVector &a_Strings2)
Merges the two vectors of strings, removing duplicate entries from the second vector.
int NoCaseCompare(const AString &s1, const AString &s2)
Case-insensitive string comparison.
bool StringToFloat(const AString &a_String, float &a_Num)
Converts a string into a float.
AString StripColorCodes(const AString &a_Message)
Removes all control codes used by MC for colors and styles.
AString StringsConcat(const AStringVector &a_Strings, char a_Separator)
Concatenates the specified strings into a single string, separated by the specified separator charact...
AStringVector StringSplitWithQuotes(const AString &str, const AString &delim)
Split the string at any of the listed delimiters.
Definition: StringUtils.cpp:97
AString Base64Encode(const AString &a_Input)
Encodes a string into Base64.
T GetStringMapInteger(const AStringMap &a_Map, const AString &a_Key, T a_Default)
Returns a number (of any integer type T) from a key-value string map.
Definition: StringUtils.h:234
std::vector< AString > AStringVector
Definition: StringUtils.h:14
AString URLEncode(const AString &a_Text)
URL-encodes the given string.
AString & Printf(AString &a_Dst, const char *format, fmt::ArgList args)
Output the formatted text into the string.
Definition: StringUtils.cpp:55
AString & RawBEToUTF8(const char *a_RawData, size_t a_NumShorts, AString &a_UTF8)
Converts a stream of BE shorts into UTF-8 string; returns a_UTF8.
unsigned short GetBEUShort(const char *a_Mem)
Reads two bytes from the specified memory location and interprets them as BigEndian unsigned short...
AString StrToUpper(const AString &s)
Returns an upper-cased copy of the string.
int GetBEInt(const char *a_Mem)
Reads four bytes from the specified memory location and interprets them as BigEndian int...
bool IsOnlyWhitespace(const AString &a_String)
Returns true if only whitespace characters are present in the string.
AString & CreateHexDump(AString &a_Out, const void *a_Data, size_t a_Size, size_t a_BytesPerLine)
Creates a nicely formatted HEX dump of the given memory block.
void ReplaceString(AString &iHayStack, const AString &iNeedle, const AString &iReplaceWith)
Replaces each occurence of iNeedle in iHayStack with iReplaceWith.
AString & InPlaceUppercase(AString &s)
In-place string conversion to uppercase.
AString StringJoin(const AStringVector &a_Strings, const AString &a_Delimiter)
Join a list of strings with the given delimiter between entries.
AString & AppendPrintf(AString &a_Dst, const char *format, const Args &...args)
Add the formated string to the existing data in the string.
Definition: StringUtils.h:37
std::list< AString > AStringList
Definition: StringUtils.h:15
std::map< AString, AString > AStringMap
A string dictionary, used for key-value pairs.
Definition: StringUtils.h:18
AString EscapeString(const AString &a_Message)
Returns a copy of a_Message with all quotes and backslashes escaped by a backslash.
AString ReplaceAllCharOccurrences(const AString &a_String, char a_From, char a_To)
Replaces all occurrences of char a_From inside a_String with char a_To.
std::pair< bool, AString > URLDecode(const AString &a_String)
URL-Decodes the given string.
AString Base64Decode(const AString &a_Base64String)
Decodes a Base64-encoded string into the raw data.
AString StrToLower(const AString &s)
Returns a lower-cased copy of the string.
std::string AString
Definition: StringUtils.h:13
bool SplitZeroTerminatedStrings(const AString &a_Strings, AStringVector &a_Output)
Splits a string that has embedded \0 characters, on those characters.
signed int Int32
Definition: Globals.h:108
AString & InPlaceLowercase(AString &s)
In-place string conversion to lowercase.
void SetBEInt(char *a_Mem, Int32 a_Value)
Writes four bytes to the specified memory location so that they interpret as BigEndian int...
AString UnicodeCharToUtf8(unsigned a_UnicodeChar)
Converts a unicode character to its UTF8 representation.
short GetBEShort(const char *a_Mem)
Reads two bytes from the specified memory location and interprets them as BigEndian short...