summaryrefslogtreecommitdiffstats
path: root/src/StringUtils.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/StringUtils.cpp93
1 files changed, 85 insertions, 8 deletions
diff --git a/src/StringUtils.cpp b/src/StringUtils.cpp
index f7aeeed26..0b38297ef 100644
--- a/src/StringUtils.cpp
+++ b/src/StringUtils.cpp
@@ -47,15 +47,13 @@ AString & AppendVPrintf(AString & str, const char *format, va_list args)
#endif // _MSC_VER
// Allocate a buffer and printf into it:
- str.resize(len + 1);
- // HACK: we're accessing AString's internal buffer in a way that is NOT guaranteed to always work. But it works on all STL implementations tested.
- // I can't think of any other way that is safe, doesn't allocate twice as much space as needed and doesn't use C++11 features like the move constructor
+ std::vector<char> Buffer(len + 1);
#ifdef _MSC_VER
- vsprintf_s((char *)str.data(), len + 1, format, args);
+ vsprintf_s((char *)&(Buffer.front()), Buffer.size(), format, args);
#else // _MSC_VER
- vsnprintf((char *)str.data(), len + 1, format, args);
+ vsnprintf((char *)&(Buffer.front()), Buffer.size(), format, args);
#endif // else _MSC_VER
- str.resize(len);
+ str.append(&(Buffer.front()), Buffer.size() - 1);
return str;
}
@@ -612,7 +610,7 @@ AString StripColorCodes(const AString & a_Message)
{
AString res(a_Message);
size_t idx = 0;
- while (true)
+ for (;;)
{
idx = res.find("\xc2\xa7", idx);
if (idx == AString::npos)
@@ -759,7 +757,86 @@ AString Base64Decode(const AString & a_Base64String)
}
}
res.resize(o >> 3);
- return res;}
+ return res;
+}
+
+
+
+
+
+AString Base64Encode(const AString & a_Input)
+{
+ static const char BASE64[64] = {
+ 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
+ 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
+ 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
+ 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
+ };
+
+ AString output;
+ output.resize(((a_Input.size() + 2) / 3) * 4);
+
+ size_t output_index = 0;
+ size_t size_full24 = (a_Input.size() / 3) * 3;
+
+ for (size_t i = 0; i < size_full24; i += 3)
+ {
+ output[output_index++] = BASE64[(unsigned char)a_Input[i] >> 2];
+ output[output_index++] = BASE64[((unsigned char)a_Input[i] << 4 | (unsigned char)a_Input[i + 1] >> 4) & 63];
+ output[output_index++] = BASE64[((unsigned char)a_Input[i + 1] << 2 | (unsigned char)a_Input[i + 2] >> 6) & 63];
+ output[output_index++] = BASE64[(unsigned char)a_Input[i + 2] & 63];
+ }
+
+ if (size_full24 < a_Input.size())
+ {
+ output[output_index++] = BASE64[(unsigned char)a_Input[size_full24] >> 2];
+ if (size_full24 + 1 == a_Input.size())
+ {
+ output[output_index++] = BASE64[((unsigned char)a_Input[size_full24] << 4) & 63];
+ output[output_index++] = '=';
+ }
+ else
+ {
+ output[output_index++] = BASE64[((unsigned char)a_Input[size_full24] << 4 | (unsigned char)a_Input[size_full24 + 1] >> 4) & 63];
+ output[output_index++] = BASE64[((unsigned char)a_Input[size_full24 + 1] << 2) & 63];
+ }
+
+ output[output_index++] = '=';
+ }
+ ASSERT(output_index == output.size());
+
+ return output;
+}
+
+
+
+
+
+short GetBEShort(const char * a_Mem)
+{
+ return (((short)a_Mem[0]) << 8) | a_Mem[1];
+}
+
+
+
+
+
+int GetBEInt(const char * a_Mem)
+{
+ return (((int)a_Mem[0]) << 24) | (((int)a_Mem[1]) << 16) | (((int)a_Mem[2]) << 8) | a_Mem[3];
+}
+
+
+
+
+
+void SetBEInt(char * a_Mem, Int32 a_Value)
+{
+ a_Mem[0] = a_Value >> 24;
+ a_Mem[1] = (a_Value >> 16) & 0xff;
+ a_Mem[2] = (a_Value >> 8) & 0xff;
+ a_Mem[3] = a_Value & 0xff;
+}