summaryrefslogtreecommitdiffstats
path: root/source/StringUtils.cpp
diff options
context:
space:
mode:
authormadmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6>2012-08-17 12:18:07 +0200
committermadmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6>2012-08-17 12:18:07 +0200
commit70a4ca5bc196676e8926028b2187fe7bd2874d42 (patch)
tree38056355c35459c35802f93eca974a20a07d4623 /source/StringUtils.cpp
parentMCServer should run just fine on Android now :D (diff)
downloadcuberite-70a4ca5bc196676e8926028b2187fe7bd2874d42.tar
cuberite-70a4ca5bc196676e8926028b2187fe7bd2874d42.tar.gz
cuberite-70a4ca5bc196676e8926028b2187fe7bd2874d42.tar.bz2
cuberite-70a4ca5bc196676e8926028b2187fe7bd2874d42.tar.lz
cuberite-70a4ca5bc196676e8926028b2187fe7bd2874d42.tar.xz
cuberite-70a4ca5bc196676e8926028b2187fe7bd2874d42.tar.zst
cuberite-70a4ca5bc196676e8926028b2187fe7bd2874d42.zip
Diffstat (limited to 'source/StringUtils.cpp')
-rw-r--r--source/StringUtils.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/source/StringUtils.cpp b/source/StringUtils.cpp
index fa61a61e7..ccdbe687a 100644
--- a/source/StringUtils.cpp
+++ b/source/StringUtils.cpp
@@ -233,3 +233,49 @@ AStringList GetDirectoryContents(const char * a_Directory)
+
+// Converts a stream of BE shorts into UTF-8 string; returns a ref to a_UTF8
+AString & RawBEToUTF8(short * a_RawData, int a_NumShorts, AString & a_UTF8)
+{
+ a_UTF8.clear();
+ a_UTF8.reserve(3 * a_NumShorts / 2); // a quick guess of the resulting size
+ for (int i = 0; i < a_NumShorts; i++)
+ {
+ int c = ntohs(*(a_RawData + i));
+ if (c < 0x80)
+ {
+ a_UTF8.push_back((char)c);
+ }
+ else if (c < 0x800)
+ {
+ a_UTF8.push_back((char)(192 + c / 64));
+ a_UTF8.push_back((char)(128 + c % 64));
+ }
+ else if (c - 0xd800u < 0x800)
+ {
+ // Error, silently drop
+ }
+ else if (c < 0x10000)
+ {
+ a_UTF8.push_back((char)(224 + c / 4096));
+ a_UTF8.push_back((char)(128 + c / 64 % 64));
+ a_UTF8.push_back((char)(128 + c % 64));
+ }
+ else if (c < 0x110000)
+ {
+ a_UTF8.push_back((char)(240 + c / 262144));
+ a_UTF8.push_back((char)(128 + c / 4096 % 64));
+ a_UTF8.push_back((char)(128 + c / 64 % 64));
+ a_UTF8.push_back((char)(128 + c % 64));
+ }
+ else
+ {
+ // Error, silently drop
+ }
+ }
+ return a_UTF8;
+}
+
+
+
+