Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ internal fun shlex(
): List<String> {
val toks = mutableListOf<String>()
var inQuote: Char? = null
var inToken = false // Track if we're building a token (for empty quoted strings)
val sb = StringBuilder()
var i = 0
fun err(msg: String): Nothing {
Expand All @@ -31,14 +32,15 @@ internal fun shlex(
i += 1
} while (i <= text.lastIndex && text[i].isWhitespace())
} else {
inToken = true
sb.append(text[i + 1])
i += 2
}
}

c == inQuote -> {
toks += sb.toString()
sb.clear()
// Don't emit here - just close the quote. Adjacent quoted/unquoted
// strings should concatenate into a single token (POSIX behavior).
inQuote = null
i += 1
}
Expand All @@ -49,19 +51,22 @@ internal fun shlex(
}

c in "\"'" && inQuote == null -> {
inToken = true
inQuote = c
i += 1
}

c.isWhitespace() && inQuote == null -> {
if (sb.isNotEmpty()) {
if (inToken) {
toks += sb.toString()
sb.clear()
inToken = false
}
i += 1
}

else -> {
inToken = true
sb.append(c)
i += 1
}
Expand All @@ -72,7 +77,7 @@ internal fun shlex(
err(localization.unclosedQuote())
}

if (sb.isNotEmpty()) {
if (inToken) {
toks += sb.toString()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,23 @@ class AtFileTest {
C().parse("@file")
}

@[Test JsName("adjacent_quoted_strings_concatenate")]
fun `adjacent quoted strings concatenate`() {
// Verify that adjacent quoted/unquoted strings produce a single token (POSIX behavior)
// e.g., "'"c"'" should produce 'c' (single-quote, c, single-quote)
class C : TestCommand() {
val arg by argument().multiple()

override fun run_() {
arg shouldBe listOf("a", "b", "'c'")
}
}

C().withAtFiles(
"foo" to """a b "'"c"'""""
).parse("@foo")
}

@[Test JsName("parsing_atfile_with_alias")]
fun `parsing atfile with alias`() {
class C : TestCommand() {
Expand Down