forked from semuxproject/semux-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tested with quotes and escaped quotes. Fixes semuxproject#13
- Loading branch information
Showing
4 changed files
with
86 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Copyright (c) 2017-2018 The Semux Developers | ||
* | ||
* Distributed under the MIT software license, see the accompanying file | ||
* LICENSE or https://opensource.org/licenses/mit-license.php | ||
*/ | ||
package org.semux.util; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class CommandParser { | ||
|
||
public static List<String> parseInput(String input) { | ||
List<String> commandArguments = new ArrayList<>(); | ||
Pattern ptrn = Pattern.compile("\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\"|\\S+"); | ||
Matcher matcher = ptrn.matcher(input); | ||
while (matcher.find()) { | ||
commandArguments.add(sanitize(matcher.group(0))); | ||
} | ||
return commandArguments; | ||
} | ||
|
||
private static String sanitize(String value) { | ||
if (value.startsWith("\"") && value.endsWith("\"")) { | ||
value = value.substring(1, value.length() - 1); | ||
} | ||
return value.replace("\\\"", "\""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Copyright (c) 2017-2018 The Semux Developers | ||
* | ||
* Distributed under the MIT software license, see the accompanying file | ||
* LICENSE or https://opensource.org/licenses/mit-license.php | ||
*/ | ||
package org.semux.util; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
|
||
public class CommandParserTest { | ||
|
||
@Test | ||
public void testDelimiting() { | ||
|
||
String input = "foo and \"bar stuff\" \"stuff \\\" with quotes \\\" \" more \"\""; | ||
|
||
List<String> parsed = CommandParser.parseInput(input); | ||
|
||
Assert.assertEquals(6, parsed.size()); | ||
Assert.assertEquals("foo", parsed.get(0)); | ||
Assert.assertEquals("and", parsed.get(1)); | ||
Assert.assertEquals("bar stuff", parsed.get(2)); | ||
Assert.assertEquals("stuff \" with quotes \" ", parsed.get(3)); | ||
Assert.assertEquals("more", parsed.get(4)); | ||
Assert.assertEquals("", parsed.get(5)); | ||
} | ||
|
||
@Test | ||
public void testBasic() { | ||
String input = "getBlockByNumber 1"; | ||
List<String> parsed = CommandParser.parseInput(input); | ||
Assert.assertEquals(2, parsed.size()); | ||
Assert.assertEquals("getBlockByNumber", parsed.get(0)); | ||
Assert.assertEquals("1", parsed.get(1)); | ||
|
||
} | ||
} |