Index: runtime/vm/unicode.cc |
diff --git a/runtime/vm/unicode.cc b/runtime/vm/unicode.cc |
index 42cd42607c29a196d4db08e6fd6ea335426be1de..620a76790129b94f7d4a801e6bb187291806b644 100644 |
--- a/runtime/vm/unicode.cc |
+++ b/runtime/vm/unicode.cc |
@@ -65,20 +65,15 @@ static bool IsAsciiSequenceStart(uint8_t code_unit) { |
static bool IsSmpSequenceStart(uint8_t code_unit) { |
- // Check is codepoint is >= U+10000. |
+ // Check the UTF-8 code unit to determine if it is a sequence start for a |
+ // code point >= U+10000. |
return (code_unit >= 0xF0); |
} |
-// Returns true if the code point is a high- or low-surrogate. |
-static bool IsSurrogate(uint32_t code_point) { |
- return (code_point & 0xfffff800) == 0xd800; |
-} |
- |
- |
// Returns true if the code point value is above Plane 17. |
static bool IsOutOfRange(uint32_t code_point) { |
- return (code_point > 0x10FFFF); |
+ return (code_point > Utf16::kMaxCodePoint); |
} |
@@ -88,18 +83,12 @@ static bool IsNonShortestForm(uint32_t code_point, size_t num_bytes) { |
} |
-void Utf8::ConvertUTF32ToUTF16(int32_t codepoint, uint16_t* dst) { |
- ASSERT(codepoint > kMaxBmpCodepoint); |
- ASSERT(dst != NULL); |
- dst[0] = (Utf8::kLeadOffset + (codepoint >> 10)); |
- dst[1] = (0xDC00 + (codepoint & 0x3FF)); |
-} |
- |
- |
-// Returns a count of the number of UTF-8 trail bytes. |
-intptr_t Utf8::CodePointCount(const uint8_t* utf8_array, |
- intptr_t array_len, |
- Type* type) { |
+// Returns a count of the number of UTF-16 code units represented by this UTF-8 |
+// array. Type is kASCII for 7-bit-only. If there are surrogate pairs then |
+// the type is kSMP. Otherwise it is kBMP. |
+intptr_t Utf8::CodeUnitCount(const uint8_t* utf8_array, |
+ intptr_t array_len, |
+ Type* type) { |
intptr_t len = 0; |
Type char_type = kAscii; |
for (intptr_t i = 0; i < array_len; i++) { |
@@ -110,7 +99,7 @@ intptr_t Utf8::CodePointCount(const uint8_t* utf8_array, |
if (!IsAsciiSequenceStart(code_unit)) { // > U+007F |
if (IsSmpSequenceStart(code_unit)) { // >= U+10000 |
char_type = kSMP; |
- ++len; |
+ ++len; // Surrogate pair in the UTF-16 encoding. |
} else if (char_type == kAscii) { |
char_type = kBMP; |
} |
@@ -121,7 +110,7 @@ intptr_t Utf8::CodePointCount(const uint8_t* utf8_array, |
} |
-// Returns true if str is a valid NUL-terminated UTF-8 string. |
+// Returns true if str is a valid UTF-8 string. |
bool Utf8::IsValid(const uint8_t* utf8_array, intptr_t array_len) { |
intptr_t i = 0; |
while (i < array_len) { |
@@ -144,7 +133,7 @@ bool Utf8::IsValid(const uint8_t* utf8_array, intptr_t array_len) { |
(j == num_trail_bytes) && |
!IsOutOfRange(ch) && |
!IsNonShortestForm(ch, j) && |
- !IsSurrogate(ch))) { |
+ !Utf16::IsSurrogate(ch))) { |
return false; |
} |
} |
@@ -170,8 +159,9 @@ intptr_t Utf8::Length(int32_t ch) { |
intptr_t Utf8::Length(const String& str) { |
intptr_t length = 0; |
for (intptr_t i = 0; i < str.Length(); ++i) { |
- int32_t ch = str.CharAt(i); |
+ int32_t ch = Utf16::CodePointAt(str, i); |
length += Utf8::Length(ch); |
+ if (ch >= 0x10000) i++; // Surrogate pair in input |
} |
return length; |
} |
@@ -206,13 +196,14 @@ intptr_t Utf8::Encode(int32_t ch, char* dst) { |
intptr_t Utf8::Encode(const String& src, char* dst, intptr_t len) { |
intptr_t pos = 0; |
for (intptr_t i = 0; i < src.Length(); ++i) { |
- intptr_t ch = src.CharAt(i); |
+ intptr_t ch = Utf16::CodePointAt(src, i); |
intptr_t num_bytes = Utf8::Length(ch); |
if (pos + num_bytes > len) { |
break; |
} |
Utf8::Encode(ch, &dst[pos]); |
pos += num_bytes; |
+ if (num_bytes > 3) i++; // Surrogate pair in input. |
} |
return pos; |
} |
@@ -220,7 +211,7 @@ intptr_t Utf8::Encode(const String& src, char* dst, intptr_t len) { |
intptr_t Utf8::Decode(const uint8_t* utf8_array, |
intptr_t array_len, |
- int32_t* dst) { |
+ uint32_t* dst) { |
uint32_t ch = utf8_array[0] & 0xFF; |
intptr_t i = 1; |
if (ch >= 0x80) { |
@@ -232,7 +223,7 @@ intptr_t Utf8::Decode(const uint8_t* utf8_array, |
is_malformed |= !IsTrailByte(code_unit); |
ch = (ch << 6) + code_unit; |
} else { |
- *dst = -1; |
+ *dst = kInvalidCodePoint; |
return 0; |
} |
} |
@@ -241,8 +232,8 @@ intptr_t Utf8::Decode(const uint8_t* utf8_array, |
(i == num_trail_bytes) && |
!IsOutOfRange(ch) && |
!IsNonShortestForm(ch, i) && |
- !IsSurrogate(ch))) { |
- *dst = -1; |
+ !Utf16::IsSurrogate(ch))) { |
+ *dst = kInvalidCodePoint; |
return 0; |
} |
} |
@@ -276,15 +267,16 @@ bool Utf8::DecodeToUTF16(const uint8_t* utf8_array, |
intptr_t j = 0; |
intptr_t num_bytes; |
for (; (i < array_len) && (j < len); i += num_bytes, ++j) { |
- int32_t ch; |
+ uint32_t ch; |
bool is_smp = IsSmpSequenceStart(utf8_array[i]); |
num_bytes = Utf8::Decode(&utf8_array[i], (array_len - i), &ch); |
- if (ch == -1) { |
+ if (ch == kInvalidCodePoint) { |
return false; // invalid input |
} |
if (is_smp) { |
- ConvertUTF32ToUTF16(ch, &(dst[j])); |
- j = j + 1; |
+ dst[j] = Utf16::LeadFromCodePoint(ch); |
+ dst[j + 1] = Utf16::TrailFromCodePoint(ch); |
+ ++j; |
} else { |
dst[j] = ch; |
} |
@@ -304,9 +296,9 @@ bool Utf8::DecodeToUTF32(const uint8_t* utf8_array, |
intptr_t j = 0; |
intptr_t num_bytes; |
for (; (i < array_len) && (j < len); i += num_bytes, ++j) { |
- int32_t ch; |
+ uint32_t ch; |
num_bytes = Utf8::Decode(&utf8_array[i], (array_len - i), &ch); |
- if (ch == -1) { |
+ if (ch == kInvalidCodePoint) { |
return false; // invalid input |
} |
dst[j] = ch; |
@@ -317,4 +309,14 @@ bool Utf8::DecodeToUTF32(const uint8_t* utf8_array, |
return true; // success |
} |
+ |
+uint32_t Utf16::CodePointAt(const String& str, int index) { |
+ uint32_t code = str.CodeUnitAt(index); |
+ if (!IsLeadSurrogate(code)) return code; |
+ if (index + 1 == str.Length()) return code; |
+ uint32_t trail = str.CodeUnitAt(index + 1); |
+ if (!IsTrailSurrogate(trail)) return code; |
+ return CodePointFromCodeUnits(code, trail); |
+} |
+ |
} // namespace dart |