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 typedef std::string AString;
12 typedef std::vector<AString> AStringVector;
13 typedef std::list<AString> AStringList;
14 
16 typedef std::map<AString, AString> AStringMap;
17 
18 
19 
20 
21 
24 extern AString & vPrintf(AString & a_Dst, const char * a_Format, fmt::printf_args a_ArgList);
25 template <typename... Args>
26 AString & Printf(AString & a_Dst, const char * a_Format, const Args & ... a_Args)
27 {
28  return vPrintf(a_Dst, a_Format, fmt::make_printf_args(a_Args...));
29 }
30 
33 extern AString vPrintf(const char * a_Format, fmt::printf_args a_ArgList);
34 template <typename... Args>
35 AString Printf(const char * a_Format, const Args & ... a_Args)
36 {
37  return vPrintf(a_Format, fmt::make_printf_args(a_Args...));
38 }
39 
42 extern AString & vAppendPrintf(AString & a_Dst, const char * a_Format, fmt::printf_args a_ArgList);
43 template <typename... Args>
44 extern AString & AppendPrintf(AString & a_Dst, const char * a_Format, const Args & ... a_Args)
45 {
46  return vAppendPrintf(a_Dst, a_Format, fmt::make_printf_args(a_Args...));
47 }
48 
51 extern AStringVector StringSplit(const AString & str, const AString & delim);
52 
56 extern AStringVector StringSplitWithQuotes(const AString & str, const AString & delim);
57 
59 AString StringJoin(const AStringVector & a_Strings, const AString & a_Delimiter);
60 
63 extern AStringVector StringSplitAndTrim(const AString & str, const AString & delim);
64 
67 extern AString TrimString(const AString & str); // tolua_export
68 
71 extern AString & InPlaceUppercase(AString & s);
72 
75 extern AString & InPlaceLowercase(AString & s);
76 
78 extern AString StrToUpper(const AString & s);
79 
81 extern AString StrToLower(const AString & s);
82 
85 extern int NoCaseCompare(const AString & s1, const AString & s2); // tolua_export
86 
88 extern size_t RateCompareString(const AString & s1, const AString & s2);
89 
91 extern void ReplaceString(AString & iHayStack, const AString & iNeedle, const AString & iReplaceWith); // tolua_export
92 
94 extern void ReplaceURL(AString & iHayStack, const AString & iNeedle, const AString & iReplaceWith);
95 
97 extern AString & RawBEToUTF8(const char * a_RawData, size_t a_NumShorts, AString & a_UTF8);
98 
100 extern AString UnicodeCharToUtf8(unsigned a_UnicodeChar);
101 
103 extern std::u16string UTF8ToRawBEUTF16(const AString & a_String);
104 
106 extern AString & CreateHexDump(AString & a_Out, const void * a_Data, size_t a_Size, size_t a_BytesPerLine);
107 
109 extern AString EscapeString(const AString & a_Message); // tolua_export
110 
112 extern AString StripColorCodes(const AString & a_Message); // tolua_export
113 
117 extern std::pair<bool, AString> URLDecode(const AString & a_String); // Exported to Lua as cUrlParser::UrlDecode()
118 
120 extern AString URLEncode(const AString & a_Text);
121 
123 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
124 
126 extern AString Base64Decode(const AString & a_Base64String); // Exported manually due to embedded NULs and extra parameter
127 
129 extern AString Base64Encode(const AString & a_Input); // Exported manually due to embedded NULs and extra parameter
130 
132 extern short GetBEShort(const std::byte * a_Mem);
133 
135 extern unsigned short GetBEUShort(const char * a_Mem);
136 
138 extern int GetBEInt(const std::byte * a_Mem);
139 
141 extern void SetBEInt(std::byte * a_Mem, Int32 a_Value);
142 
146 extern bool SplitZeroTerminatedStrings(const AString & a_Strings, AStringVector & a_Output);
147 
152 extern AStringVector MergeStringVectors(const AStringVector & a_Strings1, const AStringVector & a_Strings2);
153 
156 extern AString StringsConcat(const AStringVector & a_Strings, char a_Separator);
157 
159 extern bool StringToFloat(const AString & a_String, float & a_Num);
160 
162 bool IsOnlyWhitespace(const AString & a_String);
163 
164 
165 
166 
167 
169 template <class T>
170 bool StringToInteger(const AString & a_str, T & a_Num)
171 {
172  size_t i = 0;
173  bool positive = true;
174  T result = 0;
175  if (a_str[0] == '+')
176  {
177  i++;
178  }
179  else if (a_str[0] == '-')
180  {
181  i++;
182  positive = false;
183  }
184  if (positive)
185  {
186  for (size_t size = a_str.size(); i < size; i++)
187  {
188  if ((a_str[i] < '0') || (a_str[i] > '9'))
189  {
190  return false;
191  }
192  if (std::numeric_limits<T>::max() / 10 < result)
193  {
194  return false;
195  }
196  result *= 10;
197  T digit = static_cast<T>(a_str[i] - '0');
198  if (std::numeric_limits<T>::max() - digit < result)
199  {
200  return false;
201  }
202  result += digit;
203  }
204  }
205  else
206  {
207  // Unsigned result cannot be signed!
208  if (!std::numeric_limits<T>::is_signed)
209  {
210  return false;
211  }
212 
213  for (size_t size = a_str.size(); i < size; i++)
214  {
215  if ((a_str[i] < '0') || (a_str[i] > '9'))
216  {
217  return false;
218  }
219  if (std::numeric_limits<T>::min() / 10 > result)
220  {
221  return false;
222  }
223  result *= 10;
224  T digit = static_cast<T>(a_str[i] - '0');
225  if (std::numeric_limits<T>::min() + digit > result)
226  {
227  return false;
228  }
229  result -= digit;
230  }
231  }
232  a_Num = result;
233  return true;
234 }
235 
236 
237 
238 
239 
242 template <typename T>
243 T GetStringMapInteger(const AStringMap & a_Map, const AString & a_Key, T a_Default)
244 {
245  // Try to locate the key:
246  auto itr = a_Map.find(a_Key);
247  if (itr == a_Map.end())
248  {
249  return a_Default;
250  }
251 
252  // Try to convert the value to a number:
253  T res = a_Default;
254  if (!StringToInteger<T>(itr->second, res))
255  {
256  return a_Default;
257  }
258  return res;
259 }
260 
261 
262 
263 
264 
265 // If you have any other string helper functions, declare them here
266 
267 
268 
269 
AppendPrintf
AString & AppendPrintf(AString &a_Dst, const char *a_Format, const Args &... a_Args)
Definition: StringUtils.h:44
SplitZeroTerminatedStrings
bool SplitZeroTerminatedStrings(const AString &a_Strings, AStringVector &a_Output)
Splits a string that has embedded \0 characters, on those characters.
Definition: StringUtils.cpp:1033
AStringMap
std::map< AString, AString > AStringMap
A string dictionary, used for key-value pairs.
Definition: StringUtils.h:16
IsOnlyWhitespace
bool IsOnlyWhitespace(const AString &a_String)
Returns true if only whitespace characters are present in the string.
Definition: StringUtils.cpp:1116
vAppendPrintf
AString & vAppendPrintf(AString &a_Dst, const char *a_Format, fmt::printf_args a_ArgList)
Add the formated string to the existing data in the string.
Definition: StringUtils.cpp:78
MergeStringVectors
AStringVector MergeStringVectors(const AStringVector &a_Strings1, const AStringVector &a_Strings2)
Merges the two vectors of strings, removing duplicate entries from the second vector.
Definition: StringUtils.cpp:1061
GetBEShort
short GetBEShort(const std::byte *a_Mem)
Reads two bytes from the specified memory location and interprets them as BigEndian short.
Definition: StringUtils.cpp:985
RateCompareString
size_t RateCompareString(const AString &s1, const AString &s2)
Case-insensitive string comparison that returns a rating of equal-ness between [0 - s1....
Definition: StringUtils.cpp:317
GetBEInt
int GetBEInt(const std::byte *a_Mem)
Reads four bytes from the specified memory location and interprets them as BigEndian int.
Definition: StringUtils.cpp:1007
URLEncode
AString URLEncode(const AString &a_Text)
URL-encodes the given string.
Definition: StringUtils.cpp:825
InPlaceLowercase
AString & InPlaceLowercase(AString &s)
In-place string conversion to lowercase.
Definition: StringUtils.cpp:260
InPlaceUppercase
AString & InPlaceUppercase(AString &s)
In-place string conversion to uppercase.
Definition: StringUtils.cpp:270
ReplaceString
void ReplaceString(AString &iHayStack, const AString &iNeedle, const AString &iReplaceWith)
Replaces each occurence of iNeedle in iHayStack with iReplaceWith.
Definition: StringUtils.cpp:348
StringToFloat
bool StringToFloat(const AString &a_String, float &a_Num)
Converts a string into a float.
Definition: StringUtils.cpp:1105
UTF8ToRawBEUTF16
std::u16string UTF8ToRawBEUTF16(const AString &a_String)
Converts a UTF-8 string into a UTF-16 BE string.
Definition: StringUtils.cpp:547
AStringList
std::list< AString > AStringList
Definition: StringUtils.h:13
CreateHexDump
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.
Definition: StringUtils.cpp:652
StringSplitWithQuotes
AStringVector StringSplitWithQuotes(const AString &str, const AString &delim)
Split the string at any of the listed delimiters.
Definition: StringUtils.cpp:112
StrToLower
AString StrToLower(const AString &s)
Returns a lower-cased copy of the string.
Definition: StringUtils.cpp:280
SetBEInt
void SetBEInt(std::byte *a_Mem, Int32 a_Value)
Writes four bytes to the specified memory location so that they interpret as BigEndian int.
Definition: StringUtils.cpp:1021
StringSplit
AStringVector StringSplit(const AString &str, const AString &delim)
Split the string at any of the listed delimiters.
Definition: StringUtils.cpp:91
NoCaseCompare
int NoCaseCompare(const AString &s1, const AString &s2)
Case-insensitive string comparison.
Definition: StringUtils.cpp:304
StringToInteger
bool StringToInteger(const AString &a_str, T &a_Num)
Parses any integer type.
Definition: StringUtils.h:170
URLDecode
std::pair< bool, AString > URLDecode(const AString &a_String)
URL-Decodes the given string.
Definition: StringUtils.cpp:755
ReplaceAllCharOccurrences
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.
Definition: StringUtils.cpp:855
ReplaceURL
void ReplaceURL(AString &iHayStack, const AString &iNeedle, const AString &iReplaceWith)
Replaces each occurence of iNeedle in iHayStack with iReplaceWith, after URL-encoding iReplaceWith.
Definition: StringUtils.cpp:368
StringJoin
AString StringJoin(const AStringVector &a_Strings, const AString &a_Delimiter)
Join a list of strings with the given delimiter between entries.
Definition: StringUtils.cpp:169
Int32
signed int Int32
Definition: Globals.h:149
TrimString
AString TrimString(const AString &str)
Trims whitespace at both ends of the string.
Definition: StringUtils.cpp:226
vPrintf
AString & vPrintf(AString &a_Dst, const char *a_Format, fmt::printf_args a_ArgList)
Output the formatted text into the string.
Definition: StringUtils.cpp:55
GetBEUShort
unsigned short GetBEUShort(const char *a_Mem)
Reads two bytes from the specified memory location and interprets them as BigEndian unsigned short.
Definition: StringUtils.cpp:997
RawBEToUTF8
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.
Definition: StringUtils.cpp:378
Base64Encode
AString Base64Encode(const AString &a_Input)
Encodes a string into Base64.
Definition: StringUtils.cpp:936
Printf
AString & Printf(AString &a_Dst, const char *a_Format, const Args &... a_Args)
Definition: StringUtils.h:26
StringSplitAndTrim
AStringVector StringSplitAndTrim(const AString &str, const AString &delim)
Split the string at any of the listed delimiters and trim each value.
Definition: StringUtils.cpp:205
UnicodeCharToUtf8
AString UnicodeCharToUtf8(unsigned a_UnicodeChar)
Converts a unicode character to its UTF8 representation.
Definition: StringUtils.cpp:393
StripColorCodes
AString StripColorCodes(const AString &a_Message)
Removes all control codes used by MC for colors and styles.
Definition: StringUtils.cpp:736
StringsConcat
AString StringsConcat(const AStringVector &a_Strings, char a_Separator)
Concatenates the specified strings into a single string, separated by the specified separator charact...
Definition: StringUtils.cpp:1082
Base64Decode
AString Base64Decode(const AString &a_Base64String)
Decodes a Base64-encoded string into the raw data.
Definition: StringUtils.cpp:900
EscapeString
AString EscapeString(const AString &a_Message)
Returns a copy of a_Message with all quotes and backslashes escaped by a backslash.
Definition: StringUtils.cpp:699
StrToUpper
AString StrToUpper(const AString &s)
Returns an upper-cased copy of the string.
Definition: StringUtils.cpp:292
GetStringMapInteger
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:243
AString
std::string AString
Definition: StringUtils.h:11
AStringVector
std::vector< AString > AStringVector
Definition: StringUtils.h:12