清理代码 (#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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 42 additions and 109 deletions

View File

@ -37,7 +37,7 @@ public abstract class LocalizedRemoteModRepository implements RemoteModRepositor
@Override
public Stream<RemoteMod> search(String gameVersion, Category category, int pageOffset, int pageSize, String searchFilter, SortType sort, SortOrder sortOrder) throws IOException {
String newSearchFilter;
if (StringUtils.CHINESE_PATTERN.matcher(searchFilter).find()) {
if (StringUtils.containsChinese(searchFilter)) {
ModTranslations modTranslations = ModTranslations.getTranslationsByRepositoryType(getType());
List<ModTranslations.Mod> mods = modTranslations.searchMod(searchFilter);
List<String> searchFilters = new ArrayList<>();

View File

@ -144,17 +144,12 @@ public class AboutPage extends StackPane {
openNBT.setSubtitle("Copyright © 2013-2021 Steveice10.\nLicensed under the MIT License.");
openNBT.setExternalLink("https://github.com/Steveice10/OpenNBT");
IconedTwoLineListItem jstun = new IconedTwoLineListItem();
jstun.setTitle("JSTUN");
jstun.setSubtitle("Copyright © 2005 Thomas King <king@t-king.de>.\nLicensed under the Apache License 2.0.");
jstun.setExternalLink("https://github.com/tking/JSTUN");
IconedTwoLineListItem minecraftJFXSkin = new IconedTwoLineListItem();
minecraftJFXSkin.setTitle("minecraft-jfx-skin");
minecraftJFXSkin.setSubtitle("Copyright © 2016 InfinityStudio.\nLicensed under the GPL 3.");
minecraftJFXSkin.setExternalLink("https://github.com/InfinityStudio/minecraft-jfx-skin");
dep.getContent().setAll(javafx, jfoenix, gson, xz, fxgson, constantPoolScanner, openNBT, jstun, minecraftJFXSkin);
dep.getContent().setAll(javafx, jfoenix, gson, xz, fxgson, constantPoolScanner, openNBT, minecraftJFXSkin);
}
ComponentList legal = new ComponentList();

View File

@ -1,57 +0,0 @@
package org.jackhuang.hmcl.util;
import com.google.gson.annotations.SerializedName;
import org.jackhuang.hmcl.task.Task;
import java.util.Collection;
public class JavaRuntimeDownloadTask extends Task<Void> {
@Override
public void execute() {
// HttpRequest.GET("https://hmcl.huangyuhui.net/api/java",
// pair("os", OperatingSystem.CURRENT_OS.getCheckedName()));
// .getJson();
}
@Override
public Collection<? extends Task<?>> getDependencies() {
return super.getDependencies();
}
public static class JavaDownload {
@SerializedName("version")
private final String version;
@SerializedName("distro")
private final String distro;
@SerializedName("url")
private final String url;
public JavaDownload() {
this("", "", "");
}
public JavaDownload(String version, String distro, String url) {
this.version = version;
this.distro = distro;
this.url = url;
}
public String getVersion() {
return version;
}
public String getDistro() {
return distro;
}
public String getUrl() {
return url;
}
}
}

View File

@ -1,3 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.shape.SVGPath?>
<javafx.scene.shape.SVGPath content="M4,11V13H16L10.5,18.5L11.92,19.92L19.84,12L11.92,4.08L10.5,5.5L16,11H4Z" />

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;
}
}

View File

@ -33,10 +33,6 @@ subprojects {
sourceSets = setOf()
}
tasks.withType<Checkstyle> {
exclude("de/javawi/jstun")
}
dependencies {
"testImplementation"("org.junit.jupiter:junit-jupiter:5.9.1")
}