Skip to content

Commit

Permalink
chore: 性能优化
Browse files Browse the repository at this point in the history
减少约40%用时
  • Loading branch information
mcchampions committed Feb 8, 2025
1 parent 8e7cfdc commit 28dd164
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static void onAsyncTabCompleteEvent(AsyncTabCompleteEvent e) {
return;
}
}
List<String> args = new QuotedStringTokenizer(buffer.substring(firstPlace + 1)).tokenize(false);
List<String> args = new QuotedStringTokenizer(buffer.substring(firstPlace + 1)).tokenize();
List<String> suggests = SlimefunTabCompleter.onTabComplete(args);
e.setCompletions(suggests);
e.setHandled(true);
Expand Down
84 changes: 27 additions & 57 deletions src/main/java/me/qscbm/slimefun4/utils/QuotedStringTokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,88 +28,58 @@
import java.util.List;

public class QuotedStringTokenizer {
private final String string;
private final char[] chars;
private final int length;
private int cursor;

public QuotedStringTokenizer(String string) {
this.string = string;
this.chars = string.toCharArray();
this.length = chars.length;
}

public List<String> tokenize(boolean omitEmptyStringAtEnd) {
List<String> output = new ArrayList<>();
while (hasNext()) {
public List<String> tokenize() {
final List<String> output = new ArrayList<>(5);
while (cursor < length) {
output.add(readString());
}
if (!omitEmptyStringAtEnd && this.cursor > 0 && isWhitespace(peek(-1))) {
output.add("");
}
return output;
}

private static boolean isQuoteCharacter(char c) {
// return c == '"' || c == '“' || c == '”';
return c == '\u0022' || c == '\u201C' || c == '\u201D';
}

private static boolean isWhitespace(char c) {
return c == ' ';
}

private String readString() {
if (isQuoteCharacter(peek())) {
return readQuotedString();
} else {
return readUnquotedString();
}
final char c = chars[cursor];
return (c == '"') ? readQuotedString() : readUnquotedString();
}

private String readUnquotedString() {
final int start = this.cursor;
while (hasNext() && !isWhitespace(peek())) {
skip();
final int start = cursor;
while (cursor < length && chars[cursor] != ' ') {
cursor++;
}
final int end = this.cursor;
final int end = cursor;

if (hasNext()) {
skip(); // skip whitespace
if (cursor < length) {
cursor++;
}

return this.string.substring(start, end);
return new String(chars, start, end - start);
}

private String readQuotedString() {
skip(); // skip start quote
cursor++;

final int start = this.cursor;
while (hasNext() && !isQuoteCharacter(peek())) {
skip();
final int start = cursor;
while (cursor < length && chars[cursor] != '"') {
cursor++;
}
final int end = this.cursor;
final int end = cursor;

if (hasNext()) {
skip(); // skip end quote
if (cursor < length) {
cursor++;
if (cursor < length && chars[cursor] == ' ') {
cursor++;
}
}
if (hasNext() && isWhitespace(peek())) {
skip(); // skip whitespace
}

return this.string.substring(start, end);
}

private boolean hasNext() {
return this.cursor + 1 <= this.string.length();
}

private char peek() {
return this.string.charAt(this.cursor);
}

private char peek(int offset) {
return this.string.charAt(this.cursor + offset);
}

private void skip() {
this.cursor++;
return new String(chars, start, end - start);
}

}

0 comments on commit 28dd164

Please sign in to comment.