1
0
Fork 0
cuberite-2a/src/StringUtils.h

199 lines
6.8 KiB
C
Raw Normal View History

// StringUtils.h
// Interfaces to various string helper functions
#pragma once
#include <string>
2014-08-13 12:15:29 +00:00
#include <limits>
typedef std::string AString;
typedef std::vector<AString> AStringVector;
typedef std::list<AString> AStringList;
2015-01-21 19:39:34 +00:00
/** Add the formated string to the existing data in the string.
Returns a_Dst. */
extern AString & AppendVPrintf(AString & a_Dst, const char * format, va_list args) FORMATSTRING(2, 0);
2015-01-21 19:39:34 +00:00
/** Output the formatted text into the string.
Returns a_Dst. */
extern AString & Printf (AString & a_Dst, const char * format, ...) FORMATSTRING(2, 3);
2015-01-21 19:39:34 +00:00
/** Output the formatted text into string
Returns the formatted string by value. */
2014-03-14 13:11:49 +00:00
extern AString Printf(const char * format, ...) FORMATSTRING(1, 2);
2015-01-21 19:39:34 +00:00
/** Add the formatted string to the existing data in the string.
Returns a_Dst */
extern AString & AppendPrintf (AString & a_Dst, const char * format, ...) FORMATSTRING(2, 3);
2015-01-21 19:39:34 +00:00
/** Split the string at any of the listed delimiters.
Return the splitted strings as a stringvector. */
extern AStringVector StringSplit(const AString & str, const AString & delim);
2015-03-11 03:14:17 +00:00
/** Split the string at any of the listed delimiters. Keeps quoted content together
Resolves issue #490
Return the splitted strings as a stringvector. */
extern AStringVector StringSplitWithQuotes(const AString & str, const AString & delim);
2015-01-21 19:39:34 +00:00
/** Split the string at any of the listed delimiters and trim each value.
Returns the splitted strings as a stringvector. */
extern AStringVector StringSplitAndTrim(const AString & str, const AString & delim);
2015-01-21 19:39:34 +00:00
/** Trims whitespace at both ends of the string.
Returns a trimmed copy of the original string. */
extern AString TrimString(const AString & str); // tolua_export
2015-01-21 19:39:34 +00:00
/** In-place string conversion to uppercase.
Returns the same string object. */
extern AString & InPlaceUppercase(AString & s);
2015-01-21 19:39:34 +00:00
/** In-place string conversion to lowercase.
Returns the same string object. */
extern AString & InPlaceLowercase(AString & s);
/** Returns an upper-cased copy of the string */
extern AString StrToUpper(const AString & s);
/** Returns a lower-cased copy of the string */
extern AString StrToLower(const AString & s);
2015-01-21 19:39:34 +00:00
/** Case-insensitive string comparison.
Returns 0 if the strings are the same, <0 if s1 < s2 and >0 if s1 > s2. */
extern int NoCaseCompare(const AString & s1, const AString & s2); // tolua_export
2015-01-21 19:39:34 +00:00
/** Case-insensitive string comparison that returns a rating of equal-ness between [0 - s1.length()]. */
2014-05-08 18:16:35 +00:00
extern size_t RateCompareString(const AString & s1, const AString & s2);
/** Replaces each occurence of iNeedle in iHayStack with iReplaceWith */
extern void ReplaceString(AString & iHayStack, const AString & iNeedle, const AString & iReplaceWith); // tolua_export
2015-01-21 19:39:34 +00:00
/** Converts a stream of BE shorts into UTF-8 string; returns a_UTF8. */
2014-05-08 18:16:35 +00:00
extern AString & RawBEToUTF8(const char * a_RawData, size_t a_NumShorts, AString & a_UTF8);
2015-01-21 19:39:34 +00:00
/** Converts a UTF-8 string into a UTF-16 BE string. */
2014-09-27 21:13:37 +00:00
extern AString UTF8ToRawBEUTF16(const char * a_UTF8, size_t a_UTF8Length);
2015-01-21 19:39:34 +00:00
/** Creates a nicely formatted HEX dump of the given memory block.
Max a_BytesPerLine is 120. */
2014-04-04 08:42:17 +00:00
extern AString & CreateHexDump(AString & a_Out, const void * a_Data, size_t a_Size, size_t a_BytesPerLine);
2015-01-21 19:39:34 +00:00
/** Returns a copy of a_Message with all quotes and backslashes escaped by a backslash. */
extern AString EscapeString(const AString & a_Message); // tolua_export
2015-01-21 19:39:34 +00:00
/** Removes all control codes used by MC for colors and styles. */
extern AString StripColorCodes(const AString & a_Message); // tolua_export
/// URL-Decodes the given string, replacing all "%HH" into the correct characters. Invalid % sequences are left intact
extern AString URLDecode(const AString & a_String); // Cannot export to Lua automatically - would generated an extra return value
/// Replaces all occurrences of char a_From inside a_String with char a_To.
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
/// Decodes a Base64-encoded string into the raw data
extern AString Base64Decode(const AString & a_Base64String); // Exported manually due to embedded NULs and extra parameter
2014-01-07 11:36:36 +00:00
/// Encodes a string into Base64
extern AString Base64Encode(const AString & a_Input); // Exported manually due to embedded NULs and extra parameter
2014-01-07 11:36:36 +00:00
/// Reads two bytes from the specified memory location and interprets them as BigEndian short
extern short GetBEShort(const char * a_Mem);
/// Reads four bytes from the specified memory location and interprets them as BigEndian int
extern int GetBEInt(const char * a_Mem);
/// Writes four bytes to the specified memory location so that they interpret as BigEndian int
extern void SetBEInt(char * a_Mem, Int32 a_Value);
/** Splits a string that has embedded \0 characters, on those characters.
a_Output is first cleared and then each separate string is pushed back into a_Output.
Returns true if there are at least two strings in a_Output (there was at least one \0 separator). */
extern bool SplitZeroTerminatedStrings(const AString & a_Strings, AStringVector & a_Output);
/** Merges the two vectors of strings, removing duplicate entries from the second vector.
The resulting vector contains items from a_Strings1 first, then from a_Strings2.
The order of items doesn't change, only the duplicates are removed.
If a_Strings1 contains duplicates, the result will still contain those duplicates. */
extern AStringVector MergeStringVectors(const AStringVector & a_Strings1, const AStringVector & a_Strings2);
/** Concatenates the specified strings into a single string, separated by the specified separator. */
extern AString StringsConcat(const AStringVector & a_Strings, char a_Separator);
/** Parses any integer type. Checks bounds and returns errors out of band. */
2014-08-28 13:44:36 +00:00
template <class T>
2014-08-29 08:20:23 +00:00
bool StringToInteger(const AString & a_str, T & a_Num)
{
size_t i = 0;
2014-08-13 12:37:07 +00:00
bool positive = true;
T result = 0;
if (a_str[0] == '+')
{
i++;
}
else if (a_str[0] == '-')
{
i++;
positive = false;
}
if (positive)
{
2014-08-28 13:58:48 +00:00
for (size_t size = a_str.size(); i < size; i++)
{
2014-08-28 22:42:33 +00:00
if ((a_str[i] < '0') || (a_str[i] > '9'))
{
return false;
}
if (std::numeric_limits<T>::max() / 10 < result)
{
return false;
}
result *= 10;
T digit = a_str[i] - '0';
if (std::numeric_limits<T>::max() - digit < result)
{
return false;
}
result += digit;
}
}
else
{
2014-08-28 13:58:48 +00:00
for (size_t size = a_str.size(); i < size; i++)
{
2014-08-28 22:42:33 +00:00
if ((a_str[i] < '0') || (a_str[i] > '9'))
{
return false;
}
if (std::numeric_limits<T>::min() / 10 > result)
{
return false;
}
result *= 10;
T digit = a_str[i] - '0';
if (std::numeric_limits<T>::min() + digit > result)
{
return false;
}
result -= digit;
}
}
a_Num = result;
return true;
}
// If you have any other string helper functions, declare them here