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 import 'dart:math'; |
6 | 6 |
7 import 'dart:math'; | 7 import 'package:analyzer_plugin/src/utilities/string_utilities.dart'; |
8 | 8 |
9 /** | 9 /** |
10 * "$" | 10 * "$" |
11 */ | 11 */ |
12 const int CHAR_DOLLAR = 0x24; | 12 const int CHAR_DOLLAR = 0x24; |
13 | 13 |
14 /** | 14 /** |
15 * "." | 15 * "." |
16 */ | 16 */ |
17 const int CHAR_DOT = 0x2E; | 17 const int CHAR_DOT = 0x2E; |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 int n = min(a_length, b_length); | 152 int n = min(a_length, b_length); |
153 for (int i = 1; i <= n; i++) { | 153 for (int i = 1; i <= n; i++) { |
154 if (a.codeUnitAt(a_length - i) != b.codeUnitAt(b_length - i)) { | 154 if (a.codeUnitAt(a_length - i) != b.codeUnitAt(b_length - i)) { |
155 return i - 1; | 155 return i - 1; |
156 } | 156 } |
157 } | 157 } |
158 return n; | 158 return n; |
159 } | 159 } |
160 | 160 |
161 /** | 161 /** |
162 * Returns a list of words for the given camel case string. | |
163 * | |
164 * 'getCamelWords' => ['get', 'Camel', 'Words'] | |
165 * 'getHTMLText' => ['get', 'HTML', 'Text'] | |
166 */ | |
167 List<String> getCamelWords(String str) { | |
168 if (str == null || str.isEmpty) { | |
169 return <String>[]; | |
170 } | |
171 List<String> parts = <String>[]; | |
172 bool wasLowerCase = false; | |
173 bool wasUpperCase = false; | |
174 int wordStart = 0; | |
175 for (int i = 0; i < str.length; i++) { | |
176 int c = str.codeUnitAt(i); | |
177 var newLowerCase = isLowerCase(c); | |
178 var newUpperCase = isUpperCase(c); | |
179 // myWord | |
180 // | ^ | |
181 if (wasLowerCase && newUpperCase) { | |
182 parts.add(str.substring(wordStart, i)); | |
183 wordStart = i; | |
184 } | |
185 // myHTMLText | |
186 // | ^ | |
187 if (wasUpperCase && | |
188 newUpperCase && | |
189 i + 1 < str.length && | |
190 isLowerCase(str.codeUnitAt(i + 1))) { | |
191 parts.add(str.substring(wordStart, i)); | |
192 wordStart = i; | |
193 } | |
194 wasLowerCase = newLowerCase; | |
195 wasUpperCase = newUpperCase; | |
196 } | |
197 parts.add(str.substring(wordStart)); | |
198 return parts; | |
199 } | |
200 | |
201 /** | |
202 * Checks if [str] is `null`, empty or is whitespace. | 162 * Checks if [str] is `null`, empty or is whitespace. |
203 */ | 163 */ |
204 bool isBlank(String str) { | 164 bool isBlank(String str) { |
205 if (str == null) { | 165 if (str == null) { |
206 return true; | 166 return true; |
207 } | 167 } |
208 if (str.isEmpty) { | 168 if (str.isEmpty) { |
209 return true; | 169 return true; |
210 } | 170 } |
211 return str.codeUnits.every(isSpace); | 171 return str.codeUnits.every(isSpace); |
212 } | 172 } |
213 | 173 |
214 bool isDigit(int c) { | 174 bool isDigit(int c) { |
215 return c >= 0x30 && c <= 0x39; | 175 return c >= 0x30 && c <= 0x39; |
216 } | 176 } |
217 | 177 |
218 bool isEmpty(String str) { | |
219 return str == null || str.isEmpty; | |
220 } | |
221 | |
222 bool isEOL(int c) { | 178 bool isEOL(int c) { |
223 return c == 0x0D || c == 0x0A; | 179 return c == 0x0D || c == 0x0A; |
224 } | 180 } |
225 | 181 |
226 bool isLetter(int c) { | 182 bool isLetter(int c) { |
227 return (c >= 0x41 && c <= 0x5A) || (c >= 0x61 && c <= 0x7A); | 183 return (c >= 0x41 && c <= 0x5A) || (c >= 0x61 && c <= 0x7A); |
228 } | 184 } |
229 | 185 |
230 bool isLetterOrDigit(int c) { | 186 bool isLetterOrDigit(int c) { |
231 return isLetter(c) || isDigit(c); | 187 return isLetter(c) || isDigit(c); |
232 } | 188 } |
233 | 189 |
234 bool isLowerCase(int c) { | |
235 return c >= 0x61 && c <= 0x7A; | |
236 } | |
237 | |
238 bool isSpace(int c) => c == 0x20 || c == 0x09; | 190 bool isSpace(int c) => c == 0x20 || c == 0x09; |
239 | 191 |
240 bool isUpperCase(int c) { | |
241 return c >= 0x41 && c <= 0x5A; | |
242 } | |
243 | |
244 bool isWhitespace(int c) { | 192 bool isWhitespace(int c) { |
245 return isSpace(c) || isEOL(c); | 193 return isSpace(c) || isEOL(c); |
246 } | 194 } |
247 | 195 |
248 String remove(String str, String remove) { | 196 String remove(String str, String remove) { |
249 if (isEmpty(str) || isEmpty(remove)) { | 197 if (isEmpty(str) || isEmpty(remove)) { |
250 return str; | 198 return str; |
251 } | 199 } |
252 return str.replaceAll(remove, ''); | 200 return str.replaceAll(remove, ''); |
253 } | 201 } |
254 | 202 |
255 String removeEnd(String str, String remove) { | 203 String removeEnd(String str, String remove) { |
256 if (isEmpty(str) || isEmpty(remove)) { | 204 if (isEmpty(str) || isEmpty(remove)) { |
257 return str; | 205 return str; |
258 } | 206 } |
259 if (str.endsWith(remove)) { | 207 if (str.endsWith(remove)) { |
260 return str.substring(0, str.length - remove.length); | 208 return str.substring(0, str.length - remove.length); |
261 } | 209 } |
262 return str; | 210 return str; |
263 } | 211 } |
264 | 212 |
265 String removeStart(String str, String remove) { | |
266 if (isEmpty(str) || isEmpty(remove)) { | |
267 return str; | |
268 } | |
269 if (str.startsWith(remove)) { | |
270 return str.substring(remove.length); | |
271 } | |
272 return str; | |
273 } | |
274 | |
275 String repeat(String s, int n) { | 213 String repeat(String s, int n) { |
276 StringBuffer sb = new StringBuffer(); | 214 StringBuffer sb = new StringBuffer(); |
277 for (int i = 0; i < n; i++) { | 215 for (int i = 0; i < n; i++) { |
278 sb.write(s); | 216 sb.write(s); |
279 } | 217 } |
280 return sb.toString(); | 218 return sb.toString(); |
281 } | 219 } |
282 | 220 |
283 /** | 221 /** |
284 * Gets the substring after the last occurrence of a separator. | 222 * Gets the substring after the last occurrence of a separator. |
(...skipping 17 matching lines...) Expand all Loading... |
302 * Information about a single replacement that should be made to convert the | 240 * Information about a single replacement that should be made to convert the |
303 * "old" string to the "new" one. | 241 * "old" string to the "new" one. |
304 */ | 242 */ |
305 class SimpleDiff { | 243 class SimpleDiff { |
306 final int offset; | 244 final int offset; |
307 final int length; | 245 final int length; |
308 final String replacement; | 246 final String replacement; |
309 | 247 |
310 SimpleDiff(this.offset, this.length, this.replacement); | 248 SimpleDiff(this.offset, this.length, this.replacement); |
311 } | 249 } |
OLD | NEW |