Tauseef Mohiuddin commited on
Commit
3165de3
·
unverified ·
1 Parent(s): 016ac02

main : update escape_double_quotes() function (#776)

Browse files

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: #769

Files changed (1) hide show
  1. examples/main/main.cpp +5 -7
examples/main/main.cpp CHANGED
@@ -375,7 +375,7 @@ bool output_csv(struct whisper_context * ctx, const char * fname) {
375
  return true;
376
  }
377
 
378
- char *escape_double_quotes(const char *str) {
379
  if (str == NULL) {
380
  return NULL;
381
  }
@@ -383,7 +383,7 @@ char *escape_double_quotes(const char *str) {
383
  size_t escaped_length = strlen(str) + 1;
384
 
385
  for (size_t i = 0; str[i] != '\0'; i++) {
386
- if (str[i] == '"') {
387
  escaped_length++;
388
  }
389
  }
@@ -395,12 +395,10 @@ char *escape_double_quotes(const char *str) {
395
 
396
  size_t pos = 0;
397
  for (size_t i = 0; str[i] != '\0'; i++) {
398
- if (str[i] == '"') {
399
  escaped[pos++] = '\\';
400
- escaped[pos++] = '"';
401
- } else {
402
- escaped[pos++] = str[i];
403
  }
 
404
  }
405
 
406
  // 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
451
 
452
  auto value_s = [&](const char *name, const char *val, bool end = false) {
453
  start_value(name);
454
- char * val_escaped = escape_double_quotes(val);
455
  fout << "\"" << val_escaped << (end ? "\"\n" : "\",\n");
456
  free(val_escaped);
457
  };
 
375
  return true;
376
  }
377
 
378
+ char *escape_double_quotes_and_backslashes(const char *str) {
379
  if (str == NULL) {
380
  return NULL;
381
  }
 
383
  size_t escaped_length = strlen(str) + 1;
384
 
385
  for (size_t i = 0; str[i] != '\0'; i++) {
386
+ if (str[i] == '"' || str[i] == '\\') {
387
  escaped_length++;
388
  }
389
  }
 
395
 
396
  size_t pos = 0;
397
  for (size_t i = 0; str[i] != '\0'; i++) {
398
+ if (str[i] == '"' || str[i] == '\\') {
399
  escaped[pos++] = '\\';
 
 
 
400
  }
401
+ escaped[pos++] = str[i];
402
  }
403
 
404
  // no need to set zero due to calloc() being used prior
 
449
 
450
  auto value_s = [&](const char *name, const char *val, bool end = false) {
451
  start_value(name);
452
+ char * val_escaped = escape_double_quotes_and_backslashes(val);
453
  fout << "\"" << val_escaped << (end ? "\"\n" : "\",\n");
454
  free(val_escaped);
455
  };