From 63584525f2293729d33d14abcddf5ebd03e96b1c Mon Sep 17 00:00:00 2001 From: Sofia Date: Thu, 16 Apr 2026 20:25:00 +0300 Subject: [PATCH] Add escaping of characters --- src/tokens.cpp | 32 ++++++++++++++++++++++++++++---- test.c | 14 +++++++------- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/tokens.cpp b/src/tokens.cpp index 6cb4eb2..e426c12 100644 --- a/src/tokens.cpp +++ b/src/tokens.cpp @@ -5,7 +5,7 @@ #include #include #include - +#include static bool iswhitespace(char& character) { return character == ' ' @@ -14,6 +14,12 @@ static bool iswhitespace(char& character) { || character == '\r'; } +static std::optional get_escaped(std::string_view inspected) { + if (inspected == "n") + return '\n'; + return {}; +} + namespace token { std::string type_name(Type& type) { switch (type) { @@ -152,9 +158,27 @@ namespace token { std::string content{}; c = text[++i]; // Skip initial " do { - content += c; - if ((i + 1) >= text_length) break; - c = text[++i]; + if (c == '\\') { + std::string escaped_content{}; + if ((i + 1) >= text_length) break; + auto potential = get_escaped(escaped_content + text[++i]); + std::cout << "\"" << escaped_content << "\"" << std::endl; + while (potential.has_value() && (i + 1) < text_length) { + escaped_content += text[i]; + std::cout << "\"" << escaped_content << "\"" << std::endl; + potential = get_escaped(escaped_content + text[++i]); + } + auto escaped = get_escaped(escaped_content); + if (escaped.has_value()) + content += *escaped; + + c = text[i]; + } + else { + content += c; + if ((i + 1) >= text_length) break; + c = text[++i]; + } } while (c != '\"'); i++; // Skip second " tokens.push_back(token::Token{ token::Type::LiteralStr, content, meta + (content.size() + 2) }); diff --git a/test.c b/test.c index 377779e..7e96d41 100644 --- a/test.c +++ b/test.c @@ -13,35 +13,35 @@ void change_first(char otus[5]) { struct Otus; -void update(struct Otus potus) { - potus.field = 20; -} struct Otus { int field; }; +void update(struct Otus potus) { + potus.field = 20; +} void update_ptr(char* ptr) { *ptr = 50; } int main() { - char text[29] = "10th fibonacci number is %d!"; + char text[30] = "10th fibonacci number is %d!\n"; printf(text, fibonacci(10)); char somelist[5] = { 1, 2, 3, 4, 5 }; change_first(somelist); - printf(" first element: %d!", somelist[0]); + printf("first element: %d!\n", somelist[0]); struct Otus otus = { 5 }; update(otus); - printf(" first field: %d!", otus.field); + printf("first field: %d!\n", otus.field); char hello = 10; update_ptr(&hello); - printf(" hello: %d!", hello); + printf("hello: %d!\n", hello); return 0; } \ No newline at end of file