I’m stuck on Stage Backslash outside quotes #YT5
My go code:
func sanitiseArgs(argumentString string) []string {
arguments := make([]string, 0)
chars := make([]rune, 0)
squote := false
dquote := false
escape := false
for _, r := range argumentString {
// the character right after the escape char need to be added
if escape {
escape = false // since the escape character isn't a pair we need to make it false explicitly
chars = append(chars, r)
continue
}
switch r {
case '\n':
case ' ':
if squote || dquote {
chars = append(chars, r)
} else {
if len(chars) > 0 {
arguments = append(arguments, string(chars))
chars = chars[:0]
}
}
case '\'':
if !dquote {
squote = !squote
} else {
chars = append(chars, r)
}
case '"':
if !squote {
dquote = !dquote
} else {
chars = append(chars, r)
}
case '\\':
escape = true
default:
chars = append(chars, r)
}
}
if len(chars) > 0 {
arguments = append(arguments, string(chars))
}
return arguments
}
What’s failing:
I tried to run debugger with strings like "/tmp/fox/f\\n92" and I found out that after /tmp/fox/f it runs case: '\\' makes escape=true then in the next iteration the variable r in the for loop gets n rune instead of the second \. I am very confused at this point. What is it that I don’t know about the GO language which is causing this?
TIA
