清理代码 (#1990)

* cleanup

* Cleanup StringUtils

* Cleanup CommandBuilder

* Cleanup CommandBuilder

* Use 'String::replace(char, char)'
This commit is contained in:
Glavo
2023-01-14 02:37:55 +08:00
committed by GitHub
parent 753ba956fe
commit 92ce7c83ca
8 changed files with 42 additions and 109 deletions

View File

@@ -63,7 +63,7 @@ public final class Artifact {
String fileName = this.name + "-" + this.version;
if (classifier != null) fileName += "-" + this.classifier;
this.fileName = fileName + "." + this.extension;
this.path = String.format("%s/%s/%s/%s", this.group.replace(".", "/"), this.name, this.version, this.fileName);
this.path = String.format("%s/%s/%s/%s", this.group.replace('.', '/'), this.name, this.version, this.fileName);
// group:name:version:classifier@extension
String descriptor = String.format("%s:%s:%s", group, name, version);
@@ -87,7 +87,7 @@ public final class Artifact {
throw new IllegalArgumentException("Artifact name is malformed");
}
return new Artifact(arr[0].replace("\\", "/"), arr[1], arr[2], arr.length >= 4 ? arr[3] : null, ext);
return new Artifact(arr[0].replace('\\', '/'), arr[1], arr[2], arr.length >= 4 ? arr[3] : null, ext);
}
public String getGroup() {

View File

@@ -20,10 +20,7 @@ package org.jackhuang.hmcl.util;
import org.jackhuang.hmcl.util.platform.OperatingSystem;
import java.io.*;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.regex.Pattern;
/**
*
@@ -158,6 +155,10 @@ public final class StringUtils {
return str + suffix;
}
public static String removePrefix(String str, String prefix) {
return str.startsWith(prefix) ? str.substring(prefix.length()) : str;
}
public static String removePrefix(String str, String... prefixes) {
for (String prefix : prefixes)
if (str.startsWith(prefix))
@@ -165,6 +166,10 @@ public final class StringUtils {
return str;
}
public static String removeSuffix(String str, String suffix) {
return str.endsWith(suffix) ? str.substring(0, str.length() - suffix.length()) : str;
}
/**
* Remove one suffix of the suffixes of the string.
*/
@@ -190,10 +195,12 @@ public final class StringUtils {
return false;
}
public static boolean containsOne(String pattern, char... targets) {
for (char target : targets)
if (pattern.toLowerCase().indexOf(Character.toLowerCase(target)) >= 0)
public static boolean containsChinese(String str) {
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= '\u4e00' && ch <= '\u9fa5')
return true;
}
return false;
}
@@ -259,8 +266,11 @@ public final class StringUtils {
return Optional.of(str.substring(0, halfLength) + " ... " + str.substring(str.length() - halfLength));
}
public static boolean isASCII(CharSequence cs) {
return US_ASCII_ENCODER.canEncode(cs);
public static boolean isASCII(String cs) {
for (int i = 0; i < cs.length(); i++)
if (cs.charAt(i) >= 128)
return false;
return true;
}
public static boolean isAlphabeticOrNumber(String str) {
@@ -307,8 +317,4 @@ public final class StringUtils {
return f[a.length()][b.length()];
}
}
public static final Pattern CHINESE_PATTERN = Pattern.compile("[\\u4e00-\\u9fa5]");
public static final CharsetEncoder US_ASCII_ENCODER = StandardCharsets.US_ASCII.newEncoder();
}

View File

@@ -17,8 +17,6 @@
*/
package org.jackhuang.hmcl.util.platform;
import org.jackhuang.hmcl.util.StringUtils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
@@ -227,8 +225,8 @@ public final class CommandBuilder {
}
private static class Item {
String arg;
boolean parse;
final String arg;
final boolean parse;
Item(String arg, boolean parse) {
this.arg = arg;
@@ -250,7 +248,7 @@ public final class CommandBuilder {
return true;
}
try {
final Process process = Runtime.getRuntime().exec("powershell -Command Get-ExecutionPolicy");
final Process process = Runtime.getRuntime().exec(new String[]{"powershell", "-Command", "Get-ExecutionPolicy"});
if (!process.waitFor(5, TimeUnit.SECONDS)) {
process.destroy();
return false;
@@ -279,28 +277,26 @@ public final class CommandBuilder {
return true;
}
public static String toBatchStringLiteral(String s) {
String escape = " \t\"^&<>|";
if (StringUtils.containsOne(s, escape.toCharArray()))
// The argument has not been quoted, add quotes.
return '"' + s
.replace("\\", "\\\\")
.replace("\"", "\\\"")
+ '"';
else {
return s;
private static boolean containsEscape(String str, String escapeChars) {
for (int i = 0; i < escapeChars.length(); i++) {
if (str.indexOf(escapeChars.charAt(i)) >= 0)
return true;
}
return false;
}
private static String escape(String str, char... escapeChars) {
for (char ch : escapeChars) {
str = str.replace("" + ch, "\\" + ch);
}
return str;
}
public static String toBatchStringLiteral(String s) {
return containsEscape(s, " \t\"^&<>|") ? '"' + escape(s, '\\', '"') : s;
}
public static String toShellStringLiteral(String s) {
String escaping = " \t\"!#$&'()*,;<=>?[\\]^`{|}~";
String escaped = "\"$&`";
if (s.indexOf(' ') >= 0 || s.indexOf('\t') >= 0 || StringUtils.containsOne(s, escaping.toCharArray())) {
// The argument has not been quoted, add quotes.
for (char ch : escaped.toCharArray())
s = s.replace("" + ch, "\\" + ch);
return '"' + s + '"';
} else
return s;
return containsEscape(s, " \t\"!#$&'()*,;<=>?[\\]^`{|}~") ? '"' + escape(s, '"', '$', '&', '`') + '"' : s;
}
}