Skip to content

Commit 2485d7a

Browse files
Process escape sequences given in prompts (#1173)
1 parent 13b0c68 commit 2485d7a

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

examples/common.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,33 @@ int32_t get_num_physical_cores() {
6666
return n_threads > 0 ? (n_threads <= 4 ? n_threads : n_threads / 2) : 4;
6767
}
6868

69+
std::string process_escapes(const char* input) {
70+
std::string output;
71+
72+
if (input != nullptr) {
73+
std::size_t input_len = std::strlen(input);
74+
output.reserve(input_len);
75+
76+
for (std::size_t i = 0; i < input_len; ++i) {
77+
if (input[i] == '\\' && i + 1 < input_len) {
78+
switch (input[++i]) {
79+
case 'n': output.push_back('\n'); break;
80+
case 't': output.push_back('\t'); break;
81+
case '\'': output.push_back('\''); break;
82+
case '\"': output.push_back('\"'); break;
83+
case '\\': output.push_back('\\'); break;
84+
default: output.push_back('\\');
85+
output.push_back(input[i]); break;
86+
}
87+
} else {
88+
output.push_back(input[i]);
89+
}
90+
}
91+
}
92+
93+
return output;
94+
}
95+
6996
bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
7097
bool invalid_param = false;
7198
std::string arg;
@@ -91,7 +118,7 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
91118
invalid_param = true;
92119
break;
93120
}
94-
params.prompt = argv[i];
121+
params.prompt = process_escapes(argv[i]);
95122
} else if (arg == "--session") {
96123
if (++i >= argc) {
97124
invalid_param = true;

0 commit comments

Comments
 (0)