Skip to content

Commit d85875f

Browse files
committed
Process escape sequences given in prompts
1 parent 13b0c68 commit d85875f

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

examples/common.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@ extern "C" __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int
3030
#define CP_UTF8 65001
3131
#endif
3232

33+
<<<<<<< HEAD
3334
int32_t get_num_physical_cores() {
35+
=======
36+
37+
bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
38+
// determine sensible default number of threads.
39+
// std::thread::hardware_concurrency may not be equal to the number of cores, or may return 0.
40+
>>>>>>> 7f8e899 (Process escape sequences given in prompts)
3441
#ifdef __linux__
3542
std::ifstream cpuinfo("/proc/cpuinfo");
3643
std::string line;
@@ -66,6 +73,33 @@ int32_t get_num_physical_cores() {
6673
return n_threads > 0 ? (n_threads <= 4 ? n_threads : n_threads / 2) : 4;
6774
}
6875

76+
std::string process_escapes(const char* input) {
77+
std::string output;
78+
79+
if (input != nullptr) {
80+
std::size_t input_len = std::strlen(input);
81+
output.reserve(input_len);
82+
83+
for (std::size_t i = 0; i < input_len; ++i) {
84+
if (input[i] == '\\' && i + 1 < input_len) {
85+
switch (input[++i]) {
86+
case 'n': output.push_back('\n'); break;
87+
case 't': output.push_back('\t'); break;
88+
case '\'': output.push_back('\''); break;
89+
case '\"': output.push_back('\"'); break;
90+
case '\\': output.push_back('\\'); break;
91+
default: output.push_back('\\');
92+
output.push_back(input[i]); break;
93+
}
94+
} else {
95+
output.push_back(input[i]);
96+
}
97+
}
98+
}
99+
100+
return output;
101+
}
102+
69103
bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
70104
bool invalid_param = false;
71105
std::string arg;
@@ -91,7 +125,7 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
91125
invalid_param = true;
92126
break;
93127
}
94-
params.prompt = argv[i];
128+
params.prompt = process_escapes(argv[i]);
95129
} else if (arg == "--session") {
96130
if (++i >= argc) {
97131
invalid_param = true;

0 commit comments

Comments
 (0)