Cuberite
A lightweight, fast and extensible game server for Minecraft
Macros | Functions | Variables
StringUtils.cpp File Reference
#include "Globals.h"
#include "fmt/printf.h"
Include dependency graph for StringUtils.cpp:

Go to the source code of this file.

Macros

#define HEX(x)   static_cast<char>((x) > 9 ? (x) + 'A' - 10 : (x) + '0')
 
#define UNI_MAX_BMP   0x0000FFFF
 
#define UNI_MAX_UTF16   0x0010FFFF
 
#define UNI_SUR_HIGH_START   0xD800
 
#define UNI_SUR_LOW_END   0xDFFF
 
#define UNI_SUR_LOW_START   0xDC00
 

Functions

AString Base64Decode (const AString &a_Base64String)
 Decodes a Base64-encoded string into the raw data. More...
 
AString Base64Encode (const AString &a_Input)
 Encodes a string into Base64. More...
 
AStringCreateHexDump (AString &a_Out, const void *a_Data, size_t a_Size, size_t a_BytesPerLine)
 format binary data this way: 00001234: 31 32 33 34 35 36 37 38 39 30 61 62 63 64 65 66 1234567890abcdef More...
 
AString EscapeString (const AString &a_Message)
 Returns a copy of a_Message with all quotes and backslashes escaped by a backslash. More...
 
int GetBEInt (const std::byte *const a_Mem)
 Reads four bytes from the specified memory location and interprets them as BigEndian int. More...
 
short GetBEShort (const std::byte *const a_Mem)
 Reads two bytes from the specified memory location and interprets them as BigEndian short. More...
 
unsigned short GetBEUShort (const char *a_Mem)
 Reads two bytes from the specified memory location and interprets them as BigEndian unsigned short. More...
 
static unsigned char HexToDec (char a_HexChar)
 Returns the value of the single hex digit. More...
 
AStringInPlaceLowercase (AString &s)
 In-place string conversion to lowercase. More...
 
AStringInPlaceUppercase (AString &s)
 In-place string conversion to uppercase. More...
 
static bool isLegalUTF8 (const unsigned char *source, int length)
 
bool IsOnlyWhitespace (const AString &a_String)
 Returns true if only whitespace characters are present in the string. More...
 
AStringVector MergeStringVectors (const AStringVector &a_Strings1, const AStringVector &a_Strings2)
 Merges the two vectors of strings, removing duplicate entries from the second vector. More...
 
int NoCaseCompare (const AString &s1, const AString &s2)
 Case-insensitive string comparison. More...
 
size_t RateCompareString (const AString &s1, const AString &s2)
 Case-insensitive string comparison that returns a rating of equal-ness between [0 - s1.length()]. More...
 
AStringRawBEToUTF8 (const char *a_RawData, size_t a_NumShorts, AString &a_UTF8)
 Converts a stream of BE shorts into UTF-8 string; returns a_UTF8. More...
 
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. More...
 
void ReplaceString (AString &iHayStack, const AString &iNeedle, const AString &iReplaceWith)
 Replaces each occurence of iNeedle in iHayStack with iReplaceWith. More...
 
void ReplaceURL (AString &iHayStack, const AString &iNeedle, const AString &iReplaceWith)
 Replaces each occurence of iNeedle in iHayStack with iReplaceWith, after URL-encoding iReplaceWith. More...
 
void SetBEInt (std::byte *a_Mem, Int32 a_Value)
 Writes four bytes to the specified memory location so that they interpret as BigEndian int. More...
 
bool SplitZeroTerminatedStrings (const AString &a_Strings, AStringVector &a_Output)
 Splits a string that has embedded \0 characters, on those characters. More...
 
AString StringJoin (const AStringVector &a_Strings, const AString &a_Delimeter)
 Join a list of strings with the given delimiter between entries. More...
 
AString StringsConcat (const AStringVector &a_Strings, char a_Separator)
 Concatenates the specified strings into a single string, separated by the specified separator character. More...
 
AStringVector StringSplit (const AString &str, const AString &delim)
 Split the string at any of the listed delimiters. More...
 
AStringVector StringSplitAndTrim (const AString &str, const AString &delim)
 Split the string at any of the listed delimiters and trim each value. More...
 
AStringVector StringSplitWithQuotes (const AString &str, const AString &delim)
 Split the string at any of the listed delimiters. More...
 
bool StringToFloat (const AString &a_String, float &a_Num)
 Converts a string into a float. More...
 
AString StripColorCodes (const AString &a_Message)
 Removes all control codes used by MC for colors and styles. More...
 
AString StrToLower (const AString &s)
 Returns a lower-cased copy of the string. More...
 
AString StrToUpper (const AString &s)
 Returns an upper-cased copy of the string. More...
 
