Skip to content

Commit

Permalink
Supports escape sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Dec 8, 2023
1 parent 2dff954 commit 4585734
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
36 changes: 34 additions & 2 deletions HMCLCore/src/main/java/org/jackhuang/hmcl/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,46 @@ public static List<String> tokenize(String str) {
StringBuilder current = new StringBuilder(str.length());
for (int i = 0; i < str.length(); ) {
char c = str.charAt(i);
if (c == '\'' || c == '"') {
if (c == '\'') {
hasValue = true;
int end = str.indexOf(c, i + 1);
if (end < 0) {
end = str.length();
}
current.append(str, i + 1, end);
i = end + 1;

} else if (c == '"') {
hasValue = true;
i++;
while (i < str.length()) {
c = str.charAt(i++);
if (c == '"') {
break;
} else if (c == '\\' && i < str.length()) {
c = str.charAt(i++);
switch (c) {
case 'n':
c = '\n';
break;
case 'r':
c = '\r';
break;
case 't':
c = '\t';
break;
case 'v':
c = '\u000b';
break;
case 'a':
c = '\u0007';
break;
}
current.append(c);
} else {
current.append(c);
}
}
} else if (c == ' ') {
if (hasValue) {
parts.add(current.toString());
Expand All @@ -233,8 +265,8 @@ public static List<String> tokenize(String str) {
}
i++;
} else {
current.append(c);
hasValue = true;
current.append(c);
i++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ public void textTokenizer() {
"Text", "with", "multiple", "spaces"
);
test(
"Text with empty ''",
"Text", "with", "empty", ""
"Text with empty part ''",
"Text", "with", "empty", "part", ""
);
test(
"head\"abc\\n\\\\\\\"\"end",
"headabc\n\\\"end"
);
}
}

0 comments on commit 4585734

Please sign in to comment.