| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.bulenkov.iconloader.util; |
| 18 |
|
| 19 |
import org.jetbrains.annotations.Contract; |
| 20 |
import org.jetbrains.annotations.NonNls; |
| 21 |
import org.jetbrains.annotations.NotNull; |
| 22 |
import org.jetbrains.annotations.Nullable; |
| 23 |
|
| 24 |
import java.util.ArrayList; |
| 25 |
import java.util.Collection; |
| 26 |
import java.util.Collections; |
| 27 |
import java.util.List; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class StringUtil { |
| 33 |
public static List<String> split(String s, String separator) { |
| 34 |
return split(s, separator, true); |
| 35 |
} |
| 36 |
|
| 37 |
public static List<String> split(String s, String separator, |
| 38 |
boolean excludeSeparator) { |
| 39 |
return split(s, separator, excludeSeparator, true); |
| 40 |
} |
| 41 |
|
| 42 |
public static List<String> split(String s, String separator, |
| 43 |
boolean excludeSeparator, boolean excludeEmptyStrings) { |
| 44 |
if (separator.isEmpty()) { |
| 45 |
return Collections.singletonList(s); |
| 46 |
} |
| 47 |
List<String> result = new ArrayList<String>(); |
| 48 |
int pos = 0; |
| 49 |
while (true) { |
| 50 |
int index = s.indexOf(separator, pos); |
| 51 |
if (index == -1) break; |
| 52 |
final int nextPos = index + separator.length(); |
| 53 |
String token = s.substring(pos, excludeSeparator ? index : nextPos); |
| 54 |
if (!token.isEmpty() || !excludeEmptyStrings) { |
| 55 |
result.add(token); |
| 56 |
} |
| 57 |
pos = nextPos; |
| 58 |
} |
| 59 |
if (pos < s.length() || (!excludeEmptyStrings && pos == s.length())) { |
| 60 |
result.add(s.substring(pos, s.length())); |
| 61 |
} |
| 62 |
return result; |
| 63 |
} |
| 64 |
|
| 65 |
public static int indexOfIgnoreCase(String where, String what, int fromIndex) { |
| 66 |
int targetCount = what.length(); |
| 67 |
int sourceCount = where.length(); |
| 68 |
|
| 69 |
if (fromIndex >= sourceCount) { |
| 70 |
return targetCount == 0 ? sourceCount : -1; |
| 71 |
} |
| 72 |
|
| 73 |
if (fromIndex < 0) { |
| 74 |
fromIndex = 0; |
| 75 |
} |
| 76 |
|
| 77 |
if (targetCount == 0) { |
| 78 |
return fromIndex; |
| 79 |
} |
| 80 |
|
| 81 |
char first = what.charAt(0); |
| 82 |
int max = sourceCount - targetCount; |
| 83 |
|
| 84 |
for (int i = fromIndex; i <= max; i++) { |
| 85 |
|
| 86 |
if (!charsEqualIgnoreCase(where.charAt(i), first)) { |
| 87 |
while (++i <= max && !charsEqualIgnoreCase(where.charAt(i), first)) ; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
if (i <= max) { |
| 92 |
int j = i + 1; |
| 93 |
int end = j + targetCount - 1; |
| 94 |
for (int k = 1; j < end && charsEqualIgnoreCase(where.charAt(j), what.charAt(k)); j++, k++) ; |
| 95 |
|
| 96 |
if (j == end) { |
| 97 |
|
| 98 |
return i; |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return -1; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
public static int indexOfIgnoreCase(String where, char what, int fromIndex) { |
| 108 |
int sourceCount = where.length(); |
| 109 |
|
| 110 |
if (fromIndex >= sourceCount) { |
| 111 |
return -1; |
| 112 |
} |
| 113 |
|
| 114 |
if (fromIndex < 0) { |
| 115 |
fromIndex = 0; |
| 116 |
} |
| 117 |
|
| 118 |
for (int i = fromIndex; i < sourceCount; i++) { |
| 119 |
if (charsEqualIgnoreCase(where.charAt(i), what)) { |
| 120 |
return i; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
return -1; |
| 125 |
} |
| 126 |
|
| 127 |
public static boolean containsIgnoreCase(String where, String what) { |
| 128 |
return indexOfIgnoreCase(where, what, 0) >= 0; |
| 129 |
} |
| 130 |
|
| 131 |
public static boolean charsEqualIgnoreCase(char a, char b) { |
| 132 |
return a == b || toUpperCase(a) == toUpperCase(b) || toLowerCase(a) == toLowerCase(b); |
| 133 |
} |
| 134 |
|
| 135 |
public static char toUpperCase(char a) { |
| 136 |
if (a < 'a') { |
| 137 |
return a; |
| 138 |
} |
| 139 |
if (a <= 'z') { |
| 140 |
return (char) (a + ('A' - 'a')); |
| 141 |
} |
| 142 |
return Character.toUpperCase(a); |
| 143 |
} |
| 144 |
|
| 145 |
public static char toLowerCase(char a) { |
| 146 |
if (a < 'A' || a >= 'a' && a <= 'z') { |
| 147 |
return a; |
| 148 |
} |
| 149 |
|
| 150 |
if (a <= 'Z') { |
| 151 |
return (char) (a + ('a' - 'A')); |
| 152 |
} |
| 153 |
|
| 154 |
return Character.toLowerCase(a); |
| 155 |
} |
| 156 |
|
| 157 |
public static int compareVersionNumbers(String v1, String v2) { |
| 158 |
if (v1 == null && v2 == null) { |
| 159 |
return 0; |
| 160 |
} |
| 161 |
if (v1 == null) { |
| 162 |
return -1; |
| 163 |
} |
| 164 |
if (v2 == null) { |
| 165 |
return 1; |
| 166 |
} |
| 167 |
|
| 168 |
String[] part1 = v1.split("[\\.\\_\\-]"); |
| 169 |
String[] part2 = v2.split("[\\.\\_\\-]"); |
| 170 |
|
| 171 |
int idx = 0; |
| 172 |
for (; idx < part1.length && idx < part2.length; idx++) { |
| 173 |
String p1 = part1[idx]; |
| 174 |
String p2 = part2[idx]; |
| 175 |
|
| 176 |
int cmp; |
| 177 |
if (p1.matches("\\d+") && p2.matches("\\d+")) { |
| 178 |
cmp = new Integer(p1).compareTo(new Integer(p2)); |
| 179 |
} else { |
| 180 |
cmp = part1[idx].compareTo(part2[idx]); |
| 181 |
} |
| 182 |
if (cmp != 0) return cmp; |
| 183 |
} |
| 184 |
|
| 185 |
if (part1.length == part2.length) { |
| 186 |
return 0; |
| 187 |
} else { |
| 188 |
boolean left = part1.length > idx; |
| 189 |
String[] parts = left ? part1 : part2; |
| 190 |
|
| 191 |
for (; idx < parts.length; idx++) { |
| 192 |
String p = parts[idx]; |
| 193 |
int cmp; |
| 194 |
if (p.matches("\\d+")) { |
| 195 |
cmp = new Integer(p).compareTo(0); |
| 196 |
} else { |
| 197 |
cmp = 1; |
| 198 |
} |
| 199 |
if (cmp != 0) return left ? cmp : -cmp; |
| 200 |
} |
| 201 |
return 0; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
public static boolean startsWithChar(CharSequence s, char prefix) { |
| 206 |
return s != null && s.length() != 0 && s.charAt(0) == prefix; |
| 207 |
} |
| 208 |
|
| 209 |
public static boolean endsWithChar(CharSequence s, char suffix) { |
| 210 |
return s != null && s.length() != 0 && s.charAt(s.length() - 1) == suffix; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
public static String stripQuotesAroundValue(String text) { |
| 215 |
if (startsWithChar(text, '\"') || startsWithChar(text, '\'')) text = text.substring(1); |
| 216 |
if (endsWithChar(text, '\"') || endsWithChar(text, '\'')) text = text.substring(0, text.length() - 1); |
| 217 |
return text; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public static boolean startsWithConcatenation(String string, String... prefixes) { |
| 224 |
int offset = 0; |
| 225 |
for (String prefix : prefixes) { |
| 226 |
int prefixLen = prefix.length(); |
| 227 |
if (!string.regionMatches(offset, prefix, 0, prefixLen)) { |
| 228 |
return false; |
| 229 |
} |
| 230 |
offset += prefixLen; |
| 231 |
} |
| 232 |
return true; |
| 233 |
} |
| 234 |
|
| 235 |
public static String getFileExtension(String fileName) { |
| 236 |
int index = fileName.lastIndexOf('.'); |
| 237 |
if (index < 0) return ""; |
| 238 |
return fileName.substring(index + 1); |
| 239 |
} |
| 240 |
|
| 241 |
public static String getFileNameWithoutExtension(String name) { |
| 242 |
int i = name.lastIndexOf('.'); |
| 243 |
if (i != -1) { |
| 244 |
name = name.substring(0, i); |
| 245 |
} |
| 246 |
return name; |
| 247 |
|
| 248 |
} |
| 249 |
|
| 250 |
@NotNull |
| 251 |
@Contract(pure = true) |
| 252 |
public static String join(@NotNull Collection<String> strings, @NotNull String separator) { |
| 253 |
if (strings.size() <= 1) { |
| 254 |
return notNullize(getFirstItem(strings)); |
| 255 |
} |
| 256 |
StringBuilder result = new StringBuilder(); |
| 257 |
join(strings, separator, result); |
| 258 |
return result.toString(); |
| 259 |
} |
| 260 |
|
| 261 |
@Contract(pure = true) |
| 262 |
public static String join(@NotNull Iterable<?> items, @NotNull @NonNls String separator) { |
| 263 |
StringBuilder result = new StringBuilder(); |
| 264 |
for (Object item : items) { |
| 265 |
result.append(item).append(separator); |
| 266 |
} |
| 267 |
if (result.length() > 0) { |
| 268 |
result.setLength(result.length() - separator.length()); |
| 269 |
} |
| 270 |
return result.toString(); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
@NotNull |
| 275 |
public static String notNullize(@Nullable final String s) { |
| 276 |
return notNullize(s, ""); |
| 277 |
} |
| 278 |
|
| 279 |
@NotNull |
| 280 |
public static String notNullize(@Nullable final String s, @NotNull String defaultValue) { |
| 281 |
return s == null ? defaultValue : s; |
| 282 |
} |
| 283 |
|
| 284 |
public static void join(@NotNull Collection<String> strings, @NotNull String separator, @NotNull StringBuilder result) { |
| 285 |
boolean isFirst = true; |
| 286 |
for (String string : strings) { |
| 287 |
if (string != null) { |
| 288 |
if (isFirst) { |
| 289 |
isFirst = false; |
| 290 |
} |
| 291 |
else { |
| 292 |
result.append(separator); |
| 293 |
} |
| 294 |
result.append(string); |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
@Nullable |
| 300 |
public static <T> T getFirstItem(@Nullable Collection<T> items) { |
| 301 |
return items == null || items.isEmpty() ? null : items.iterator().next(); |
| 302 |
} |
| 303 |
|
| 304 |
} |