Skip to content

Commit effbdc9

Browse files
committed
jimp: add support for some common escape sequences
1 parent 8b04432 commit effbdc9

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

jimp.h

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ typedef struct {
4242
const char *point;
4343

4444
Jimp_Token token;
45-
const char *token_start;
45+
const char *token_start; // TODO: `token_start` is primarily used for diagnostics location. Rename it accordingly.
4646

4747
char *string;
4848
size_t string_count;
@@ -188,12 +188,45 @@ static bool jimp__get_token(Jimp *jimp)
188188
while (jimp->point < jimp->end) {
189189
// TODO: support all the JSON escape sequences defined in the spec
190190
// Yes, including those dumb suroggate pairs. Spec is spec.
191-
if (*jimp->point == '"') {
191+
switch (*jimp->point) {
192+
case '\\': {
193+
jimp->point++;
194+
if (jimp->point >= jimp->end) {
195+
jimp->token_start = jimp->point;
196+
jimp_diagf(jimp, "ERROR: unfinished escape sequence\n");
197+
return false;
198+
}
199+
switch (*jimp->point) {
200+
case 'r':
201+
jimp->point++;
202+
jimp__append_to_string(jimp, '\r');
203+
break;
204+
case 'n':
205+
jimp->point++;
206+
jimp__append_to_string(jimp, '\n');
207+
break;
208+
case 't':
209+
jimp->point++;
210+
jimp__append_to_string(jimp, '\t');
211+
break;
212+
case '\\':
213+
jimp->point++;
214+
jimp__append_to_string(jimp, '\\');
215+
break;
216+
default:
217+
jimp->token_start = jimp->point;
218+
jimp_diagf(jimp, "ERROR: invalid escape sequence\n");
219+
return false;
220+
}
221+
break;
222+
}
223+
case '"': {
192224
jimp->point++;
193225
jimp__append_to_string(jimp, '\0');
194226
jimp->token = JIMP_STRING;
195227
return true;
196-
} else {
228+
}
229+
default:
197230
char x = *jimp->point++;
198231
jimp__append_to_string(jimp, x);
199232
}

0 commit comments

Comments
 (0)