Last active
December 2, 2024 10:50
-
-
Save raymyers/8077031 to your computer and use it in GitHub Desktop.
Simple Java program to tokenize string as a shell would - similar to shlex in Python. Not checked for POSIX compliance yet - feel free to comment with edge cases that could be fixed. This is my original work, free to reuse. Created for Stack Overflow question: https://stackoverflow.com/questions/1082953/shlex-alternative-for-java/20725050:
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
package com.cadrlife; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class ShellSplitter { | |
public List<String> shellSplit(CharSequence string) { | |
List<String> tokens = new ArrayList<String>(); | |
boolean escaping = false; | |
char quoteChar = ' '; | |
boolean quoting = false; | |
int lastCloseQuoteIndex = Integer.MIN_VALUE; | |
StringBuilder current = new StringBuilder(); | |
for (int i = 0; i<string.length(); i++) { | |
char c = string.charAt(i); | |
if (escaping) { | |
current.append(c); | |
escaping = false; | |
} else if (c == '\\' && !(quoting && quoteChar == '\'')) { | |
escaping = true; | |
} else if (quoting && c == quoteChar) { | |
quoting = false; | |
lastCloseQuoteIndex = i; | |
} else if (!quoting && (c == '\'' || c == '"')) { | |
quoting = true; | |
quoteChar = c; | |
} else if (!quoting && Character.isWhitespace(c)) { | |
if (current.length() > 0 || lastCloseQuoteIndex == (i - 1)) { | |
tokens.add(current.toString()); | |
current = new StringBuilder(); | |
} | |
} else { | |
current.append(c); | |
} | |
} | |
if (current.length() > 0 || lastCloseQuoteIndex == (string.length() - 1)) { | |
tokens.add(current.toString()); | |
} | |
return tokens; | |
} | |
} |
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
package com.cadrlife; | |
import org.junit.Test; | |
import java.util.Arrays; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertTrue; | |
public class ShellSplitterTest { | |
ShellSplitter token = new ShellSplitter(); | |
@Test | |
public void blankYieldsEmptyArgs() { | |
assertTrue(token.shellSplit("").isEmpty()); | |
} | |
@Test | |
public void whitespacesOnlyYeildsEmptyArgs() { | |
assertTrue(token.shellSplit(" \t \n").isEmpty()); | |
} | |
@Test | |
public void normalTokens() { | |
assertEquals(Arrays.asList("a", "bee", "cee"), token.shellSplit("a\tbee cee")); | |
} | |
@Test | |
public void doubleQuotes() { | |
assertEquals(Arrays.asList("hello world"), token.shellSplit("\"hello world\"")); | |
} | |
@Test | |
public void singleQuotes() { | |
assertEquals(Arrays.asList("hello world"), token.shellSplit("'hello world'")); | |
} | |
@Test | |
public void escapedDoubleQuotes() { | |
assertEquals(Arrays.asList("\"hello world\""), token.shellSplit("\"\\\"hello world\\\"")); | |
} | |
@Test | |
public void noEscapeWithinSingleQuotes() { | |
assertEquals(Arrays.asList("hello \\\" world"), token.shellSplit("'hello \\\" world'")); | |
} | |
@Test | |
public void backToBackQuotedStringsShouldFormSingleToken() { | |
assertEquals(Arrays.asList("foobarbaz"), token.shellSplit("\"foo\"'bar'baz")); | |
assertEquals(Arrays.asList("three four"), token.shellSplit("\"three\"' 'four")); | |
} | |
@Test | |
public void escapedSpacesDoNotBreakUpTokens() { | |
assertEquals(Arrays.asList("three four"), token.shellSplit("three\\ four")); | |
} | |
@Test | |
public void emptyDoubleQuotesYieldsListOfEmptyString() { | |
assertEquals(Arrays.asList(""), token.shellSplit("\"\"")); | |
} | |
@Test | |
public void emptySingleQuotesYieldsListOfEmptyString() { | |
assertEquals(Arrays.asList(""), token.shellSplit("''")); | |
} | |
@Test | |
public void twoPairsEmptySingleQuotesYieldsListOfTwoEmptyStringWhenSeparated() { | |
assertEquals(Arrays.asList("", ""), token.shellSplit("'' ''")); | |
} | |
@Test | |
public void twoPairsEmptySingleQuotesYieldsListOfOneEmptyStringWhenNotSeparated() { | |
assertEquals(Arrays.asList(""), token.shellSplit("''''")); | |
} | |
} |
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 is free and unencumbered software released into the public domain. | |
Anyone is free to copy, modify, publish, use, compile, sell, or | |
distribute this software, either in source code form or as a compiled | |
binary, for any purpose, commercial or non-commercial, and by any | |
means. | |
In jurisdictions that recognize copyright laws, the author or authors | |
of this software dedicate any and all copyright interest in the | |
software to the public domain. We make this dedication for the benefit | |
of the public at large and to the detriment of our heirs and | |
successors. We intend this dedication to be an overt act of | |
relinquishment in perpetuity of all present and future rights to this | |
software under copyright law. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
OTHER DEALINGS IN THE SOFTWARE. | |
For more information, please refer to <http://unlicense.org/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@raymyers its used to automate the prosses of giving SSL certs in our company. I was only saying this simply because if I'm calling it once or twice it would be easier to edit the pom, than copy and paste the function and the unit tests (our pipeline will fail the build if under a certain amount of line coverage). Also, I noticed I was using an outdated version of the gist having it as a maven jar would give a better chance of me seeing it was out of date. It's not that important if you want to leave it I'm cool.