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 /** | 9 /** |
10 * "$" | 10 * "$" |
(...skipping 23 matching lines...) Expand all Loading... |
34 } | 34 } |
35 if (a == null) { | 35 if (a == null) { |
36 return 1; | 36 return 1; |
37 } | 37 } |
38 if (b == null) { | 38 if (b == null) { |
39 return -1; | 39 return -1; |
40 } | 40 } |
41 return a.compareTo(b); | 41 return a.compareTo(b); |
42 } | 42 } |
43 | 43 |
| 44 /** |
| 45 * Return a simple difference between the given [oldStr] and [newStr]. |
| 46 */ |
| 47 SimpleDiff computeSimpleDiff(String oldStr, String newStr) { |
| 48 int prefixLength = findCommonPrefix(oldStr, newStr); |
| 49 int suffixLength = findCommonSuffix(oldStr, newStr); |
| 50 while (prefixLength >= 0) { |
| 51 int oldReplaceLength = oldStr.length - prefixLength - suffixLength; |
| 52 int newReplaceLength = newStr.length - prefixLength - suffixLength; |
| 53 if (oldReplaceLength >= 0 && newReplaceLength >= 0) { |
| 54 return new SimpleDiff(prefixLength, oldReplaceLength, |
| 55 newStr.substring(prefixLength, newStr.length - suffixLength)); |
| 56 } |
| 57 prefixLength--; |
| 58 } |
| 59 return new SimpleDiff(0, oldStr.length, newStr); |
| 60 } |
| 61 |
44 int countLeadingWhitespaces(String str) { | 62 int countLeadingWhitespaces(String str) { |
45 int i = 0; | 63 int i = 0; |
46 for (; i < str.length; i++) { | 64 for (; i < str.length; i++) { |
47 int c = str.codeUnitAt(i); | 65 int c = str.codeUnitAt(i); |
48 if (!isWhitespace(c)) { | 66 if (!isWhitespace(c)) { |
49 break; | 67 break; |
50 } | 68 } |
51 } | 69 } |
52 return i; | 70 return i; |
53 } | 71 } |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 } | 290 } |
273 if (isEmpty(separator)) { | 291 if (isEmpty(separator)) { |
274 return ''; | 292 return ''; |
275 } | 293 } |
276 int pos = str.lastIndexOf(separator); | 294 int pos = str.lastIndexOf(separator); |
277 if (pos == -1) { | 295 if (pos == -1) { |
278 return str; | 296 return str; |
279 } | 297 } |
280 return str.substring(pos + separator.length); | 298 return str.substring(pos + separator.length); |
281 } | 299 } |
| 300 |
| 301 /** |
| 302 * Information about a single replacement that should be made to convert the |
| 303 * "old" string to the "new" one. |
| 304 */ |
| 305 class SimpleDiff { |
| 306 final int offset; |
| 307 final int length; |
| 308 final String replacement; |
| 309 |
| 310 SimpleDiff(this.offset, this.length, this.replacement); |
| 311 } |
OLD | NEW |