Update StringUtils.java
This commit is contained in:
parent
a569f3a953
commit
44afd27a32
|
@ -17,6 +17,8 @@
|
|||
*/
|
||||
package com.tungsten.fclcore.util;
|
||||
|
||||
import com.tungsten.fclcore.util.platform.OperatingSystem;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.*;
|
||||
|
@ -196,21 +198,92 @@ public final class StringUtils {
|
|||
}
|
||||
|
||||
public static List<String> tokenize(String str) {
|
||||
if (str == null)
|
||||
if (isBlank(str)) {
|
||||
return new ArrayList<>();
|
||||
else
|
||||
return tokenize(str, " \t\n\r\f");
|
||||
} else {
|
||||
// Split the string with ' and space cleverly.
|
||||
ArrayList<String> parts = new ArrayList<>();
|
||||
|
||||
boolean hasValue = false;
|
||||
StringBuilder current = new StringBuilder(str.length());
|
||||
for (int i = 0; i < str.length(); ) {
|
||||
char c = str.charAt(i);
|
||||
if (c == '\'') {
|
||||
hasValue = true;
|
||||
int end = str.indexOf(c, i + 1);
|
||||
if (end < 0) {
|
||||
end = str.length();
|
||||
}
|
||||
current.append(str, i + 1, end);
|
||||
i = end + 1;
|
||||
|
||||
} else if (c == '"') {
|
||||
hasValue = true;
|
||||
i++;
|
||||
while (i < str.length()) {
|
||||
c = str.charAt(i++);
|
||||
if (c == '"') {
|
||||
break;
|
||||
} else if (c == '\\' && i < str.length()) {
|
||||
c = str.charAt(i++);
|
||||
switch (c) {
|
||||
case 'n':
|
||||
c = '\n';
|
||||
break;
|
||||
case 'r':
|
||||
c = '\r';
|
||||
break;
|
||||
case 't':
|
||||
c = '\t';
|
||||
break;
|
||||
case 'v':
|
||||
c = '\u000b';
|
||||
break;
|
||||
case 'a':
|
||||
c = '\u0007';
|
||||
break;
|
||||
}
|
||||
current.append(c);
|
||||
} else {
|
||||
current.append(c);
|
||||
}
|
||||
}
|
||||
} else if (c == ' ') {
|
||||
if (hasValue) {
|
||||
parts.add(current.toString());
|
||||
current.setLength(0);
|
||||
hasValue = false;
|
||||
}
|
||||
i++;
|
||||
} else {
|
||||
hasValue = true;
|
||||
current.append(c);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
if (hasValue) {
|
||||
parts.add(current.toString());
|
||||
}
|
||||
|
||||
return parts;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> tokenize(String str, String delim) {
|
||||
ArrayList<String> result = new ArrayList<>();
|
||||
StringTokenizer tokenizer = new StringTokenizer(str, delim);
|
||||
while (tokenizer.hasMoreTokens()) {
|
||||
delim = tokenizer.nextToken();
|
||||
result.add(delim);
|
||||
public static List<String> parseCommand(String command, Map<String, String> env) {
|
||||
StringBuilder stringBuilder = new StringBuilder(command);
|
||||
for (Map.Entry<String, String> entry : env.entrySet()) {
|
||||
String key = "$" + entry.getKey();
|
||||
int i = 0;
|
||||
while (true) {
|
||||
i = stringBuilder.indexOf(key, i);
|
||||
if (i == -1) {
|
||||
break;
|
||||
}
|
||||
stringBuilder.replace(i, i + key.length(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
return tokenize(stringBuilder.toString());
|
||||
}
|
||||
|
||||
public static String parseColorEscapes(String original) {
|
||||
|
@ -274,6 +347,32 @@ public final class StringUtils {
|
|||
return true;
|
||||
}
|
||||
|
||||
public static class DynamicCommonSubsequence {
|
||||
private LongestCommonSubsequence calculator;
|
||||
|
||||
public DynamicCommonSubsequence(int intLengthA, int intLengthB) {
|
||||
if (intLengthA > intLengthB) {
|
||||
calculator = new LongestCommonSubsequence(intLengthA, intLengthB);
|
||||
} else {
|
||||
calculator = new LongestCommonSubsequence(intLengthB, intLengthA);
|
||||
}
|
||||
}
|
||||
|
||||
public int calc(CharSequence a, CharSequence b) {
|
||||
if (a.length() < b.length()) {
|
||||
CharSequence t = a;
|
||||
a = b;
|
||||
b = t;
|
||||
}
|
||||
|
||||
if (calculator.maxLengthA < a.length() || calculator.maxLengthB < b.length()) {
|
||||
calculator = new LongestCommonSubsequence(a.length(), b.length());
|
||||
}
|
||||
|
||||
return calculator.calc(a, b);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Class for computing the longest common subsequence between strings.
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue