Skip to content

Commit b2aceed

Browse files
Strip UTF-8 BOM from source input and stdin (closes #225)
UTF-8 BOM being unhandled was the cause of the nine failing tests.
1 parent f3b6154 commit b2aceed

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

src/builtins.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7515,6 +7515,11 @@ static Value builtin_input(Interpreter *interp, Value *args, int argc, Expr **ar
75157515
char buf[4096];
75167516
if (fgets(buf, sizeof(buf), stdin) != NULL) {
75177517
size_t len = strlen(buf);
7518+
if (len >= 3 && (unsigned char)buf[0] == 0xEF && (unsigned char)buf[1] == 0xBB &&
7519+
(unsigned char)buf[2] == 0xBF) {
7520+
memmove(buf, buf + 3, len - 2);
7521+
len -= 3;
7522+
}
75187523
if (len > 0 && buf[len - 1] == '\n') {
75197524
buf[len - 1] = '\0';
75207525
}

src/lexer.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ void lexer_init(Lexer *lexer, const char *source, const char *filename) {
8888
lexer->current = 0;
8989
lexer->line = 1;
9090
lexer->column = 1;
91+
if (lexer->source_len >= 3 && (unsigned char)lexer->source[0] == 0xEF && (unsigned char)lexer->source[1] == 0xBB &&
92+
(unsigned char)lexer->source[2] == 0xBF) {
93+
lexer->source += 3;
94+
lexer->source_len -= 3;
95+
}
9196
}
9297

9398
char *lexer_get_line(Lexer *lexer, int line_num) {

0 commit comments

Comments
 (0)