I’m stuck on Stage #NI6.
I have a “handleSingleQuotes” function takes the value of the ‘command’ variable which holds the user input. It appears to parse correctly according to the challenge’s instructions and echo test passes.
(I’m on macOS Tahoe 26.4.1 incase that helps.)
Here are my logs (all previous stages passed prior to this):
[tester::#NI6] Running tests for Stage #NI6 (Quoting - Single quotes)
[tester::#NI6] [setup] export PATH=/tmp/apple/raspberry/apple:$PATH
[tester::#NI6] Running ./your_program.sh
[tester::#NI6] [setup] echo -n "blueberry pear." > "/tmp/rat/f 50"
[tester::#NI6] [setup] echo -n "grape banana." > "/tmp/rat/f 90"
[tester::#NI6] [setup] echo "blueberry mango." > "/tmp/rat/f 9"
[your-program] $ echo 'test world'
[your-program] test world
[tester::#NI6] ✓ Received expected response
[your-program] $ echo world shell
[your-program] world shell
[tester::#NI6] ✓ Received expected response
[your-program] $ echo 'script example' 'shell''test' world''hello
[your-program] script example shelltest worldhello
[tester::#NI6] ✓ Received expected response
[your-program] $ cat '/tmp/rat/f 50' '/tmp/rat/f 90' '/tmp/rat/f 9'
[your-program] cat: /tmp/rat/f: No such file or directory
[tester::#NI6] ^ Line does not match expected value.
[tester::#NI6] Expected: "blueberry pear.grape banana.blueberry mango."
[tester::#NI6] Received: "cat: /tmp/rat/f: No such file or directory"
[your-program] cat: 50: No such file or directory
[your-program] cat: /tmp/rat/f: No such file or directory
[your-program] cat: 90: No such file or directory
[your-program] cat: /tmp/rat/f: No such file or directory
[your-program] cat: 9: No such file or directory
[your-program] $
[tester::#NI6] Test failed
And here are the snippets of my code:
handleSingleQuote (can’t pass by reference for some reason):
std::string handleSingleQuote(std::string command) {
if (command.find(39) != std::string::npos) {
std::erase(command, '\'');
} else {
const auto new_end = std::ranges::unique(command, [](const char a, const char b) {
return a == ' ' && b == ' ';
}).begin();
command.erase(new_end, command.end());
}
return command;
}
echo implementation:
if (command.rfind("echo ", 0) == 0) {
command.erase(0, 5);
command = handleSingleQuote(command);
std::cout << command << std::endl;
std::cout << std::unitbuf;
}
executable implementation:
else if (isExecutable(program_name)) {
command = handleSingleQuote(command);
//std::cout << command << std::endl;
//std::cout << std::unitbuf;
pid_t pid = fork();
if (pid == 0) {
std::vector<char *> exec_argv;
std::istringstream iss1(command);
std::string arg;
while (iss1 >> arg) {
exec_argv.push_back(strdup(arg.c_str()));
}
exec_argv.push_back(nullptr);
execvp(exec_argv[0], exec_argv.data());
std::cerr << command << ": command not found" << std::endl;
std::cerr << std::unitbuf;
exit(1);
}
// Parent process: wait for child
waitpid(pid, nullptr, 0);
}
Here’s the link to the repo: https://git.codecrafters.io/4cb343607dfd3165