mirror of
https://github.com/ggerganov/whisper.cpp.git
synced 2025-01-18 02:39:47 +00:00
main : update escape_double_quotes() function (#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: #769
This commit is contained in:
parent
c23588cc4b
commit
eecf2c3d41
@ -375,7 +375,7 @@ bool output_csv(struct whisper_context * ctx, const char * fname) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *escape_double_quotes(const char *str) {
|
char *escape_double_quotes_and_backslashes(const char *str) {
|
||||||
if (str == NULL) {
|
if (str == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -383,7 +383,7 @@ char *escape_double_quotes(const char *str) {
|
|||||||
size_t escaped_length = strlen(str) + 1;
|
size_t escaped_length = strlen(str) + 1;
|
||||||
|
|
||||||
for (size_t i = 0; str[i] != '\0'; i++) {
|
for (size_t i = 0; str[i] != '\0'; i++) {
|
||||||
if (str[i] == '"') {
|
if (str[i] == '"' || str[i] == '\\') {
|
||||||
escaped_length++;
|
escaped_length++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -395,12 +395,10 @@ char *escape_double_quotes(const char *str) {
|
|||||||
|
|
||||||
size_t pos = 0;
|
size_t pos = 0;
|
||||||
for (size_t i = 0; str[i] != '\0'; i++) {
|
for (size_t i = 0; str[i] != '\0'; i++) {
|
||||||
if (str[i] == '"') {
|
if (str[i] == '"' || str[i] == '\\') {
|
||||||
escaped[pos++] = '\\';
|
escaped[pos++] = '\\';
|
||||||
escaped[pos++] = '"';
|
|
||||||
} else {
|
|
||||||
escaped[pos++] = str[i];
|
|
||||||
}
|
}
|
||||||
|
escaped[pos++] = str[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
// no need to set zero due to calloc() being used prior
|
// 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
|
|||||||
|
|
||||||
auto value_s = [&](const char *name, const char *val, bool end = false) {
|
auto value_s = [&](const char *name, const char *val, bool end = false) {
|
||||||
start_value(name);
|
start_value(name);
|
||||||
char * val_escaped = escape_double_quotes(val);
|
char * val_escaped = escape_double_quotes_and_backslashes(val);
|
||||||
fout << "\"" << val_escaped << (end ? "\"\n" : "\",\n");
|
fout << "\"" << val_escaped << (end ? "\"\n" : "\",\n");
|
||||||
free(val_escaped);
|
free(val_escaped);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user