Skip to content

Commit

Permalink
Fix handling multiple space
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Dec 8, 2023
1 parent bb36049 commit 1c4ed0d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
34 changes: 17 additions & 17 deletions HMCLCore/src/main/java/org/jackhuang/hmcl/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,28 +213,28 @@ public static List<String> tokenize(String str) {
// Split the string with ' and space cleverly.
ArrayList<String> parts = new ArrayList<>();

boolean inside = false;
StringBuilder current = new StringBuilder(str.length());
for (int i = 0; i < str.length(); i++) {
for (int i = 0; i < str.length(); ) {
char c = str.charAt(i);
switch (c) {
case '\'':
case '"': {
inside = !inside;
break;
if (c == '\'' || c == '"') {
int end = str.indexOf(c, i + 1);
if (end < 0) {
end = str.length();
}
case ' ':
if (!inside) {
parts.add(current.toString());
current.setLength(0);
break;
}
// fallthrough
default:
current.append(c);
current.append(str, i + 1, end);
i = end + 1;
} else if (c == ' ') {
if (current.length() > 0) {
parts.add(current.toString());
current.setLength(0);
}
i++;
} else {
current.append(c);
i++;
}
}
if (current.length() != 0) {
if (current.length() > 0) {
parts.add(current.toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@ public void textTokenizer() {
"Text without quote",
"Text", "without", "quote"
);
test(
"Text with multiple spaces",
"Text", "with", "multiple", "spaces"
);
}
}

0 comments on commit 1c4ed0d

Please sign in to comment.