| 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 | 7 |
| 8 /** | 8 /** |
| 9 * "$" | 9 * "$" |
| 10 */ | 10 */ |
| (...skipping 24 matching lines...) Expand all Loading... |
| 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 /** | 44 /** |
| 45 * Counts how many times [sub] appears in [str]. |
| 46 */ |
| 47 int countMatches(String str, String sub) { |
| 48 if (isEmpty(str) || isEmpty(sub)) { |
| 49 return 0; |
| 50 } |
| 51 int count = 0; |
| 52 int idx = 0; |
| 53 while ((idx = str.indexOf(sub, idx)) != -1) { |
| 54 count++; |
| 55 idx += sub.length; |
| 56 } |
| 57 return count; |
| 58 } |
| 59 |
| 60 /** |
| 45 * Checks if [str] is `null`, empty or is whitespace. | 61 * Checks if [str] is `null`, empty or is whitespace. |
| 46 */ | 62 */ |
| 47 bool isBlank(String str) { | 63 bool isBlank(String str) { |
| 48 if (str == null) { | 64 if (str == null) { |
| 49 return true; | 65 return true; |
| 50 } | 66 } |
| 51 if (str.isEmpty) { | 67 if (str.isEmpty) { |
| 52 return true; | 68 return true; |
| 53 } | 69 } |
| 54 return str.codeUnits.every(isSpace); | 70 return str.codeUnits.every(isSpace); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 String removeStart(String str, String remove) { | 110 String removeStart(String str, String remove) { |
| 95 if (isEmpty(str) || isEmpty(remove)) { | 111 if (isEmpty(str) || isEmpty(remove)) { |
| 96 return str; | 112 return str; |
| 97 } | 113 } |
| 98 if (str.startsWith(remove)) { | 114 if (str.startsWith(remove)) { |
| 99 return str.substring(remove.length); | 115 return str.substring(remove.length); |
| 100 } | 116 } |
| 101 return str; | 117 return str; |
| 102 } | 118 } |
| 103 | 119 |
| 120 |
| 104 String repeat(String s, int n) { | 121 String repeat(String s, int n) { |
| 105 StringBuffer sb = new StringBuffer(); | 122 StringBuffer sb = new StringBuffer(); |
| 106 for (int i = 0; i < n; i++) { | 123 for (int i = 0; i < n; i++) { |
| 107 sb.write(s); | 124 sb.write(s); |
| 108 } | 125 } |
| 109 return sb.toString(); | 126 return sb.toString(); |
| 110 } | 127 } |
| OLD | NEW |