Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2061)

Unified Diff: runtime/vm/unicode.cc

Issue 11368138: Add some support for the code-point code-unit distinction. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: runtime/vm/unicode.cc
diff --git a/runtime/vm/unicode.cc b/runtime/vm/unicode.cc
index 42cd42607c29a196d4db08e6fd6ea335426be1de..9671861aae24214911c52988d0b6774a47878cf6 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;
}
@@ -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;
}
}
@@ -220,7 +209,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 +221,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 +230,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 +265,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 +294,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;

Powered by Google App Engine
This is Rietveld 408576698