Skip to content

Commit 620d43f

Browse files
main : update escape_double_quotes() function (ggml-org#776)
Updated the escape_double_quotes() function such that the function now escapes both double quotes and backslashes in the input string. Changes Made: - Renamed the function to escape_quotes_and_backslashes - Modified the condition in the first loop to increment the value of 'escaped_length' for both double quotes and backslashes. - Modified the condition in second loop to add a backslash before the current character if it is a double quote or a backslash. Resolves: ggml-org#769
1 parent 2bfdd6c commit 620d43f

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

examples/main/main.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -375,15 +375,15 @@ bool output_csv(struct whisper_context * ctx, const char * fname) {
375375
return true;
376376
}
377377

378-
char *escape_double_quotes(const char *str) {
378+
char *escape_double_quotes_and_backslashes(const char *str) {
379379
if (str == NULL) {
380380
return NULL;
381381
}
382382

383383
size_t escaped_length = strlen(str) + 1;
384384

385385
for (size_t i = 0; str[i] != '\0'; i++) {
386-
if (str[i] == '"') {
386+
if (str[i] == '"' || str[i] == '\\') {
387387
escaped_length++;
388388
}
389389
}
@@ -395,12 +395,10 @@ char *escape_double_quotes(const char *str) {
395395

396396
size_t pos = 0;
397397
for (size_t i = 0; str[i] != '\0'; i++) {
398-
if (str[i] == '"') {
398+
if (str[i] == '"' || str[i] == '\\') {
399399
escaped[pos++] = '\\';
400-
escaped[pos++] = '"';
401-
} else {
402-
escaped[pos++] = str[i];
403400
}
401+
escaped[pos++] = str[i];
404402
}
405403

406404
// no need to set zero due to calloc() being used prior
@@ -451,7 +449,7 @@ bool output_json(struct whisper_context * ctx, const char * fname, const whisper
451449

452450
auto value_s = [&](const char *name, const char *val, bool end = false) {
453451
start_value(name);
454-
char * val_escaped = escape_double_quotes(val);
452+
char * val_escaped = escape_double_quotes_and_backslashes(val);
455453
fout << "\"" << val_escaped << (end ? "\"\n" : "\",\n");
456454
free(val_escaped);
457455
};

0 commit comments

Comments
 (0)