AString TrimString (const AString &str)
 Trims whitespace at both ends of the string. More...
 
static int UnBase64 (char c)
 Converts one Hex character in a Base64 encoding into the data value. More...
 
AString UnicodeCharToUtf8 (unsigned a_UnicodeChar)
 Converts a unicode character to its UTF8 representation. More...
 
std::pair< bool, AStringURLDecode (const AString &a_Text)
 URL-Decodes the given string. More...
 
AString URLEncode (const AString &a_Text)
 URL-encodes the given string. More...
 
std::u16string UTF8ToRawBEUTF16 (const AString &a_UTF8)
 Converts a UTF-8 string into a UTF-16 BE string. More...
 

Variables

static const unsigned int offsetsFromUTF8 [6]
 
static const Byte trailingBytesForUTF8 [256]
 

Macro Definition Documentation

◆ HEX

#define HEX (   x)    static_cast<char>((x) > 9 ? (x) + 'A' - 10 : (x) + '0')

Definition at line 610 of file StringUtils.cpp.

◆ UNI_MAX_BMP

#define UNI_MAX_BMP   0x0000FFFF

Definition at line 440 of file StringUtils.cpp.

◆ UNI_MAX_UTF16

#define UNI_MAX_UTF16   0x0010FFFF

Definition at line 441 of file StringUtils.cpp.

◆ UNI_SUR_HIGH_START

#define UNI_SUR_HIGH_START   0xD800

Definition at line 442 of file StringUtils.cpp.

◆ UNI_SUR_LOW_END

#define UNI_SUR_LOW_END   0xDFFF

Definition at line 444 of file StringUtils.cpp.

◆ UNI_SUR_LOW_START

#define UNI_SUR_LOW_START   0xDC00

Definition at line 443 of file StringUtils.cpp.

Function Documentation

◆ Base64Decode()

AString Base64Decode ( const AString a_Base64String)

Decodes a Base64-encoded string into the raw data.

Definition at line 864 of file StringUtils.cpp.

◆ Base64Encode()

AString Base64Encode ( const AString a_Input)

Encodes a string into Base64.

Definition at line 900 of file StringUtils.cpp.

◆ CreateHexDump()

AString& CreateHexDump ( AString a_Out,
const void *  a_Data,
size_t  a_Size,
size_t  a_BytesPerLine 
)

format binary data this way: 00001234: 31 32 33 34 35 36 37 38 39 30 61 62 63 64 65 66 1234567890abcdef

Creates a nicely formatted HEX dump of the given memory block.

Definition at line 616 of file StringUtils.cpp.

◆ EscapeString()

AString EscapeString ( const AString a_Message)

Returns a copy of a_Message with all quotes and backslashes escaped by a backslash.

Definition at line 663 of file StringUtils.cpp.

◆ GetBEInt()

int GetBEInt ( const std::byte *const  a_Mem)

Reads four bytes from the specified memory location and interprets them as BigEndian int.

Definition at line 971 of file StringUtils.cpp.

◆ GetBEShort()

short GetBEShort ( const std::byte *const  a_Mem)

Reads two bytes from the specified memory location and interprets them as BigEndian short.

Definition at line 949 of file StringUtils.cpp.

◆ GetBEUShort()

unsigned short GetBEUShort ( const char *  a_Mem)

Reads two bytes from the specified memory location and interprets them as BigEndian unsigned short.

Definition at line 961 of file StringUtils.cpp.

◆ HexToDec()

static unsigned char HexToDec ( char  a_HexChar)
static

Returns the value of the single hex digit.

Returns 0xff on failure.

Definition at line 21 of file StringUtils.cpp.

◆ InPlaceLowercase()

AString& InPlaceLowercase ( AString s)

In-place string conversion to lowercase.

Returns the same string object.

Definition at line 224 of file StringUtils.cpp.

◆ InPlaceUppercase()

AString& InPlaceUppercase ( AString s)

In-place string conversion to uppercase.

Returns the same string object.

Definition at line 234 of file StringUtils.cpp.

◆ isLegalUTF8()

static bool isLegalUTF8 ( const unsigned char *  source,
int  length 
)
static

Definition at line 476 of file StringUtils.cpp.

◆ IsOnlyWhitespace()

bool IsOnlyWhitespace ( const AString a_String)

Returns true if only whitespace characters are present in the string.

Definition at line 1080 of file StringUtils.cpp.

◆ MergeStringVectors()

AStringVector MergeStringVectors ( const AStringVector a_Strings1,
const AStringVector a_Strings2 
)

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.

Definition at line 1025 of file StringUtils.cpp.

◆ NoCaseCompare()

int NoCaseCompare ( const AString s1,
const AString s2 
)

Case-insensitive string comparison.

Returns 0 if the strings are the same, <0 if s1 < s2 and >0 if s1 > s2.

