Skip to content

Commit

Permalink
Fix handling empty part
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Dec 8, 2023
1 parent 1c4ed0d commit 2dff954
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ public static List<String> tokenize(String str) {
// 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);
Expand All @@ -223,18 +224,21 @@ public static List<String> tokenize(String str) {
}
current.append(str, i + 1, end);
i = end + 1;
hasValue = true;
} else if (c == ' ') {
if (current.length() > 0) {
if (hasValue) {
parts.add(current.toString());
current.setLength(0);
hasValue = false;
}
i++;
} else {
current.append(c);
hasValue = true;
i++;
}
}
if (current.length() > 0) {
if (hasValue) {
parts.add(current.toString());
}

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

0 comments on commit 2dff954

Please sign in to comment.