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

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: 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 | « no previous file | 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 character strings. Strings are
Lasse Reichstein Nielsen 2013/02/01 12:44:10 Newlines after first sentence. "strings" is not a
floitsch 2013/02/01 20:21:15 Done.
9 * immutable. A string is represented by a list of 32-bit Unicode 9 * immutable. A string is represented by a list of 16-bit Unicode
erikcorry 2013/02/01 09:42:50 Unicode code units -> Unicode UTF-16 code units
Lasse Reichstein Nielsen 2013/02/01 12:44:10 "list" means something else in Dart. Again "sequen
floitsch 2013/02/01 20:21:15 Done.
floitsch 2013/02/01 20:21:15 Done.
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.
erikcorry 2013/02/01 09:42:50 Also accessible with []
Lasse Reichstein Nielsen 2013/02/01 12:44:10 That returns a String, not a code unit.
floitsch 2013/02/01 20:21:15 reworded. PTAL.
12 *
13 * Strings are encoded in Utf16. Decoding Utf16, which combines
erikcorry 2013/02/01 09:42:50 Correct spelling is "UTF-16". Our style guide and
Lasse Reichstein Nielsen 2013/02/01 12:44:10 "The characters of a string are encoded as UTF-16
floitsch 2013/02/01 20:21:15 Done.
floitsch 2013/02/01 20:21:15 Done.
14 * surrogate pairs, yields Unicode code-points. Following a similar
Lasse Reichstein Nielsen 2013/02/01 12:44:10 No dash in "code points". It's two words.
floitsch 2013/02/01 20:21:15 Done.
15 * terminology as Go we call Unicode code-points "runes". The 32-bit
erikcorry 2013/02/01 09:42:50 as -> to
Lasse Reichstein Nielsen 2013/02/01 12:44:10 "we call X Y" puts two names right next to each ot
floitsch 2013/02/01 20:21:15 Done.
16 * rune value is accessible through the [runes] getter.
12 */ 17 */
13 abstract class String implements Comparable, Pattern { 18 abstract class String implements Comparable, Pattern {
14 /** 19 /**
15 * Allocates a new String for the specified [charCodes]. 20 * Allocates a new String for the specified [charCodes].
21 *
22 * The [charCodes] can be code-units or runes. If a char-code value is
erikcorry 2013/02/01 09:42:50 code units -> UTF-16 code units
floitsch 2013/02/01 20:21:15 Done.
23 * 16-bit it is copied verbatim. If it is greater than 16 bits it is
24 * decomposed into a surrogate pair.
16 */ 25 */
17 external factory String.fromCharCodes(List<int> charCodes); 26 external factory String.fromCharCodes(Iterable<int> charCodes);
27
28 /**
29 * *Deprecated*. Use [String.fromCharCode] instead.
30 */
31 factory String.character(int charCode) => new String.fromCharCode(charCode);
18 32
19 /** 33 /**
20 * Allocates a new String for the specified [charCode]. 34 * Allocates a new String for the specified [charCode].
21 * 35 *
22 * The built string is of [length] one, if the [charCode] lies inside the 36 * The built string is of [length] one, if the [charCode] is less than
Lasse Reichstein Nielsen 2013/02/01 12:44:10 "The new string contains a single code unit if the
floitsch 2013/02/01 20:21:15 Done.
23 * basic multilingual plane (plane 0). Otherwise the [length] is 2 and 37 * 16 bits. Otherwise the [length] is 2 and the code units form a surrogate
24 * the code units form a surrogate pair. 38 * pair.
39 *
40 * It is allowed (though generally discouraged) to create a String with only
41 * one half of a surrogate pair.
25 */ 42 */
26 factory String.character(int charCode) { 43 factory String.fromCharCode(int charCode) {
27 List<int> charCodes = new List<int>.fixedLength(1, fill: charCode); 44 List<int> charCodes = new List<int>.fixedLength(1, fill: charCode);
28 return new String.fromCharCodes(charCodes); 45 return new String.fromCharCodes(charCodes);
29 } 46 }
30 47
31 /** 48 /**
32 * Gets the character (as [String]) at the given [index]. 49 * Gets the character (as [String]) at the given [index].
erikcorry 2013/02/01 09:42:50 Clarification: This is UTF_16 code-unit based and
floitsch 2013/02/01 20:21:15 Reworded and added example.
33 */ 50 */
34 String operator [](int index); 51 String operator [](int index);
35 52
36 /** 53 /**
37 * Gets the scalar character code at the given [index]. 54 * Gets the scalar character code at the given [index].
55 *
56 * *This method is deprecated. Please use [codeUnitAt] instead.*
38 */ 57 */
39 int charCodeAt(int index); 58 int charCodeAt(int index);
40 59
41 /** 60 /**
61 * Returns the code-unit (16-bit) at the given [index].
erikcorry 2013/02/01 09:42:50 16-bit -> 16 bit UTF-16
floitsch 2013/02/01 20:21:15 Done.
62 */
63 int codeUnitAt(int index);
64
65 /**
42 * The length of the string. 66 * The length of the string.
67 *
68 * Returns the number of 16-bit code units in this string. The number
erikcorry 2013/02/01 09:42:50 code units -> UTF-16 code units
floitsch 2013/02/01 20:21:15 Done.
69 * of [runes] might be less, if the string contains characters outside
70 * the basic multilingual plane (plane 0).
43 */ 71 */
44 int get length; 72 int get length;
45 73
46 /** 74 /**
47 * Returns whether the two strings are equal. This method compares 75 * Returns whether the two strings are equal.
48 * each individual scalar character codes of the strings. 76 *
77 * This method compares each individual code unit of the strings. It does not
78 * check for Unicode equivalence. For example the two following strings both
79 * represent the string "Amélie" but, due to their different encoding will
80 * not return equal.
81 *
82 * "Am\xe9lie"
83 * "Ame\u{301}lie"
84 *
85 * In the first string the "é" is encoded as a single unicode code unit,
86 * whereas the second string encodes it as "e" with the combining
87 * accent character "◌́".
49 */ 88 */
50 bool operator ==(String other); 89 bool operator ==(String other);
Lasse Reichstein Nielsen 2013/02/01 12:44:10 Shouldn't this be bool operator==(Object other)
floitsch 2013/02/01 20:21:15 yes. Changed to "var".
51 90
52 /** 91 /**
53 * Returns whether this string ends with [other]. 92 * Returns whether this string ends with [other].
54 */ 93 */
55 bool endsWith(String other); 94 bool endsWith(String other);
56 95
57 /** 96 /**
58 * Returns whether this string starts with [other]. 97 * Returns whether this string starts with [other].
59 */ 98 */
60 bool startsWith(String other); 99 bool startsWith(String other);
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 * 179 *
141 * 180 *
142 * The [replace] function is called with the [Match] generated 181 * The [replace] function is called with the [Match] generated
143 * by the pattern, and its result is used as replacement. 182 * by the pattern, and its result is used as replacement.
144 */ 183 */
145 String replaceAllMapped(Pattern from, String replace(Match match)); 184 String replaceAllMapped(Pattern from, String replace(Match match));
146 185
147 /** 186 /**
148 * Splits the string around matches of [pattern]. Returns 187 * Splits the string around matches of [pattern]. Returns
149 * a list of substrings. 188 * a list of substrings.
189 *
190 * Splitting with an empty string pattern (`""`) splits at code unit
erikcorry 2013/02/01 09:42:50 code unit -> UTF-16 code unit
floitsch 2013/02/01 20:21:15 Done.
191 * boundaries and not at rune boundaries. The following two expressions
192 * are hence equivalent:
193 *
194 * string.split("")
195 * string.codeUnits.map((unit) => new String.character(unit))
196 *
197 * Unless it guaranteed that the string is in the basic multilingual plane
198 * (meaning that a code-unit represents a rune) it is often better to
erikcorry 2013/02/01 09:42:50 a -> each
floitsch 2013/02/01 20:21:15 Done.
199 * map the runes instead:
200 *
201 * string.runes.map((rune) => new String.character(rune))
150 */ 202 */
151 List<String> split(Pattern pattern); 203 List<String> split(Pattern pattern);
152 204
153 /** 205 /**
154 * Returns a list of the characters of this string. 206 * Returns a list of the individual code-units characters of this string.
207 *
208 * *Deprecated*
209 * If you want to split on code-unit boundaries, use [split]. If you
210 * want to split on rune boundaries, use [runes] and map the result.
erikcorry 2013/02/01 09:42:50 I feel this comment would benefit from an example:
floitsch 2013/02/01 20:21:15 Shouldn't be necessary since the method is going a
155 */ 211 */
156 List<String> splitChars(); 212 List<String> splitChars();
157 213
158 /** 214 /**
159 * Splits the string on the [pattern], then converts each part and each match. 215 * Splits the string on the [pattern], then converts each part and each match.
160 * 216 *
161 * The pattern is used to split the string into parts and separating matches. 217 * The pattern is used to split the string into parts and separating matches.
162 * 218 *
163 * Each match is converted to a string by calling [onMatch]. If [onMatch] 219 * Each match is converted to a string by calling [onMatch]. If [onMatch]
164 * is omitted, the matched string is used. 220 * is omitted, the matched string is used.
165 * 221 *
166 * Each non-matched part is converted by a call to [onNonMatch]. If 222 * Each non-matched part is converted by a call to [onNonMatch]. If
167 * [onNonMatch] is omitted, the non-matching part is used. 223 * [onNonMatch] is omitted, the non-matching part is used.
168 * 224 *
169 * Then all the converted parts are combined into the resulting string. 225 * Then all the converted parts are combined into the resulting string.
170 */ 226 */
171 String splitMapJoin(Pattern pattern, 227 String splitMapJoin(Pattern pattern,
172 {String onMatch(Match match), 228 {String onMatch(Match match),
173 String onNonMatch(String nonMatch)}); 229 String onNonMatch(String nonMatch)});
174 230
175 /** 231 /**
176 * Returns a list of the scalar character codes of this string. 232 * Returns a list of 16-bit code-units of this string.
erikcorry 2013/02/01 09:42:50 code-units -> UTF-16 code units
floitsch 2013/02/01 20:21:15 Done.
233 *
234 * *This getter is deprecated. Use [codeUnits] instead.*
177 */ 235 */
178 List<int> get charCodes; 236 List<int> get charCodes;
179 237
180 /** 238 /**
239 * Returns an iterable of the 16-bit code-units of this string.
erikcorry 2013/02/01 09:42:50 And here
floitsch 2013/02/01 20:21:15 Done.
240 */
241 // TODO(floitsch): should it return a list?
242 // TODO(floitsch): make it a bidirectional iterator.
243 Iterable<int> get codeUnits;
244
245 /**
246 * Returns an iterable of Unicode code-points of this string.
247 *
248 * If the string contains surrogate pairs, they will be combined and returned
249 * as one integer by this iterator. Unmatched surrogate halves are treated
250 * like valid 16-bit code-units.
251 */
252 // TODO(floitsch): make it a bidirectional iterator.
Lasse Reichstein Nielsen 2013/02/01 12:44:10 Let's make it a Runes class with extra functionali
floitsch 2013/02/01 20:21:15 Changed TODO. I will see that I can commit this CL
253 Iterable<int> get runes;
254
255 /**
181 * If this string is not already all lower case, returns a new string 256 * If this string is not already all lower case, returns a new string
182 * where all characters are made lower case. Returns [:this:] otherwise. 257 * where all characters are made lower case. Returns [:this:] otherwise.
erikcorry 2013/02/01 09:42:50 double space
Lasse Reichstein Nielsen 2013/02/01 12:44:10 You need to say how upper-casing is done. ASCII on
floitsch 2013/02/01 20:21:15 Done.
floitsch 2013/02/01 20:21:15 Same as JavaScript: using the locale-independent U
183 */ 258 */
184 String toLowerCase(); 259 String toLowerCase();
185 260
186 /** 261 /**
187 * If this string is not already all uper case, returns a new string 262 * If this string is not already all uper case, returns a new string
erikcorry 2013/02/01 09:42:50 uper -> upper
floitsch 2013/02/01 20:21:15 Done.
188 * where all characters are made upper case. Returns [:this:] otherwise. 263 * where all characters are made upper case. Returns [:this:] otherwise.
189 */ 264 */
190 String toUpperCase(); 265 String toUpperCase();
191 } 266 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698