I dont really understand what the test wanted me to do

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'
}

oh yeah i forgot to add the command run function

func runBin(cmd string, args []string) {
	if _, err := exec.LookPath(cmd); err != nil {
		notExist(cmd)
		return
	}

	extCmd := exec.Command(cmd, args...)
	extCmd.Stderr = os.Stderr
	extCmd.Stdout = os.Stdout

	if err := extCmd.Run(); err != nil {
		fmt.Printf("err: %v\n", err)
	}
}

and the parser get exec’d at main at this block

		line, err := buffer.ReadString('\n')
		if err != nil {
			fmt.Print("error: reading command")
		}

		lineAr := parseQuote(line[:len(line)-1])
		cmdStr := lineAr[0]
		cmd := getBuiltin(cmdStr)
		ln := Line{
			cmd:  cmdStr,
			args: lineAr[1:],
		}
		loop = run(cmd, ln)

1 Like

idk what happened but i solve the code :moai:

It wanted you to execute the command in your path with the literal name ’exe with ”quotes”’

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.