Definition at line 268 of file StringUtils.cpp.

◆ RateCompareString()

size_t RateCompareString ( const AString s1,
const AString s2 
)

Case-insensitive string comparison that returns a rating of equal-ness between [0 - s1.length()].

Definition at line 281 of file StringUtils.cpp.

◆ 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 at line 342 of file StringUtils.cpp.

◆ 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 at line 819 of file StringUtils.cpp.

◆ ReplaceString()

void ReplaceString ( AString iHayStack,
const AString iNeedle,
const AString iReplaceWith 
)

Replaces each occurence of iNeedle in iHayStack with iReplaceWith.

Definition at line 312 of file StringUtils.cpp.

◆ 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 at line 332 of file StringUtils.cpp.

◆ 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 at line 985 of file StringUtils.cpp.

◆ SplitZeroTerminatedStrings()

bool SplitZeroTerminatedStrings ( const AString a_Strings,
AStringVector a_Output 
)

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).

Definition at line 997 of file StringUtils.cpp.

◆ StringJoin()

AString StringJoin ( const AStringVector a_Strings,
const AString a_Delimeter 
)

Join a list of strings with the given delimiter between entries.

Definition at line 133 of file StringUtils.cpp.

◆ StringsConcat()

AString StringsConcat ( const AStringVector a_Strings,
char  a_Separator 
)

Concatenates the specified strings into a single string, separated by the specified separator character.

Use StringJoin() if you need multiple separator characters.

Definition at line 1046 of file StringUtils.cpp.

◆ StringSplit()

AStringVector StringSplit ( const AString str,
const AString delim 
)

Split the string at any of the listed delimiters.

Return the splitted strings as a stringvector.

Definition at line 55 of file StringUtils.cpp.

◆ StringSplitAndTrim()

AStringVector StringSplitAndTrim ( const AString str,
const AString delim 
)

Split the string at any of the listed delimiters and trim each value.

Returns the splitted strings as a stringvector.

Definition at line 169 of file StringUtils.cpp.

◆ StringSplitWithQuotes()

AStringVector StringSplitWithQuotes ( const AString str,
const AString delim 
)

Split the string at any of the listed delimiters.

Keeps quoted content together Resolves issue #490 Return the splitted strings as a stringvector.

Definition at line 76 of file StringUtils.cpp.

◆ StringToFloat()

bool StringToFloat ( const AString a_String,
float &  a_Num 
)

Converts a string into a float.

Returns false if the conversion fails.

Definition at line 1069 of file StringUtils.cpp.

◆ StripColorCodes()

AString StripColorCodes ( const AString a_Message)

Removes all control codes used by MC for colors and styles.

Definition at line 700 of file StringUtils.cpp.

◆ StrToLower()

AString StrToLower ( const AString s)

Returns a lower-cased copy of the string.

Definition at line 244 of file StringUtils.cpp.

◆ StrToUpper()

AString StrToUpper ( const AString s)

Returns an upper-cased copy of the string.

Definition at line 256 of file StringUtils.cpp.

◆ TrimString()

AString TrimString ( const AString str)

Trims whitespace at both ends of the string.

Returns a trimmed copy of the original string.

Definition at line 190 of file StringUtils.cpp.

◆ UnBase64()

static int UnBase64 ( char  c)
inlinestatic

Converts one Hex character in a Base64 encoding into the data value.

Definition at line 831 of file StringUtils.cpp.

◆ UnicodeCharToUtf8()

AString UnicodeCharToUtf8 ( unsigned  a_UnicodeChar)

Converts a unicode character to its UTF8 representation.

Definition at line 357 of file StringUtils.cpp.

◆ URLDecode()

std::pair<bool, AString> URLDecode ( const AString a_String)

URL-Decodes the given string.

The first value specifies whether the decoding was successful. The second value is the decoded string, if successful.

Definition at line 719 of file StringUtils.cpp.

◆ URLEncode()

AString URLEncode ( const AString a_Text)

URL-encodes the given string.

Definition at line 789 of file StringUtils.cpp.

◆ UTF8ToRawBEUTF16()

std::u16string UTF8ToRawBEUTF16 ( const AString a_UTF8)

Converts a UTF-8 string into a UTF-16 BE string.

Definition at line 511 of file StringUtils.cpp.

Variable Documentation

◆ offsetsFromUTF8

const unsigned int offsetsFromUTF8[6]
static
Initial value:
=
{
0x00000000UL, 0x00003080UL, 0x000E2080UL,
0x03C82080UL, 0xFA082080UL, 0x82082080UL
}

Definition at line 466 of file StringUtils.cpp.

◆ trailingBytesForUTF8

const Byte trailingBytesForUTF8[256]
static
Initial value:
=
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5
}

Definition at line 450 of file StringUtils.cpp.