OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 patch class String { | 5 patch class String { |
6 /* patch */ factory String.fromCharCodes(Iterable<int> charCodes) { | 6 /* patch */ factory String.fromCharCodes(Iterable<int> charCodes) { |
7 return _StringBase.createFromCharCodes(charCodes); | 7 return _StringBase.createFromCharCodes(charCodes); |
8 } | 8 } |
9 | 9 |
10 /* patch */ factory String.fromCharCode(int charCode) { | 10 /* patch */ factory String.fromCharCode(int charCode) { |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
121 } | 121 } |
122 final len = this.length; | 122 final len = this.length; |
123 for (int i = 0; i < len; i++) { | 123 for (int i = 0; i < len; i++) { |
124 if (this.codeUnitAt(i) != other.codeUnitAt(i)) { | 124 if (this.codeUnitAt(i) != other.codeUnitAt(i)) { |
125 return false; | 125 return false; |
126 } | 126 } |
127 } | 127 } |
128 return true; | 128 return true; |
129 } | 129 } |
130 | 130 |
131 | |
132 int compareTo(String other) { | 131 int compareTo(String other) { |
133 int thisLength = this.length; | 132 int thisLength = this.length; |
134 int otherLength = other.length; | 133 int otherLength = other.length; |
135 int len = (thisLength < otherLength) ? thisLength : otherLength; | 134 int len = (thisLength < otherLength) ? thisLength : otherLength; |
136 for (int i = 0; i < len; i++) { | 135 for (int i = 0; i < len; i++) { |
137 int thisCodePoint = this.codeUnitAt(i); | 136 int thisCodePoint = this.codeUnitAt(i); |
138 int otherCodePoint = other.codeUnitAt(i); | 137 int otherCodePoint = other.codeUnitAt(i); |
139 if (thisCodePoint < otherCodePoint) { | 138 if (thisCodePoint < otherCodePoint) { |
140 return -1; | 139 return -1; |
141 } | 140 } |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
274 // 1680 ; White_Space # Zs OGHAM SPACE MARK | 273 // 1680 ; White_Space # Zs OGHAM SPACE MARK |
275 // 180E ; White_Space # Zs MONGOLIAN VOWEL SEPARATOR | 274 // 180E ; White_Space # Zs MONGOLIAN VOWEL SEPARATOR |
276 // 2000..200A ; White_Space # Zs EN QUAD..HAIR SPACE | 275 // 2000..200A ; White_Space # Zs EN QUAD..HAIR SPACE |
277 // 2028 ; White_Space # Zl LINE SEPARATOR | 276 // 2028 ; White_Space # Zl LINE SEPARATOR |
278 // 2029 ; White_Space # Zp PARAGRAPH SEPARATOR | 277 // 2029 ; White_Space # Zp PARAGRAPH SEPARATOR |
279 // 202F ; White_Space # Zs NARROW NO-BREAK SPACE | 278 // 202F ; White_Space # Zs NARROW NO-BREAK SPACE |
280 // 205F ; White_Space # Zs MEDIUM MATHEMATICAL SPACE | 279 // 205F ; White_Space # Zs MEDIUM MATHEMATICAL SPACE |
281 // 3000 ; White_Space # Zs IDEOGRAPHIC SPACE | 280 // 3000 ; White_Space # Zs IDEOGRAPHIC SPACE |
282 // | 281 // |
283 // BOM: 0xFEFF | 282 // BOM: 0xFEFF |
284 static bool _isTwoByteWhitespace(int codePoint) { | 283 static bool _isTwoByteWhitespace(int codePoint) { |
Lasse Reichstein Nielsen
2014/07/04 06:46:01
The "codePoint" argument is really a "codeUnit". I
| |
285 if (codePoint < 256) return _isOneByteWhitespace(codePoint); | 284 if (codePoint < 256) return _isOneByteWhitespace(codePoint); |
286 return (codePoint == 0x1680) || | 285 return (codePoint == 0x1680) || |
287 (codePoint == 0x180E) || | 286 (codePoint == 0x180E) || |
288 ((0x2000 <= codePoint) && (codePoint <= 0x200A)) || | 287 ((0x2000 <= codePoint) && (codePoint <= 0x200A)) || |
289 (codePoint == 0x2028) || | 288 (codePoint == 0x2028) || |
290 (codePoint == 0x2029) || | 289 (codePoint == 0x2029) || |
291 (codePoint == 0x202F) || | 290 (codePoint == 0x202F) || |
292 (codePoint == 0x205F) || | 291 (codePoint == 0x205F) || |
293 (codePoint == 0x3000) || | 292 (codePoint == 0x3000) || |
294 (codePoint == 0xFEFF); | 293 (codePoint == 0xFEFF); |
295 } | 294 } |
296 | 295 |
297 String trim() { | 296 int _firstNonWhitespace() { |
298 final len = this.length; | 297 final len = this.length; |
299 int first = 0; | 298 int first = 0; |
300 for (; first < len; first++) { | 299 for (; first < len; first++) { |
301 if (!_isWhitespace(this.codeUnitAt(first))) { | 300 if (!_isWhitespace(this.codeUnitAt(first))) { |
302 break; | 301 break; |
303 } | 302 } |
304 } | 303 } |
304 return first; | |
305 } | |
306 | |
307 int _lastNonWhitespace() { | |
308 int last = this.length - 1; | |
309 for (; last >= 0; last--) { | |
Ivan Posva
2014/07/03 19:44:14
I know this was used in trim before, but I think i
Lasse Reichstein Nielsen
2014/07/04 06:46:01
Yes. All Unicode white-space are in the basic mult
| |
310 if (!_isWhitespace(this.codeUnitAt(last))) { | |
311 break; | |
312 } | |
313 } | |
314 return last; | |
315 } | |
316 | |
317 String trim() { | |
318 final len = this.length; | |
319 int first = _firstNonWhitespace(); | |
305 if (len == first) { | 320 if (len == first) { |
306 // String contains only whitespaces. | 321 // String contains only whitespaces. |
307 return ""; | 322 return ""; |
308 } | 323 } |
309 int last = len - 1; | 324 int last = _lastNonWhitespace() + 1; |
310 for (; last >= first; last--) { | 325 if ((first == 0) && (last == len)) { |
311 if (!_isWhitespace(this.codeUnitAt(last))) { | |
312 break; | |
313 } | |
314 } | |
315 if ((first == 0) && (last == (len - 1))) { | |
316 // Returns this string since it does not have leading or trailing | 326 // Returns this string since it does not have leading or trailing |
317 // whitespaces. | 327 // whitespaces. |
318 return this; | 328 return this; |
319 } | 329 } |
320 return _substringUnchecked(first, last + 1); | 330 return _substringUnchecked(first, last); |
321 } | 331 } |
322 | 332 |
323 String trimLeft() { | 333 String trimLeft() { |
324 final len = this.length; | 334 final len = this.length; |
325 int first = 0; | 335 int first = 0; |
326 for (; first < len; first++) { | 336 for (; first < len; first++) { |
327 if (!_isWhitespace(this.codeUnitAt(first))) { | 337 if (!_isWhitespace(this.codeUnitAt(first))) { |
328 break; | 338 break; |
329 } | 339 } |
330 } | 340 } |
(...skipping 659 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
990 class _CodeUnits extends Object with ListMixin<int>, | 1000 class _CodeUnits extends Object with ListMixin<int>, |
991 UnmodifiableListMixin<int> { | 1001 UnmodifiableListMixin<int> { |
992 /** The string that this is the code units of. */ | 1002 /** The string that this is the code units of. */ |
993 String _string; | 1003 String _string; |
994 | 1004 |
995 _CodeUnits(this._string); | 1005 _CodeUnits(this._string); |
996 | 1006 |
997 int get length => _string.length; | 1007 int get length => _string.length; |
998 int operator[](int i) => _string.codeUnitAt(i); | 1008 int operator[](int i) => _string.codeUnitAt(i); |
999 } | 1009 } |
OLD | NEW |