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

Unified Diff: runtime/vm/unicode.cc

Issue 11416054: Provide a code point iterator to the String class to simplify iteration. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: remove unintended code 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
« runtime/vm/object.cc ('K') | « runtime/vm/unicode.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/vm/unicode.cc
diff --git a/runtime/vm/unicode.cc b/runtime/vm/unicode.cc
index e2e741d848339162fd9fa7f89924ed8363e1469e..9902afcfe40225e9ab43fbd32a26607d6974d723 100644
--- a/runtime/vm/unicode.cc
+++ b/runtime/vm/unicode.cc
@@ -70,12 +70,6 @@ static bool IsSmpSequenceStart(uint8_t code_unit) {
}
-// 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);
@@ -88,14 +82,6 @@ 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,
@@ -144,7 +130,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;
}
}
@@ -169,8 +155,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);
+ String::CodePointIterator it(str);
+ while (it.Next()) {
+ int32_t ch = it.Current();
length += Utf8::Length(ch);
}
return length;
@@ -205,8 +192,9 @@ 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);
+ String::CodePointIterator it(src);
+ while (it.Next()) {
+ intptr_t ch = it.Current();
siva 2012/11/19 18:25:29 int32_t ch = it.Current() ?
cshapiro 2012/11/19 18:55:11 Yes. I will fix this before submitting.
intptr_t num_bytes = Utf8::Length(ch);
if (pos + num_bytes > len) {
break;
@@ -241,7 +229,7 @@ intptr_t Utf8::Decode(const uint8_t* utf8_array,
(i == num_trail_bytes) &&
!IsOutOfRange(ch) &&
!IsNonShortestForm(ch, i) &&
- !IsSurrogate(ch))) {
+ !Utf16::IsSurrogate(ch))) {
*dst = -1;
return 0;
}
@@ -290,7 +278,7 @@ bool Utf8::DecodeToUTF16(const uint8_t* utf8_array,
return false; // invalid input
}
if (is_smp) {
- ConvertUTF32ToUTF16(ch, &(dst[j]));
+ Utf16::Encode(ch, &dst[j]);
j = j + 1;
} else {
dst[j] = ch;
@@ -324,4 +312,12 @@ bool Utf8::DecodeToUTF32(const uint8_t* utf8_array,
return true; // success
}
+
+void Utf16::Encode(int32_t codepoint, uint16_t* dst) {
+ ASSERT(codepoint > kMaxBmpCodepoint);
+ ASSERT(dst != NULL);
+ dst[0] = (Utf16::kLeadSurrogateOffset + (codepoint >> 10));
+ dst[1] = (0xDC00 + (codepoint & 0x3FF));
+}
+
} // namespace dart
« runtime/vm/object.cc ('K') | « runtime/vm/unicode.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698