From 9ea76aee85f27fc7fae1d89cfe2b862e3ef964a4 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Thu, 15 Jun 2023 22:07:16 +0800 Subject: [PATCH] ... --- src/lexer.h | 12 ++++++++++++ tests/04_str.py | 4 +++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/lexer.h b/src/lexer.h index 7613edbc..bef7cc2b 100644 --- a/src/lexer.h +++ b/src/lexer.h @@ -323,6 +323,18 @@ struct Lexer { case 'n': buff.push_back('\n'); break; case 'r': buff.push_back('\r'); break; case 't': buff.push_back('\t'); break; + case 'x': { + char hex[3] = {eatchar(), eatchar(), '\0'}; + size_t parsed; + char code; + try{ + code = (char)Number::stoi(hex, &parsed, 16); + }catch(std::invalid_argument&){ + SyntaxError("invalid hex char"); + } + if (parsed != 2) SyntaxError("invalid hex char"); + buff.push_back(code); + } break; default: SyntaxError("invalid escape char"); } } else { diff --git a/tests/04_str.py b/tests/04_str.py index 9eaf05f1..a2983784 100644 --- a/tests/04_str.py +++ b/tests/04_str.py @@ -106,4 +106,6 @@ a = '123' assert a.rjust(5) == ' 123' assert a.rjust(5, '0') == '00123' assert a.ljust(5) == '123 ' -assert a.ljust(5, '0') == '12300' \ No newline at end of file +assert a.ljust(5, '0') == '12300' + +assert '\x30\x31\x32' == '012' \ No newline at end of file