1
0

Added URLDecode() and ReplaceAllCharOccurrences() to StringUtils.

This commit is contained in:
madmaxoft 2013-09-28 19:28:19 +02:00
parent c22ea7efff
commit 3b473f7a67
2 changed files with 76 additions and 0 deletions

View File

@ -658,3 +658,73 @@ AString StripColorCodes(const AString & a_Message)
AString URLDecode(const AString & a_String)
{
AString res;
size_t len = a_String.length();
res.reserve(len);
for (size_t i = 0; i < len; i++)
{
char ch = a_String[i];
if ((ch != '%') || (i > len - 3))
{
res.push_back(ch);
continue;
}
// Decode the hex value:
char hi = a_String[i + 1], lo = a_String[i + 2];
if ((hi >= '0') && (hi <= '9'))
{
hi = hi - '0';
}
else if ((hi >= 'a') && (hi <= 'f'))
{
hi = hi - 'a' + 10;
}
else if ((hi >= 'A') && (hi <= 'F'))
{
hi = hi - 'F' + 10;
}
else
{
res.push_back(ch);
continue;
}
if ((lo >= '0') && (lo <= '9'))
{
lo = lo - '0';
}
else if ((lo >= 'a') && (lo <= 'f'))
{
lo = lo - 'a' + 10;
}
else if ((lo >= 'A') && (lo <= 'F'))
{
lo = lo - 'A' + 10;
}
else
{
res.push_back(ch);
continue;
}
res.push_back((hi << 4) | lo);
i += 2;
} // for i - a_String[]
return res;
}
AString ReplaceAllCharOccurrences(const AString & a_String, char a_From, char a_To)
{
AString res(a_String);
std::replace(res.begin(), res.end(), a_From, a_To);
return res;
}

View File

@ -72,6 +72,12 @@ extern AString EscapeString(const AString & a_Message); // tolua_export
/// Removes all control codes used by MC for colors and styles /// Removes all control codes used by MC for colors and styles
extern AString StripColorCodes(const AString & a_Message); // tolua_export 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
// If you have any other string helper functions, declare them here // If you have any other string helper functions, declare them here