Skip to content

Commit

Permalink
Allow common process_escapes to handle \x sequences (ggerganov#3928)
Browse files Browse the repository at this point in the history
* Allow common process_escapes to handle \x sequences

* Fix edge case when second hex digit is NUL
  • Loading branch information
KerfuffleV2 authored Nov 5, 2023
1 parent bb60fd0 commit d9ccce2
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions common/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ void process_escapes(std::string& input) {
case '\'': input[output_idx++] = '\''; break;
case '\"': input[output_idx++] = '\"'; break;
case '\\': input[output_idx++] = '\\'; break;
case 'x':
// Handle \x12, etc
if (input_idx + 2 < input_len) {
const char x[3] = { input[input_idx + 1], input[input_idx + 2], 0 };
char *err_p = nullptr;
const long val = std::strtol(x, &err_p, 16);
if (err_p == x + 2) {
input_idx += 2;
input[output_idx++] = char(val);
break;
}
// Intentionally fall through to default.
}
default: input[output_idx++] = '\\';
input[output_idx++] = input[input_idx]; break;
}
Expand Down

0 comments on commit d9ccce2

Please sign in to comment.