summaryrefslogtreecommitdiff
path: root/libs/utils/String16.cpp
diff options
context:
space:
mode:
author Jean-Baptiste Queru <jbq@google.com> 2009-03-18 11:33:14 -0700
committer Jean-Baptiste Queru <jbq@google.com> 2009-03-18 11:33:14 -0700
commit2a73de7b21a89aa2ba4c254d28658b49793425b2 (patch)
treeded5bcd581464b4174d81c373044b6d36eee58d2 /libs/utils/String16.cpp
parent42e48026b21a962e5bf40344d738665ecbd9d74d (diff)
parentba87e3e6c985e7175152993b5efcc7dd2f0e1c93 (diff)
Merge commit 'remotes/korg/cupcake' into merge
Conflicts: core/java/android/view/animation/TranslateAnimation.java core/jni/Android.mk core/res/res/values-en-rGB/strings.xml libs/audioflinger/AudioFlinger.cpp libs/surfaceflinger/LayerScreenshot.cpp packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
Diffstat (limited to 'libs/utils/String16.cpp')
-rw-r--r--libs/utils/String16.cpp22
1 files changed, 18 insertions, 4 deletions
diff --git a/libs/utils/String16.cpp b/libs/utils/String16.cpp
index 1f81cadb7a4c..aef67f22af95 100644
--- a/libs/utils/String16.cpp
+++ b/libs/utils/String16.cpp
@@ -244,7 +244,6 @@ void terminate_string16()
// ---------------------------------------------------------------------------
-// Note: not dealing with generating surrogate pairs.
static char16_t* allocFromUTF8(const char* in, size_t len)
{
if (len == 0) return getEmptyString();
@@ -255,7 +254,10 @@ static char16_t* allocFromUTF8(const char* in, size_t len)
while (p < end) {
chars++;
- p += utf8_char_len(*p);
+ int utf8len = utf8_char_len(*p);
+ uint32_t codepoint = utf8_to_utf32((const uint8_t*)p, utf8len);
+ if (codepoint > 0xFFFF) chars++; // this will be a surrogate pair in utf16
+ p += utf8len;
}
SharedBuffer* buf = SharedBuffer::alloc((chars+1)*sizeof(char16_t));
@@ -265,7 +267,19 @@ static char16_t* allocFromUTF8(const char* in, size_t len)
char16_t* d = str;
while (p < end) {
size_t len = utf8_char_len(*p);
- *d++ = (char16_t)utf8_to_utf32((const uint8_t*)p, len);
+ uint32_t codepoint = utf8_to_utf32((const uint8_t*)p, len);
+
+ // Convert the UTF32 codepoint to one or more UTF16 codepoints
+ if (codepoint <= 0xFFFF) {
+ // Single UTF16 character
+ *d++ = (char16_t) codepoint;
+ } else {
+ // Multiple UTF16 characters with surrogates
+ codepoint = codepoint - 0x10000;
+ *d++ = (char16_t) ((codepoint >> 10) + 0xD800);
+ *d++ = (char16_t) ((codepoint & 0x3FF) + 0xDC00);
+ }
+
p += len;
}
*d = 0;
@@ -388,7 +402,7 @@ status_t String16::setTo(const char16_t* other, size_t len)
->editResize((len+1)*sizeof(char16_t));
if (buf) {
char16_t* str = (char16_t*)buf->data();
- memcpy(str, other, len*sizeof(char16_t));
+ memmove(str, other, len*sizeof(char16_t));
str[len] = 0;
mString = str;
return NO_ERROR;