OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library services.src.correction.strings; | 5 library services.src.correction.strings; |
6 | 6 |
7 import 'dart:math'; | 7 import 'dart:math'; |
8 | 8 |
9 | |
10 /** | 9 /** |
11 * "$" | 10 * "$" |
12 */ | 11 */ |
13 const int CHAR_DOLLAR = 0x24; | 12 const int CHAR_DOLLAR = 0x24; |
14 | 13 |
15 /** | 14 /** |
16 * "." | 15 * "." |
17 */ | 16 */ |
18 const int CHAR_DOT = 0x2E; | 17 const int CHAR_DOT = 0x2E; |
19 | 18 |
20 /** | 19 /** |
21 * "_" | 20 * "_" |
22 */ | 21 */ |
23 const int CHAR_UNDERSCORE = 0x5F; | 22 const int CHAR_UNDERSCORE = 0x5F; |
24 | 23 |
25 | |
26 String capitalize(String str) { | 24 String capitalize(String str) { |
27 if (isEmpty(str)) { | 25 if (isEmpty(str)) { |
28 return str; | 26 return str; |
29 } | 27 } |
30 return str.substring(0, 1).toUpperCase() + str.substring(1); | 28 return str.substring(0, 1).toUpperCase() + str.substring(1); |
31 } | 29 } |
32 | 30 |
33 int compareStrings(String a, String b) { | 31 int compareStrings(String a, String b) { |
34 if (a == b) { | 32 if (a == b) { |
35 return 0; | 33 return 0; |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 bool isSpace(int c) => c == 0x20 || c == 0x09; | 154 bool isSpace(int c) => c == 0x20 || c == 0x09; |
157 | 155 |
158 bool isUpperCase(int c) { | 156 bool isUpperCase(int c) { |
159 return c >= 0x41 && c <= 0x5A; | 157 return c >= 0x41 && c <= 0x5A; |
160 } | 158 } |
161 | 159 |
162 bool isWhitespace(int c) { | 160 bool isWhitespace(int c) { |
163 return isSpace(c) || c == 0x0D || c == 0x0A; | 161 return isSpace(c) || c == 0x0D || c == 0x0A; |
164 } | 162 } |
165 | 163 |
166 | |
167 String remove(String str, String remove) { | 164 String remove(String str, String remove) { |
168 if (isEmpty(str) || isEmpty(remove)) { | 165 if (isEmpty(str) || isEmpty(remove)) { |
169 return str; | 166 return str; |
170 } | 167 } |
171 return str.replaceAll(remove, ''); | 168 return str.replaceAll(remove, ''); |
172 } | 169 } |
173 | 170 |
174 | |
175 String removeEnd(String str, String remove) { | 171 String removeEnd(String str, String remove) { |
176 if (isEmpty(str) || isEmpty(remove)) { | 172 if (isEmpty(str) || isEmpty(remove)) { |
177 return str; | 173 return str; |
178 } | 174 } |
179 if (str.endsWith(remove)) { | 175 if (str.endsWith(remove)) { |
180 return str.substring(0, str.length - remove.length); | 176 return str.substring(0, str.length - remove.length); |
181 } | 177 } |
182 return str; | 178 return str; |
183 } | 179 } |
184 | 180 |
185 | |
186 String removeStart(String str, String remove) { | 181 String removeStart(String str, String remove) { |
187 if (isEmpty(str) || isEmpty(remove)) { | 182 if (isEmpty(str) || isEmpty(remove)) { |
188 return str; | 183 return str; |
189 } | 184 } |
190 if (str.startsWith(remove)) { | 185 if (str.startsWith(remove)) { |
191 return str.substring(remove.length); | 186 return str.substring(remove.length); |
192 } | 187 } |
193 return str; | 188 return str; |
194 } | 189 } |
195 | 190 |
196 | |
197 String repeat(String s, int n) { | 191 String repeat(String s, int n) { |
198 StringBuffer sb = new StringBuffer(); | 192 StringBuffer sb = new StringBuffer(); |
199 for (int i = 0; i < n; i++) { | 193 for (int i = 0; i < n; i++) { |
200 sb.write(s); | 194 sb.write(s); |
201 } | 195 } |
202 return sb.toString(); | 196 return sb.toString(); |
203 } | 197 } |
204 | 198 |
205 | |
206 /** | 199 /** |
207 * Gets the substring after the last occurrence of a separator. | 200 * Gets the substring after the last occurrence of a separator. |
208 * The separator is not returned. | 201 * The separator is not returned. |
209 */ | 202 */ |
210 String substringAfterLast(String str, String separator) { | 203 String substringAfterLast(String str, String separator) { |
211 if (isEmpty(str)) { | 204 if (isEmpty(str)) { |
212 return str; | 205 return str; |
213 } | 206 } |
214 if (isEmpty(separator)) { | 207 if (isEmpty(separator)) { |
215 return ''; | 208 return ''; |
216 } | 209 } |
217 int pos = str.lastIndexOf(separator); | 210 int pos = str.lastIndexOf(separator); |
218 if (pos == -1) { | 211 if (pos == -1) { |
219 return str; | 212 return str; |
220 } | 213 } |
221 return str.substring(pos + separator.length); | 214 return str.substring(pos + separator.length); |
222 } | 215 } |
OLD | NEW |