| 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 * A sequence of characters. | 8 * A sequence of characters. |
| 9 * | 9 * |
| 10 * A string can be either single or multiline. Single line strings are | 10 * A string can be either single or multiline. Single line strings are |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 * [RegExp] to work with regular expressions. | 84 * [RegExp] to work with regular expressions. |
| 85 * | 85 * |
| 86 * Also see: | 86 * Also see: |
| 87 | 87 |
| 88 * * [Dart Cookbook](https://www.dartlang.org/docs/cookbook/#strings) | 88 * * [Dart Cookbook](https://www.dartlang.org/docs/cookbook/#strings) |
| 89 * for String examples and recipes. | 89 * for String examples and recipes. |
| 90 * * [Dart Up and Running] | 90 * * [Dart Up and Running] |
| 91 * (https://www.dartlang.org/docs/dart-up-and-running/contents/ch03.html#ch03-st
rings-and-regular-expressions) | 91 * (https://www.dartlang.org/docs/dart-up-and-running/contents/ch03.html#ch03-st
rings-and-regular-expressions) |
| 92 */ | 92 */ |
| 93 abstract class String implements Comparable<String>, Pattern { | 93 abstract class String implements Comparable<String>, Pattern { |
| 94 @patch | 94 /** |
| 95 * Allocates a new String for the specified [charCodes]. |
| 96 * |
| 97 * The [charCodes] can be UTF-16 code units or runes. If a char-code value is |
| 98 * 16-bit, it is copied verbatim: |
| 99 * |
| 100 * new String.fromCharCodes([68]); // 'D' |
| 101 * |
| 102 * If a char-code value is greater than 16-bits, it is decomposed into a |
| 103 * surrogate pair: |
| 104 * |
| 105 * var clef = new String.fromCharCodes([0x1D11E]); |
| 106 * clef.codeUnitAt(0); // 0xD834 |
| 107 * clef.codeUnitAt(1); // 0xDD1E |
| 108 * |
| 109 * If [start] and [end] is provided, only the values of [charCodes] |
| 110 * at positions from `start` to, but not including, `end`, are used. |
| 111 * The `start` and `end` values must satisfy |
| 112 * `0 <= start <= end <= charCodes.length`. |
| 113 */ |
| 95 factory String.fromCharCodes(Iterable<int> charCodes, | 114 factory String.fromCharCodes(Iterable<int> charCodes, |
| 96 [int start = 0, int end]) { | 115 [int start = 0, int end]) { |
| 97 // If possible, recognize typed lists too. | 116 // If possible, recognize typed lists too. |
| 98 if (charCodes is! JSArray) { | 117 if (charCodes is! JSArray) { |
| 99 return _stringFromIterable(charCodes, start, end); | 118 return _stringFromIterable(charCodes, start, end); |
| 100 } | 119 } |
| 101 | 120 |
| 102 List list = charCodes; | 121 List list = charCodes; |
| 103 int len = list.length; | 122 int len = list.length; |
| 104 if (start < 0 || start > len) { | 123 if (start < 0 || start > len) { |
| 105 throw new RangeError.range(start, 0, len); | 124 throw new RangeError.range(start, 0, len); |
| 106 } | 125 } |
| 107 if (end == null) { | 126 if (end == null) { |
| 108 end = len; | 127 end = len; |
| 109 } else if (end < start || end > len) { | 128 } else if (end < start || end > len) { |
| 110 throw new RangeError.range(end, start, len); | 129 throw new RangeError.range(end, start, len); |
| 111 } | 130 } |
| 112 | 131 |
| 113 if (start > 0 || end < len) { | 132 if (start > 0 || end < len) { |
| 114 list = list.sublist(start, end); | 133 list = list.sublist(start, end); |
| 115 } | 134 } |
| 116 return Primitives.stringFromCharCodes(list); | 135 return Primitives.stringFromCharCodes(list); |
| 117 } | 136 } |
| 118 | 137 |
| 119 @patch | 138 /** |
| 139 * Allocates a new String for the specified [charCode]. |
| 140 * |
| 141 * If the [charCode] can be represented by a single UTF-16 code unit, the new |
| 142 * string contains a single code unit. Otherwise, the [length] is 2 and |
| 143 * the code units form a surrogate pair. See documentation for |
| 144 * [fromCharCodes]. |
| 145 * |
| 146 * Creating a String with half of a surrogate pair is allowed. |
| 147 */ |
| 120 factory String.fromCharCode(int charCode) { | 148 factory String.fromCharCode(int charCode) { |
| 121 return Primitives.stringFromCharCode(charCode); | 149 return Primitives.stringFromCharCode(charCode); |
| 122 } | 150 } |
| 123 | 151 |
| 124 @patch | 152 /** |
| 153 * Returns the string value of the environment declaration [name]. |
| 154 * |
| 155 * Environment declarations are provided by the surrounding system compiling |
| 156 * or running the Dart program. Declarations map a string key to a string |
| 157 * value. |
| 158 * |
| 159 * If [name] is not declared in the environment, the result is instead |
| 160 * [defaultValue]. |
| 161 * |
| 162 * Example of getting a value: |
| 163 * |
| 164 * const String.fromEnvironment("defaultFloo", defaultValue: "no floo") |
| 165 * |
| 166 * Example of checking whether a declaration is there at all: |
| 167 * |
| 168 * var isDeclared = const String.fromEnvironment("maybeDeclared") != null; |
| 169 */ |
| 125 factory String.fromEnvironment(String name, {String defaultValue}) { | 170 factory String.fromEnvironment(String name, {String defaultValue}) { |
| 126 throw new UnsupportedError( | 171 throw new UnsupportedError( |
| 127 'String.fromEnvironment can only be used as a const constructor'); | 172 'String.fromEnvironment can only be used as a const constructor'); |
| 128 } | 173 } |
| 129 | 174 |
| 130 /** | 175 /** |
| 131 * Gets the character (as a single-code-unit [String]) at the given [index]. | 176 * Gets the character (as a single-code-unit [String]) at the given [index]. |
| 132 * | 177 * |
| 133 * The returned string represents exactly one UTF-16 code unit, which may be | 178 * The returned string represents exactly one UTF-16 code unit, which may be |
| 134 * half of a surrogate pair. A single member of a surrogate pair is an | 179 * half of a surrogate pair. A single member of a surrogate pair is an |
| (...skipping 602 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 737 _position = position - 1; | 782 _position = position - 1; |
| 738 _currentCodePoint = _combineSurrogatePair(prevCodeUnit, codeUnit); | 783 _currentCodePoint = _combineSurrogatePair(prevCodeUnit, codeUnit); |
| 739 return true; | 784 return true; |
| 740 } | 785 } |
| 741 } | 786 } |
| 742 _position = position; | 787 _position = position; |
| 743 _currentCodePoint = codeUnit; | 788 _currentCodePoint = codeUnit; |
| 744 return true; | 789 return true; |
| 745 } | 790 } |
| 746 } | 791 } |
| OLD | NEW |