From 89b99ad4c35182572b3192fb448c3c22029d2217 Mon Sep 17 00:00:00 2001 From: Delyan Angelov <delian66@gmail.com> Date: Wed, 16 Feb 2022 23:41:05 +0200 Subject: [PATCH] scanner: remove the restriction for \x00 in strings (#13493) --- vlib/builtin/string_test.v | 4 ++++ vlib/v/checker/tests/string_char_null_err.out | 5 ----- vlib/v/checker/tests/string_char_null_err.vv | 3 --- vlib/v/scanner/scanner.v | 15 --------------- 4 files changed, 4 insertions(+), 23 deletions(-) delete mode 100644 vlib/v/checker/tests/string_char_null_err.out delete mode 100644 vlib/v/checker/tests/string_char_null_err.vv diff --git a/vlib/builtin/string_test.v b/vlib/builtin/string_test.v index 6c7283838f..5ae69866e5 100644 --- a/vlib/builtin/string_test.v +++ b/vlib/builtin/string_test.v @@ -980,3 +980,7 @@ fn test_string_f32() { assert '-123'.f32() - (-123) < f32_epsilon assert '-123.456'.f32() - (-123.456) <= f32_epsilon } + +fn test_string_with_zero_byte_escape() { + assert '\x00'.bytes() == [byte(0)] +} diff --git a/vlib/v/checker/tests/string_char_null_err.out b/vlib/v/checker/tests/string_char_null_err.out deleted file mode 100644 index 60e1f7af1a..0000000000 --- a/vlib/v/checker/tests/string_char_null_err.out +++ /dev/null @@ -1,5 +0,0 @@ -vlib/v/checker/tests/string_char_null_err.vv:2:31: error: cannot use `\0` (NULL character) in the string literal - 1 | fn main() { - 2 | println('Null character: \0') - | ^ - 3 | } diff --git a/vlib/v/checker/tests/string_char_null_err.vv b/vlib/v/checker/tests/string_char_null_err.vv deleted file mode 100644 index d60854f829..0000000000 --- a/vlib/v/checker/tests/string_char_null_err.vv +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println('Null character: \0') -} diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index 13cb3a8eba..9b117613dc 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -1204,21 +1204,6 @@ fn (mut s Scanner) ident_string() string { if c == scanner.b_lf { s.inc_line_number() } - // Don't allow \0 - if c == `0` && s.pos > 2 && prevc == scanner.backslash { - if (s.pos < s.text.len - 1 && s.text[s.pos + 1].is_digit()) - || s.count_symbol_before(s.pos - 1, scanner.backslash) % 2 == 0 { - } else if !is_cstr && !is_raw { - s.error(r'cannot use `\0` (NULL character) in the string literal') - } - } - // Don't allow \x00 - if c == `0` && s.pos > 5 && s.expect('\\x0', s.pos - 3) { - if s.count_symbol_before(s.pos - 3, scanner.backslash) % 2 == 0 { - } else if !is_cstr && !is_raw { - s.error(r'cannot use `\x00` (NULL character) in the string literal') - } - } // Escape `\x` `\u` if backslash_count % 2 == 1 && !is_raw && !is_cstr { // Escape `\x`