Permalink
Browse files

shellwords experiments

  • Loading branch information...
1 parent 5f57afe commit 3256eda756de4f418bc67e8abb6527f8a9112154 @aslakhellesoy aslakhellesoy committed Sep 3, 2012
@@ -0,0 +1,23 @@
+package cucumber.runtime;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+// http://stackoverflow.com/questions/6030400/regular-expression-troubles-escaped-quotes
+// https://github.com/marayl/doogal/blob/f413e12640ef655256d273aa4e5f6538ed5428f7/src/main/java/org/doogal/core/Shellwords.java
+// http://stackoverflow.com/questions/197233/how-to-parse-a-command-line-with-regular-expressions
+public class Shellwords {
+// private static final Pattern PATTERN = Pattern.compile("(\"[^\"]*\"|[^\"]+)(\\s+|$)");
+ private static final Pattern PATTERN = Pattern.compile("\\s*(?:([^\\s\\\\\\'\\\"]+)|'((?:[^\\'\\\\]|\\\\.)*)'|\"((?:[^\\\"\\\\]|\\\\.)*)\"|(\\\\.?)|(\\S))(\\s|$)?");
+
+ public static String[] split(String string) {
+ Matcher matcher = PATTERN.matcher(string);
+ matcher.find();
+ System.out.println(matcher.matches());
+ System.out.println(matcher.groupCount());
+ for(int i = 0; i < matcher.groupCount(); i++) {
+ System.out.println("matcher = " + matcher.group(i));
+ }
+ return PATTERN.split(string);
+ }
+}
@@ -0,0 +1,35 @@
+package cucumber.runtime;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertArrayEquals;
+
+public class ShellwordsTest {
+ @Test
+ public void splits_normal_words() {
+ String[] split = Shellwords.split("foo bar baz");
+ String[] expected = {"foo", "bar", "baz"};
+ assertArrayEquals(expected, split);
+ }
+
+ @Test
+ public void splits_single_quoted_phrases() {
+ String[] split = Shellwords.split("foo 'bar baz'");
+ String[] expected = {"foo", "bar baz"};
+ assertArrayEquals(expected, split);
+ }
+
+ @Test
+ public void splits_double_quoted_phrases() {
+ String[] split = Shellwords.split("\"foo bar\" baz");
+ String[] expected = {"foo bar", "baz"};
+ assertArrayEquals(expected, split);
+ }
+
+ @Test
+ public void respects_escaped_characters() {
+ String[] split = Shellwords.split("foo\\ bar baz");
+ String[] expected = {"foo bar", "baz"};
+ assertArrayEquals(expected, split);
+ }
+}

0 comments on commit 3256eda

Please sign in to comment.