瀏覽代碼

Shell: Add support for \uhhhhhhhh escapes in strings

This will be replaced with the unicode character whose codepoint is
given by the unsigned 32-bit number 'hhhhhhhh' (hex).
Ali Mohammad Pur 4 年之前
父節點
當前提交
22b244df45
共有 3 個文件被更改,包括 16 次插入2 次删除
  1. 2 1
      Base/usr/share/man/man5/Shell.md
  2. 12 0
      Userland/Shell/Parser.cpp
  3. 2 1
      Userland/Shell/Parser.h

+ 2 - 1
Base/usr/share/man/man5/Shell.md

@@ -477,7 +477,8 @@ string :: '"' dquoted_string_inner '"'
 dquoted_string_inner :: '\' . dquoted_string_inner?       {concat}
                       | variable dquoted_string_inner?    {compose}
                       | . dquoted_string_inner?
-                      | '\' 'x' digit digit dquoted_string_inner?
+                      | '\' 'x' xdigit*2 dquoted_string_inner?
+                      | '\' 'u' xdigit*8 dquoted_string_inner?
                       | '\' [abefrn] dquoted_string_inner?
 
 variable :: variable_ref slice?

+ 12 - 0
Userland/Shell/Parser.cpp

@@ -1315,6 +1315,18 @@ RefPtr<AST::Node> Parser::parse_doublequoted_string_inner()
                 builder.append(to_byte(first_nibble, second_nibble));
                 break;
             }
+            case 'u': {
+                if (m_input.length() <= m_offset + 8)
+                    break;
+                size_t counter = 8;
+                auto chars = consume_while([&](auto) { return counter-- > 0; });
+                if (auto number = AK::StringUtils::convert_to_uint_from_hex(chars); number.has_value())
+                    builder.append(Utf32View { &number.value(), 1 });
+                else
+                    builder.append(chars);
+
+                break;
+            }
             case 'a':
                 builder.append('\a');
                 break;

+ 2 - 1
Userland/Shell/Parser.h

@@ -265,7 +265,8 @@ string :: '"' dquoted_string_inner '"'
 dquoted_string_inner :: '\' . dquoted_string_inner?       {concat}
                       | variable dquoted_string_inner?    {compose}
                       | . dquoted_string_inner?
-                      | '\' 'x' digit digit dquoted_string_inner?
+                      | '\' 'x' xdigit*2 dquoted_string_inner?
+                      | '\' 'u' xdigit*8 dquoted_string_inner?
                       | '\' [abefrn] dquoted_string_inner?
 
 variable :: variable_ref slice?