From ef539696b9540cc45a4343f0fecdb4bbcb1a8a0e Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 23 May 2020 11:39:29 +0100 Subject: [PATCH] Strip carriage return `\r` from raw string literals --- src/string.cpp | 22 +++++++++++++++++++++- src/tokenizer.cpp | 6 +++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/string.cpp b/src/string.cpp index dfcbe4854..165468965 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -661,10 +661,25 @@ bool unquote_char(String s, u8 quote, Rune *rune, bool *multiple_bytes, String * } +String strip_carriage_return(gbAllocator a, String s) { + isize buf_len = s.len; + u8 *buf = gb_alloc_array(a, u8, buf_len); + isize i = 0; + for (isize j = 0; j < s.len; j++) { + u8 c = s.text[j]; + + if (c != '\r') { + buf[i++] = c; + } + } + return make_string(buf, i); +} + + // 0 == failure // 1 == original memory // 2 == new allocation -i32 unquote_string(gbAllocator a, String *s_, u8 quote=0) { +i32 unquote_string(gbAllocator a, String *s_, u8 quote=0, bool has_carriage_return=false) { String s = *s_; isize n = s.len; if (quote == 0) { @@ -683,6 +698,11 @@ i32 unquote_string(gbAllocator a, String *s_, u8 quote=0) { if (string_contains_char(s, '`')) { return 0; } + + if (has_carriage_return) { + *s_ = strip_carriage_return(a, s); + return 2; + } *s_ = s; return 1; } diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp index b4c1b01a1..c9a58cee6 100644 --- a/src/tokenizer.cpp +++ b/src/tokenizer.cpp @@ -960,6 +960,7 @@ Token tokenizer_get_token(Tokenizer *t) { case '`': // Raw String Literal case '"': // String Literal { + bool has_carriage_return = false; i32 success; Rune quote = curr_rune; token.kind = Token_String; @@ -989,10 +990,13 @@ Token tokenizer_get_token(Tokenizer *t) { if (r == quote) { break; } + if (r == '\r') { + has_carriage_return = true; + } } } token.string.len = t->curr - token.string.text; - success = unquote_string(heap_allocator(), &token.string); + success = unquote_string(heap_allocator(), &token.string, 0, has_carriage_return); if (success > 0) { if (success == 2) { array_add(&t->allocated_strings, token.string);