Fix string tokenizer (#2538)

* Fix

* Parse quote and double quote at the same time. Add TokenizerTest.

* Simplify TokenizerTest

* Fix handling multiple space

* Fix handling empty part

* Supports escape sequences

* Remove an unnecessary lambda.

---------

Co-authored-by: Burning_TNT <pangyl08@163.com“>
Co-authored-by: Glavo <zjx001202@gmail.com>
This commit is contained in:
Burning_TNT
2023-12-31 22:45:26 +08:00
committed by GitHub
parent bdcbe6c948
commit 5defff2bb0
2 changed files with 103 additions and 31 deletions

View File

@@ -0,0 +1,40 @@
package org.jackhuang.hmcl.util;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
public class TokenizerTest {
private void test(String source, String... expected) {
Assertions.assertEquals(Arrays.asList(expected), StringUtils.tokenize(source));
}
@Test
public void textTokenizer() {
test(
"\"C:/Program Files/Bellsoft/JDK-11/bin.java.exe\" -version \"a.b.c\" something else",
"C:/Program Files/Bellsoft/JDK-11/bin.java.exe", "-version", "a.b.c", "something", "else"
);
test(
"\"Another\"Text something else",
"AnotherText", "something", "else"
);
test(
"Text without quote",
"Text", "without", "quote"
);
test(
"Text with multiple spaces",
"Text", "with", "multiple", "spaces"
);
test(
"Text with empty part ''",
"Text", "with", "empty", "part", ""
);
test(
"head\"abc\\n\\\\\\\"\"end",
"headabc\n\\\"end"
);
}
}