fix: escape quotes in String.prn (#1287)

This commit is contained in:
Veit Heller 2021-07-29 08:30:42 +02:00 committed by GitHub
parent 98acdb53e9
commit 39e8fc6d24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 3 deletions

View File

@ -8,7 +8,7 @@ String String_allocate(int len, char byte) {
* String_alloc(10, "a") == "aaaaaaaaaa"
*/
String ptr = CARP_MALLOC(len + 1);
if( ptr!=NULL) {
if (ptr != NULL) {
// calling memset(NULL,...) would exercise undefined behaviour...
memset(ptr, byte, len);
ptr[len] = '\0';
@ -107,10 +107,28 @@ String String_str(const String *s) {
return String_copy(s);
}
int count_occurrences(String s, char c) {
int res = 0;
while (*s != '\0') {
if (*s == c) res++;
s++;
}
return res;
}
String String_prn(const String *s) {
int n = strlen(*s) + 4;
int n = strlen(*s) + 4 + count_occurrences(*s, '"');
String buffer = CARP_MALLOC(n);
sprintf(buffer, "@\"%s\"", *s);
buffer[0] = '@';
buffer[1] = '"';
String c = *s;
for (int i = 2; i < n - 2; i++) {
if (*c == '"') buffer[i++] = '\\';
buffer[i] = *c;
c++;
}
buffer[n - 2] = '"';
buffer[n - 1] = '\0';
return buffer;
}

View File

@ -331,4 +331,8 @@
"HäLLO WöRLD"
&(ascii-to-upper "HälLo WöRld")
"ascii-to-upper works for valid UTF-8" )
(assert-equal test
"@\"\\\"h\\\"i\\\"\""
&(prn "\"h\"i\"")
"prn works on quoted strings")
)