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