I’m stuck on Stage #QJ0, specifically on test 2
I’ve tried .. any quotes combination
Here are my logs:
[compile] Moved ./.codecrafters/run.sh → ./your_program.sh
[compile] Compilation successful.
[tester::#QJ0] Running tests for Stage #QJ0 (Quoting - Executing a quoted executable)
[tester::#QJ0] [setup] export PATH=/tmp/cow:$PATH
[tester::#QJ0] [setup] Available executables:
[tester::#QJ0] [setup] - 'exe with space'
[tester::#QJ0] [setup] - 'exe with "quotes"'
[tester::#QJ0] [setup] - "exe with \'single quotes\'"
[tester::#QJ0] [setup] - 'exe with \n newline'
[tester::#QJ0] Running ./your_program.sh
[tester::#QJ0] [setup] echo "pear strawberry." > "/tmp/cow/f1"
[tester::#QJ0] [setup] echo "orange pear." > "/tmp/cow/f2"
[tester::#QJ0] [setup] echo "blueberry apple." > "/tmp/cow/f3"
[tester::#QJ0] [setup] echo "blueberry mango." > "/tmp/cow/f4"
[your-program] $ 'exe with space' /tmp/cow/f1
[your-program] pear strawberry.
[tester::#QJ0] ✓ Received expected response
[your-program] $ 'exe with "quotes"' /tmp/cow/f2
[your-program] exe with "quotes" /tmp/cow/f2: command not found
[tester::#QJ0] ^ Line does not match expected value.
[tester::#QJ0] Expected: "orange pear."
[tester::#QJ0] Received: "exe with "quotes" /tmp/cow/f2: command not found"
[your-program] $
[tester::#QJ0] Assertion failed.
[tester::#QJ0] Test failed
And here’s a snippet of my code:
func parseQuote(str string) (arr []string) {
var res []byte
var (
quote = false
dquote = false
backlash = false
)
for i := range str {
if backlash {
res = append(res, str[i])
backlash = false
continue
}
if str[i] == '\\' {
if !(len(str) > i+1) {
res = append(res, str[i])
continue
}
next := str[i+1]
if dquote {
if isSpecialChar(next) {
backlash = true
continue
}
res = append(res, str[i])
continue
}
if quote {
res = append(res, str[i])
}
backlash = true
continue
}
if str[i] == '"' {
if quote {
res = append(res, str[i])
}
if dquote {
dquote = false
quote = false
continue
}
dquote = true
continue
}
if str[i] == '\'' {
if dquote {
res = append(res, str[i])
}
if quote {
quote = false
continue
}
quote = true
continue
}
if quote {
res = append(res, str[i])
continue
}
if dquote {
res = append(res, str[i])
continue
}
if str[i] == ' ' {
if len(res) > 0 {
arr = append(arr, string(res))
res = nil
}
continue
}
res = append(res, str[i])
}
// edge cases for if the end of the line
// is straight newline
if len(res) > 0 {
arr = append(arr, string(res))
}
return arr
}
func isSpecialChar(b byte) bool {
return b == '"' ||
b == '\\' ||
b == '$' ||
b == '`' ||
b == 'n'
}