Chromium Code Reviews| 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 part of dart.core; | 5 part of dart.core; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * The String class represents character strings. Strings are | 8 * The String class represents sequences of characters. Strings are |
| 9 * immutable. A string is represented by a list of 32-bit Unicode | 9 * immutable. A string is represented by a sequence of Unicode UTF-16 |
| 10 * scalar character codes accessible through the [charCodeAt] or the | 10 * code units accessible through the [codeUnitAt] or the |
| 11 * [charCodes] method. | 11 * [codeUnits] members. Their string representation is accessible through |
| 12 * the index-operator. | |
| 13 * | |
| 14 * The characters of a string are encoded in UTF-16. Decoding UTF-16, which | |
| 15 * combines surrogate pairs, yields Unicode code points. Following a similar | |
| 16 * terminology to Go we use the name "rune" for a Unicode code point. The 32-bit | |
|
Lasse Reichstein Nielsen
2013/02/04 15:26:01
What makes them 32-bit?
In Dart, integers do not h
floitsch
2013/02/04 16:03:26
Done.
| |
| 17 * rune values are accessible through the [runes] getter. | |
| 12 */ | 18 */ |
| 13 abstract class String implements Comparable, Pattern { | 19 abstract class String implements Comparable, Pattern { |
| 14 /** | 20 /** |
| 15 * Allocates a new String for the specified [charCodes]. | 21 * Allocates a new String for the specified [charCodes]. |
| 22 * | |
| 23 * The [charCodes] can be UTF-16 code units or runes. If a char-code value is | |
| 24 * 16-bit it is copied verbatim. If it is greater than 16 bits it is | |
| 25 * decomposed into a surrogate pair. | |
| 16 */ | 26 */ |
| 17 external factory String.fromCharCodes(List<int> charCodes); | 27 external factory String.fromCharCodes(Iterable<int> charCodes); |
| 28 | |
| 29 /** | |
| 30 * *Deprecated*. Use [String.fromCharCode] instead. | |
| 31 */ | |
| 32 factory String.character(int charCode) => new String.fromCharCode(charCode); | |
| 18 | 33 |
| 19 /** | 34 /** |
| 20 * Allocates a new String for the specified [charCode]. | 35 * Allocates a new String for the specified [charCode]. |
| 21 * | 36 * |
| 22 * The built string is of [length] one, if the [charCode] lies inside the | 37 * The new string contains a single code unit if the [charCode] can be |
| 23 * basic multilingual plane (plane 0). Otherwise the [length] is 2 and | 38 * represented in 16 bits. Otherwise the [length] is 2 and the code units form |
|
Lasse Reichstein Nielsen
2013/02/04 15:26:01
... represented by a single UTF-16 code unit.
In
floitsch
2013/02/04 16:03:26
Done.
| |
| 24 * the code units form a surrogate pair. | 39 * a surrogate pair. |
| 40 * | |
| 41 * It is allowed (though generally discouraged) to create a String with only | |
| 42 * one half of a surrogate pair. | |
| 25 */ | 43 */ |
| 26 factory String.character(int charCode) { | 44 factory String.fromCharCode(int charCode) { |
|
Lasse Reichstein Nielsen
2013/02/04 15:26:01
Should this be ..(int rune) ?
floitsch
2013/02/04 16:03:26
Not necessarily, since it can contain half of a su
| |
| 27 List<int> charCodes = new List<int>.fixedLength(1, fill: charCode); | 45 List<int> charCodes = new List<int>.fixedLength(1, fill: charCode); |
| 28 return new String.fromCharCodes(charCodes); | 46 return new String.fromCharCodes(charCodes); |
| 29 } | 47 } |
| 30 | 48 |
| 31 /** | 49 /** |
| 32 * Gets the character (as [String]) at the given [index]. | 50 * Gets the character (as [String]) at the given [index]. |
| 51 * | |
| 52 * The returned string represents exactly one UTF-16 code unit which may be | |
| 53 * half of a surrogate pair. For example the Unicode character for a | |
| 54 * musical g clef ("𝄞") with rune value 0x1D11E consists of a UTF-16 surrogate | |
|
Lasse Reichstein Nielsen
2013/02/04 15:26:01
g clef -> G-clef
floitsch
2013/02/04 16:03:26
Done.
| |
| 55 * pair: `"\uDBFF\uDFFD"`. Using the index-operator on this string yields | |
| 56 * a String with half of a surrogate pair: | |
| 57 * | |
| 58 * var clef = "\uDBFF\uDFFD"; | |
| 59 * clef.length; // => 2 | |
| 60 * clef.runes.first == 0x1D11E; // => true | |
| 61 * clef.runes.length; // => 1 | |
| 62 * // The following strings are halves of a UTF-16 surrogate pair and | |
| 63 * // thus invalid UTF-16 strings: | |
| 64 * clef[0]; // => "\uDBFF" | |
| 65 * clef[1]; // => "\uDFFD" | |
| 66 * | |
| 67 * This method is equivalent to | |
| 68 * `new String.fromCharCode(this.codeUnitAt(index))`. | |
| 33 */ | 69 */ |
| 34 String operator [](int index); | 70 String operator [](int index); |
| 35 | 71 |
| 36 /** | 72 /** |
| 37 * Gets the scalar character code at the given [index]. | 73 * Gets the scalar character code at the given [index]. |
| 74 * | |
| 75 * *This method is deprecated. Please use [codeUnitAt] instead.* | |
| 38 */ | 76 */ |
| 39 int charCodeAt(int index); | 77 int charCodeAt(int index); |
| 40 | 78 |
| 41 /** | 79 /** |
| 80 * Returns the 16-bit UTF-16 code unit at the given [index]. | |
| 81 */ | |
| 82 int codeUnitAt(int index); | |
| 83 | |
| 84 /** | |
| 42 * The length of the string. | 85 * The length of the string. |
| 86 * | |
| 87 * Returns the number of UTF-16 code units in this string. The number | |
| 88 * of [runes] might be less, if the string contains characters outside | |
| 89 * the basic multilingual plane (plane 0). | |
| 43 */ | 90 */ |
| 44 int get length; | 91 int get length; |
| 45 | 92 |
| 46 /** | 93 /** |
| 47 * Returns whether the two strings are equal. This method compares | 94 * Returns whether the two strings are equal. |
| 48 * each individual scalar character codes of the strings. | 95 * |
| 96 * This method compares each individual code unit of the strings. It does not | |
| 97 * check for Unicode equivalence. For example the two following strings both | |
| 98 * represent the string "Amélie" but, due to their different encoding will | |
| 99 * not return equal. | |
| 100 * | |
| 101 * "Am\xe9lie" | |
| 102 * "Ame\u{301}lie" | |
| 103 * | |
| 104 * In the first string the "é" is encoded as a single unicode code unit, | |
| 105 * whereas the second string encodes it as "e" with the combining | |
| 106 * accent character "◌́". | |
| 49 */ | 107 */ |
| 50 bool operator ==(String other); | 108 bool operator ==(var other); |
| 51 | 109 |
| 52 /** | 110 /** |
| 53 * Returns whether this string ends with [other]. | 111 * Returns whether this string ends with [other]. |
| 54 */ | 112 */ |
| 55 bool endsWith(String other); | 113 bool endsWith(String other); |
| 56 | 114 |
| 57 /** | 115 /** |
| 58 * Returns whether this string starts with [other]. | 116 * Returns whether this string starts with [other]. |
| 59 */ | 117 */ |
| 60 bool startsWith(String other); | 118 bool startsWith(String other); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 140 * | 198 * |
| 141 * | 199 * |
| 142 * The [replace] function is called with the [Match] generated | 200 * The [replace] function is called with the [Match] generated |
| 143 * by the pattern, and its result is used as replacement. | 201 * by the pattern, and its result is used as replacement. |
| 144 */ | 202 */ |
| 145 String replaceAllMapped(Pattern from, String replace(Match match)); | 203 String replaceAllMapped(Pattern from, String replace(Match match)); |
| 146 | 204 |
| 147 /** | 205 /** |
| 148 * Splits the string around matches of [pattern]. Returns | 206 * Splits the string around matches of [pattern]. Returns |
| 149 * a list of substrings. | 207 * a list of substrings. |
| 208 * | |
| 209 * Splitting with an empty string pattern (`""`) splits at UTF-16 code unit | |
| 210 * boundaries and not at rune boundaries. The following two expressions | |
| 211 * are hence equivalent: | |
| 212 * | |
| 213 * string.split("") | |
| 214 * string.codeUnits.map((unit) => new String.character(unit)) | |
| 215 * | |
| 216 * Unless it guaranteed that the string is in the basic multilingual plane | |
| 217 * (meaning that each code unit represents a rune) it is often better to | |
| 218 * map the runes instead: | |
| 219 * | |
| 220 * string.runes.map((rune) => new String.character(rune)) | |
| 150 */ | 221 */ |
| 151 List<String> split(Pattern pattern); | 222 List<String> split(Pattern pattern); |
| 152 | 223 |
| 153 /** | 224 /** |
| 154 * Returns a list of the characters of this string. | 225 * Returns a list of the individual code-units characters of this string. |
|
Lasse Reichstein Nielsen
2013/02/04 15:26:01
code-units -> code-unit.
Or? Not sure I understand
floitsch
2013/02/04 16:03:26
Changed to: Returns a list of the individual code-
| |
| 226 * | |
| 227 * *Deprecated* | |
| 228 * If you want to split on code-unit boundaries, use [split]. If you | |
| 229 * want to split on rune boundaries, use [runes] and map the result. | |
| 230 * | |
| 231 * Iterable<String> characters = | |
| 232 * string.runes.map((c) => new String.fromCharCode(c)); | |
| 155 */ | 233 */ |
| 156 List<String> splitChars(); | 234 List<String> splitChars(); |
| 157 | 235 |
| 158 /** | 236 /** |
| 159 * Splits the string on the [pattern], then converts each part and each match. | 237 * Splits the string on the [pattern], then converts each part and each match. |
| 160 * | 238 * |
| 161 * The pattern is used to split the string into parts and separating matches. | 239 * The pattern is used to split the string into parts and separating matches. |
| 162 * | 240 * |
| 163 * Each match is converted to a string by calling [onMatch]. If [onMatch] | 241 * Each match is converted to a string by calling [onMatch]. If [onMatch] |
| 164 * is omitted, the matched string is used. | 242 * is omitted, the matched string is used. |
| 165 * | 243 * |
| 166 * Each non-matched part is converted by a call to [onNonMatch]. If | 244 * Each non-matched part is converted by a call to [onNonMatch]. If |
| 167 * [onNonMatch] is omitted, the non-matching part is used. | 245 * [onNonMatch] is omitted, the non-matching part is used. |
| 168 * | 246 * |
| 169 * Then all the converted parts are combined into the resulting string. | 247 * Then all the converted parts are combined into the resulting string. |
| 170 */ | 248 */ |
| 171 String splitMapJoin(Pattern pattern, | 249 String splitMapJoin(Pattern pattern, |
| 172 {String onMatch(Match match), | 250 {String onMatch(Match match), |
| 173 String onNonMatch(String nonMatch)}); | 251 String onNonMatch(String nonMatch)}); |
| 174 | 252 |
| 175 /** | 253 /** |
| 176 * Returns a list of the scalar character codes of this string. | 254 * Returns a list of 16-bit UTF-16 code units of this string. |
|
Lasse Reichstein Nielsen
2013/02/04 15:26:01
Drop 16-bit. That's what UTF-16 code units are.
floitsch
2013/02/04 16:03:26
Done.
| |
| 255 * | |
| 256 * *This getter is deprecated. Use [codeUnits] instead.* | |
| 177 */ | 257 */ |
| 178 List<int> get charCodes; | 258 List<int> get charCodes; |
| 179 | 259 |
| 180 /** | 260 /** |
| 261 * Returns an iterable of the 16-bit UTF-16 code units of this string. | |
|
Lasse Reichstein Nielsen
2013/02/04 15:26:01
drop 16-bit.
floitsch
2013/02/04 16:03:26
Done.
| |
| 262 */ | |
| 263 // TODO(floitsch): should it return a list? | |
| 264 // TODO(floitsch): make it a bidirectional iterator. | |
| 265 Iterable<int> get codeUnits; | |
| 266 | |
| 267 /** | |
| 268 * Returns an iterable of Unicode code-points of this string. | |
| 269 * | |
| 270 * If the string contains surrogate pairs, they will be combined and returned | |
| 271 * as one integer by this iterator. Unmatched surrogate halves are treated | |
| 272 * like valid 16-bit code-units. | |
| 273 */ | |
| 274 // TODO(floitsch): make it a Runes class. | |
| 275 Iterable<int> get runes; | |
| 276 | |
| 277 /** | |
| 181 * If this string is not already all lower case, returns a new string | 278 * If this string is not already all lower case, returns a new string |
| 182 * where all characters are made lower case. Returns [:this:] otherwise. | 279 * where all characters are made lower case. Returns [:this:] otherwise. |
| 183 */ | 280 */ |
| 281 // TODO(floitsch): document better. (See EcmaScript for description). | |
| 184 String toLowerCase(); | 282 String toLowerCase(); |
| 185 | 283 |
| 186 /** | 284 /** |
| 187 * If this string is not already all uper case, returns a new string | 285 * If this string is not already all upper case, returns a new string |
| 188 * where all characters are made upper case. Returns [:this:] otherwise. | 286 * where all characters are made upper case. Returns [:this:] otherwise. |
| 189 */ | 287 */ |
| 288 // TODO(floitsch): document better. (See EcmaScript for description). | |
| 190 String toUpperCase(); | 289 String toUpperCase(); |
| 191 } | 290 } |
| OLD | NEW |