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

Unified Diff: runtime/lib/string_base.dart

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/lib/string_base.dart
diff --git a/runtime/lib/string_base.dart b/runtime/lib/string_base.dart
index 11b594ed9e562cebc71e654acdb8b433678031f5..3f60457130a16e584c664636043867a96aeeac53 100644
--- a/runtime/lib/string_base.dart
+++ b/runtime/lib/string_base.dart
@@ -19,15 +19,15 @@ class _StringBase {
* Create the most efficient string representation for specified
* [codePoints].
*/
- static String createFromCharCodes(List<int> charCodes) {
+ static String createFromCharCodes(List<int> codePoints) {
_ObjectArray objectArray;
- if (charCodes is _ObjectArray) {
- objectArray = charCodes;
+ if (codePoints is _ObjectArray) {
+ objectArray = codePoints;
} else {
- int len = charCodes.length;
+ int len = codePoints.length;
objectArray = new _ObjectArray(len);
for (int i = 0; i < len; i++) {
- objectArray[i] = charCodes[i];
+ objectArray[i] = codePoints[i];
}
}
return _createFromCodePoints(objectArray);
@@ -36,10 +36,29 @@ class _StringBase {
static String _createFromCodePoints(List<int> codePoints)
native "StringBase_createFromCodePoints";
+ static String createFromCodeUnits(List<int> codeUnits) {
+ _ObjectArray objectArray;
+ if (codeUnits is _ObjectArray) {
+ objectArray = codeUnits;
+ } else {
+ int len = codeUnits.length;
+ objectArray = new _ObjectArray(len);
+ for (int i = 0; i < len; i++) {
+ objectArray[i] = codeUnits[i];
+ }
+ }
+ return _createFromCodeUnits(objectArray);
+ }
+
+ static String _createFromCodeUnits(List<int> codeUnits)
+ native "StringBase_createFromCodeUnits";
+
String operator [](int index) native "String_charAt";
int charCodeAt(int index) native "String_charCodeAt";
+ int codeUnitAt(int index) native "String_codeUnitAt";
+
int get length native "String_getLength";
bool get isEmpty {
@@ -69,12 +88,12 @@ class _StringBase {
int otherLength = other.length;
int len = (thisLength < otherLength) ? thisLength : otherLength;
for (int i = 0; i < len; i++) {
- int thisCodePoint = this.charCodeAt(i);
- int otherCodePoint = other.charCodeAt(i);
- if (thisCodePoint < otherCodePoint) {
+ int thisCodeUnit = this.codeUnitAt(i);
+ int otherCodeUnit = other.codeUnitAt(i);
+ if (thisCodeUnit < otherCodeUnit) {
return -1;
}
- if (thisCodePoint > otherCodePoint) {
+ if (thisCodeUnit > otherCodeUnit) {
return 1;
}
}
@@ -93,7 +112,7 @@ class _StringBase {
return false;
}
for (int i = 0; i < len; i++) {
- if (this.charCodeAt(i + start) != other.charCodeAt(i)) {
+ if (this.codeUnitAt(i + start) != other.codeUnitAt(i)) {
return false;
}
}
@@ -162,7 +181,9 @@ class _StringBase {
final int len = this.length;
int first = 0;
for (; first < len; first++) {
- if (!_isWhitespace(this.charCodeAt(first))) {
+ // There are no whitespace characters that are outside the BMP so we
+ // can use code units here for efficiency.
+ if (!_isWhitespace(this.codeUnitAt(first))) {
break;
}
}
@@ -172,7 +193,7 @@ class _StringBase {
}
int last = len - 1;
for (; last >= first; last--) {
- if (!_isWhitespace(this.charCodeAt(last))) {
+ if (!_isWhitespace(this.codeUnitAt(last))) {
break;
}
}
@@ -293,20 +314,57 @@ class _StringBase {
return result;
}
+ // TODO(erikcorry): Fix this to use the new code point iterator when it is
+ // available.
List<String> splitChars() {
int len = this.length;
final result = new List<String>(len);
- for (int i = 0; i < len; i++) {
- result[i] = this[i];
+ int i, j;
+ for (i = j = 0; i < len; i++, j++) {
+ int c = charCodeAt(i);
+ // Check for non-basic plane character encoded as a UTF-16 surrogate pair.
+ if (c > 0xffff) {
floitsch 2012/11/08 15:28:21 Can't you use Utf16::IsSurrogate(c)?
erikcorry 2012/11/15 13:28:25 No, that's a C++ function. I added some named con
+ i++;
+ }
+ result[j] = new String.fromCharCodes([c]);
}
- return result;
+ if (i == j) return result;
+ // If we saw some non-basic plane characters, then we have to return a
+ // slightly smaller array than expected (we can't trim the original one
+ // because it is non-extendable). This rarely happens so this is preferable
+ // to having a separate pass over the string to count the code points.
+ final newResult = new List<String>(j);
+ for (i = 0; i < j; i++) newResult[i] = result[i];
+ return newResult;
}
List<int> get charCodes {
int len = this.length;
final result = new List<int>(len);
+ int i, j;
+ for (i = j = 0; i < len; i++, j++) {
+ int c = this.charCodeAt(i);
+ // Check for non-basic plane character encoded as a UTF-16 surrogate pair.
+ if (c > 0xffff) {
+ i++;
+ }
+ result[j] = c;
+ }
+ if (i == j) return result;
+ // If we saw some non-basic plane characters, then we have to return a
+ // slightly smaller array than expected (we can't trim the original one
+ // because it is non-extendable). This rarely happens so this is preferable
+ // to having a separate pass over the string to count the code points.
+ final newResult = new List<int>(j);
+ for (i = 0; i < j; i++) newResult[i] = result[i];
+ return newResult;
+ }
+
+ List<int> get codeUnits {
+ int len = this.length;
+ final result = new List<int>(len);
for (int i = 0; i < len; i++) {
- result[i] = this.charCodeAt(i);
+ result[i] = this.codeUnitAt(i);
}
return result;
}
@@ -360,12 +418,10 @@ class _OneByteString extends _StringBase implements String {
"_OneByteString can only be allocated by the VM");
}
- // Checks for one-byte whitespaces only.
- // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid
- // whitespaces for one byte strings.
bool _isWhitespace(int codePoint) {
return
- (codePoint === 32) || // Space.
+ (codePoint == 32) || // Space.
+ (codePoint == 0xa0) || // No-break space.
((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc.
}
@@ -378,17 +434,24 @@ class _TwoByteString extends _StringBase implements String {
"_TwoByteString can only be allocated by the VM");
}
- // Checks for one-byte whitespaces only.
- // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid
- // whitespaces. Add checking for multi-byte whitespace codepoints.
+ // Works for both code points and code units since all spaces are in the BMP.
bool _isWhitespace(int codePoint) {
return
- (codePoint === 32) || // Space.
- ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc.
+ (codePoint == 32) || // Space.
+ (codePoint == 0xa0) || // No-break space.
+ ((9 <= codePoint) && (codePoint <= 13)) || // CR, LF, TAB, etc.
+ (codePoint >= 0x1680 && // Optimization.
+ (codePoint == 0x1680 || // Ogham space mark.
+ codePoint == 0x180e || // Mongolian vowel separator.
+ (codePoint >= 0x2000 && codePoint <= 0x200a) || // Wide/narrow spaces.
+ codePoint == 0x202f || // Narrow no-break space.
+ codePoint == 0x205f || // Medium mathematical space.
+ codePoint == 0x3000)); // Ideographic space.
}
}
+// TODO(erikcorry): This is going away.
class _FourByteString extends _StringBase implements String {
factory _FourByteString._uninstantiable() {
throw new UnsupportedError(
@@ -412,12 +475,10 @@ class _ExternalOneByteString extends _StringBase implements String {
"_ExternalOneByteString can only be allocated by the VM");
}
- // Checks for one-byte whitespaces only.
- // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid
- // whitespaces for one byte strings.
bool _isWhitespace(int codePoint) {
return
- (codePoint === 32) || // Space.
+ (codePoint == 32) || // Space.
+ (codePoint == 0xa0) || // No-break space.
((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc.
}
}
@@ -429,17 +490,24 @@ class _ExternalTwoByteString extends _StringBase implements String {
"_ExternalTwoByteString can only be allocated by the VM");
}
- // Checks for one-byte whitespaces only.
- // TODO(srdjan): Investigate if 0x85 (NEL) and 0xA0 (NBSP) are valid
- // whitespaces. Add checking for multi-byte whitespace codepoints.
+ // Works for both code points and code units since all spaces are in the BMP.
bool _isWhitespace(int codePoint) {
return
- (codePoint === 32) || // Space.
- ((9 <= codePoint) && (codePoint <= 13)); // CR, LF, TAB, etc.
+ (codePoint == 32) || // Space.
+ (codePoint == 0xa0) || // No-break space.
+ ((9 <= codePoint) && (codePoint <= 13)) || // CR, LF, TAB, etc.
+ (codePoint >= 0x1680 && // Optimization.
+ (codePoint == 0x1680 || // Ogham space mark.
+ codePoint == 0x180e || // Mongolian vowel separator.
+ (codePoint >= 0x2000 && codePoint <= 0x200a) || // Wide/narrow spaces.
+ codePoint == 0x202f || // Narrow no-break space.
+ codePoint == 0x205f || // Medium mathematical space.
+ codePoint == 0x3000)); // Ideographic space.
}
}
+// TODO(erikcorry): This is going away.
class _ExternalFourByteString extends _StringBase implements String {
factory _ExternalFourByteString._uninstantiable() {
throw new UnsupportedError(

Powered by Google App Engine
This is Rietveld 408576698