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

Unified Diff: sdk/lib/_internal/compiler/implementation/lib/js_string.dart

Issue 15740005: Implement string.trim for dart2js. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 7 years, 7 months 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
« no previous file with comments | « no previous file | tests/corelib/string_trim2_test.dart » ('j') | tests/corelib/string_trim2_test.dart » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/_internal/compiler/implementation/lib/js_string.dart
diff --git a/sdk/lib/_internal/compiler/implementation/lib/js_string.dart b/sdk/lib/_internal/compiler/implementation/lib/js_string.dart
index 56a494daee63d7b666ce7fea20fa0e0b46946e91..a1470753c61f8951a83589f0617887035b388860 100644
--- a/sdk/lib/_internal/compiler/implementation/lib/js_string.dart
+++ b/sdk/lib/_internal/compiler/implementation/lib/js_string.dart
@@ -97,8 +97,90 @@ class JSString extends Interceptor implements String, JSIndexable {
return JS('String', r'#.toUpperCase()', this);
}
+ // Characters with Whitespace property (Unicode 6.2).
+ // 0009..000D ; White_Space # Cc <control-0009>..<control-000D>
+ // 0020 ; White_Space # Zs SPACE
+ // 0085 ; White_Space # Cc <control-0085>
+ // 00A0 ; White_Space # Zs NO-BREAK SPACE
+ // 1680 ; White_Space # Zs OGHAM SPACE MARK
+ // 180E ; White_Space # Zs MONGOLIAN VOWEL SEPARATOR
+ // 2000..200A ; White_Space # Zs EN QUAD..HAIR SPACE
+ // 2028 ; White_Space # Zl LINE SEPARATOR
+ // 2029 ; White_Space # Zp PARAGRAPH SEPARATOR
+ // 202F ; White_Space # Zs NARROW NO-BREAK SPACE
+ // 205F ; White_Space # Zs MEDIUM MATHEMATICAL SPACE
+ // 3000 ; White_Space # Zs IDEOGRAPHIC SPACE
+ //
+ // BOM: 0xFEFF
+ bool _isWhitespace(int codeUnit) {
sra1 2013/05/22 22:39:03 static?
floitsch 2013/05/23 12:52:50 Done.
+ switch (codeUnit) {
+ case 0x09:
+ case 0x0A:
+ case 0x0B:
+ case 0x0C:
+ case 0x0D:
+ case 0x20:
+ case 0x85:
+ case 0xA0:
+ case 0x1680:
+ case 0x180E:
+ case 0x2000:
+ case 0x2001:
+ case 0x2002:
+ case 0x2003:
+ case 0x2004:
+ case 0x2005:
+ case 0x2006:
+ case 0x2007:
+ case 0x2008:
+ case 0x2009:
+ case 0x200A:
+ case 0x2028:
+ case 0x2029:
+ case 0x202F:
+ case 0x205F:
+ case 0x3000:
+ case 0xFEFF:
+ return true;
+ default:
+ return false;
+ }
+ }
+
+ // Dart2js can't use JavaScript trim, because JavaScript does not trim
+ // the New-line character (0x85).
sra1 2013/05/22 22:39:03 I believe it is called NEXT LINE, not new-line. C
floitsch 2013/05/23 12:52:50 yes. NEXT LINE.
String trim() {
sra1 2013/05/22 22:39:03 Can't this whole operation be coded concisely with
floitsch 2013/05/23 12:52:50 The regexp will run in squared behavior for trimmi
- return JS('String', r'#.trim()', this);
+ const int CARRIAGE_RETURN = 0x0D;
+ const int SPACE = 0x20;
+
+ int startIndex = 0;
+ while (startIndex < this.length) {
+ int codeUnit = this.codeUnitAt(startIndex);
+ if (codeUnit == SPACE ||
+ codeUnit == CARRIAGE_RETURN ||
+ _isWhitespace(codeUnit)) {
+ startIndex++;
+ } else {
+ break;
+ }
+ }
+ if (startIndex == this.length) return "";
+
+ int endIndex = this.length;
+ // We know that there is at least one character that is non-whitespace.
+ // Therefore we don't need to verify that endIndex > startIndex.
+ while (true) {
+ int codeUnit = this.codeUnitAt(endIndex - 1);
+ if (codeUnit == SPACE ||
+ codeUnit == CARRIAGE_RETURN ||
+ _isWhitespace(codeUnit)) {
+ endIndex--;
+ } else {
+ break;
+ }
+ }
+ if (startIndex == 0 && endIndex == this.length) return this;
+ return JS('String', r'#.substring(#, #)', this, startIndex, endIndex);
}
List<int> get codeUnits => new _CodeUnits(this);
« no previous file with comments | « no previous file | tests/corelib/string_trim2_test.dart » ('j') | tests/corelib/string_trim2_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698