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

Side by Side Diff: sdk/lib/core/string.dart

Issue 12088086: Adapt String interface for Utf16. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Don't change intrinsified String.charCodeAt. Created 7 years, 10 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/_internal/compiler/implementation/lib/js_string.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 an integer representing a
17 * Unicode code point. The runes of a string are accessible through the [runes]
18 * getter.
12 */ 19 */
13 abstract class String implements Comparable, Pattern { 20 abstract class String implements Comparable, Pattern {
14 /** 21 /**
15 * Allocates a new String for the specified [charCodes]. 22 * 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.
16 */ 27 */
17 external factory String.fromCharCodes(List<int> charCodes); 28 external factory String.fromCharCodes(Iterable<int> charCodes);
29
30 /**
31 * *Deprecated*. Use [String.fromCharCode] instead.
32 */
33 factory String.character(int charCode) => new String.fromCharCode(charCode);
18 34
19 /** 35 /**
20 * Allocates a new String for the specified [charCode]. 36 * Allocates a new String for the specified [charCode].
21 * 37 *
22 * The built string is of [length] one, if the [charCode] lies inside the 38 * 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 39 * represented by a single UTF-16 code unit. Otherwise the [length] is 2 and
24 * the code units form a surrogate pair. 40 * 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.
25 */ 44 */
26 factory String.character(int charCode) { 45 factory String.fromCharCode(int charCode) {
27 List<int> charCodes = new List<int>.fixedLength(1, fill: charCode); 46 List<int> charCodes = new List<int>.fixedLength(1, fill: charCode);
28 return new String.fromCharCodes(charCodes); 47 return new String.fromCharCodes(charCodes);
29 } 48 }
30 49
31 /** 50 /**
32 * Gets the character (as [String]) at the given [index]. 51 * 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))`.
33 */ 70 */
34 String operator [](int index); 71 String operator [](int index);
35 72
36 /** 73 /**
37 * Gets the scalar character code at the given [index]. 74 * Gets the scalar character code at the given [index].
75 *
76 * *This method is deprecated. Please use [codeUnitAt] instead.*
38 */ 77 */
39 int charCodeAt(int index); 78 int charCodeAt(int index);
40 79
41 /** 80 /**
81 * Returns the 16-bit UTF-16 code unit at the given [index].
82 */
83 int codeUnitAt(int index);
84
85 /**
42 * The length of the string. 86 * 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).
43 */ 91 */
44 int get length; 92 int get length;
45 93
46 /** 94 /**
47 * Returns whether the two strings are equal. This method compares 95 * Returns whether the two strings are equal.
48 * each individual scalar character codes of the strings. 96 *
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 "◌́".
49 */ 108 */
50 bool operator ==(String other); 109 bool operator ==(var other);
51 110
52 /** 111 /**
53 * Returns whether this string ends with [other]. 112 * Returns whether this string ends with [other].
54 */ 113 */
55 bool endsWith(String other); 114 bool endsWith(String other);
56 115
57 /** 116 /**
58 * Returns whether this string starts with [other]. 117 * Returns whether this string starts with [other].
59 */ 118 */
60 bool startsWith(String other); 119 bool startsWith(String other);
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 * 204 *
146 * 205 *
147 * The [replace] function is called with the [Match] generated 206 * The [replace] function is called with the [Match] generated
148 * by the pattern, and its result is used as replacement. 207 * by the pattern, and its result is used as replacement.
149 */ 208 */
150 String replaceAllMapped(Pattern from, String replace(Match match)); 209 String replaceAllMapped(Pattern from, String replace(Match match));
151 210
152 /** 211 /**
153 * Splits the string around matches of [pattern]. Returns 212 * Splits the string around matches of [pattern]. Returns
154 * a list of substrings. 213 * 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))
155 */ 227 */
156 List<String> split(Pattern pattern); 228 List<String> split(Pattern pattern);
157 229
158 /** 230 /**
159 * Returns a list of the characters of this string. 231 * Returns a list of the individual code-units converted to strings.
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));
160 */ 239 */
161 List<String> splitChars(); 240 List<String> splitChars();
162 241
163 /** 242 /**
164 * Splits the string on the [pattern], then converts each part and each match. 243 * Splits the string on the [pattern], then converts each part and each match.
165 * 244 *
166 * The pattern is used to split the string into parts and separating matches. 245 * The pattern is used to split the string into parts and separating matches.
167 * 246 *
168 * Each match is converted to a string by calling [onMatch]. If [onMatch] 247 * Each match is converted to a string by calling [onMatch]. If [onMatch]
169 * is omitted, the matched string is used. 248 * is omitted, the matched string is used.
170 * 249 *
171 * Each non-matched part is converted by a call to [onNonMatch]. If 250 * Each non-matched part is converted by a call to [onNonMatch]. If
172 * [onNonMatch] is omitted, the non-matching part is used. 251 * [onNonMatch] is omitted, the non-matching part is used.
173 * 252 *
174 * Then all the converted parts are combined into the resulting string. 253 * Then all the converted parts are combined into the resulting string.
175 */ 254 */
176 String splitMapJoin(Pattern pattern, 255 String splitMapJoin(Pattern pattern,
177 {String onMatch(Match match), 256 {String onMatch(Match match),
178 String onNonMatch(String nonMatch)}); 257 String onNonMatch(String nonMatch)});
179 258
180 /** 259 /**
181 * Returns a list of the scalar character codes of this string. 260 * Returns a list of UTF-16 code units of this string.
261 *
262 * *This getter is deprecated. Use [codeUnits] instead.*
182 */ 263 */
183 List<int> get charCodes; 264 List<int> get charCodes;
184 265
185 /** 266 /**
267 * Returns an iterable of the UTF-16 code units of this string.
268 */
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 /**
186 * If this string is not already all lower case, returns a new string 284 * If this string is not already all lower case, returns a new string
187 * where all characters are made lower case. Returns [:this:] otherwise. 285 * where all characters are made lower case. Returns [:this:] otherwise.
188 */ 286 */
287 // TODO(floitsch): document better. (See EcmaScript for description).
189 String toLowerCase(); 288 String toLowerCase();
190 289
191 /** 290 /**
192 * If this string is not already all uper case, returns a new string 291 * If this string is not already all upper case, returns a new string
193 * where all characters are made upper case. Returns [:this:] otherwise. 292 * where all characters are made upper case. Returns [:this:] otherwise.
194 */ 293 */
294 // TODO(floitsch): document better. (See EcmaScript for description).
195 String toUpperCase(); 295 String toUpperCase();
196 } 296 }
OLDNEW
« no previous file with comments | « sdk/lib/_internal/compiler/implementation/lib/js_string.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698