From 490dec222f7272aeed13b320c30c91f5476997e0 Mon Sep 17 00:00:00 2001 From: zakuro Date: Fri, 6 Aug 2021 12:21:28 +0900 Subject: [PATCH] fmt: keep char literal, `'` (#11060) --- cmd/tools/vrepl.v | 2 +- vlib/builtin/string_test.v | 2 +- vlib/net/html/parser.v | 4 ++-- vlib/strconv/vprintf.v | 2 +- vlib/v/fmt/fmt.v | 6 +++++- vlib/v/fmt/tests/char_literal_backtick_keep.vv | 3 --- vlib/v/fmt/tests/char_literal_keep.vv | 3 +++ vlib/v/gen/c/cgen.v | 2 +- vlib/v/scanner/scanner.v | 4 ++-- vlib/v/util/util.v | 2 +- vlib/v/vmod/parser.v | 2 +- 11 files changed, 18 insertions(+), 14 deletions(-) delete mode 100644 vlib/v/fmt/tests/char_literal_backtick_keep.vv create mode 100644 vlib/v/fmt/tests/char_literal_keep.vv diff --git a/cmd/tools/vrepl.v b/cmd/tools/vrepl.v index 99ea224465..701d453195 100644 --- a/cmd/tools/vrepl.v +++ b/cmd/tools/vrepl.v @@ -44,7 +44,7 @@ fn (mut r Repl) checks() bool { mut in_string := false was_indent := r.indent > 0 for i := 0; i < r.line.len; i++ { - if r.line[i] == `\'` && (i == 0 || r.line[i - 1] != `\\`) { + if r.line[i] == `'` && (i == 0 || r.line[i - 1] != `\\`) { in_string = !in_string } if r.line[i] == `{` && !in_string { diff --git a/vlib/builtin/string_test.v b/vlib/builtin/string_test.v index 8a5ee6659b..5936aff5f0 100644 --- a/vlib/builtin/string_test.v +++ b/vlib/builtin/string_test.v @@ -651,7 +651,7 @@ fn test_for_loop_two() { } fn test_quote() { - a := `\'` + a := `'` println('testing double quotes') b := 'hi' assert b == 'hi' diff --git a/vlib/net/html/parser.v b/vlib/net/html/parser.v index 461c145bc1..5b9bbd16ce 100644 --- a/vlib/net/html/parser.v +++ b/vlib/net/html/parser.v @@ -111,10 +111,10 @@ pub fn (mut parser Parser) split_parse(data string) { parser.init() for chr in data { // returns true if byte is a " or ' - is_quote := chr == `"` || chr == `\'` + is_quote := chr == `"` || chr == `'` string_code := match chr { `"` { 1 } // " - `\'` { 2 } // ' + `'` { 2 } // ' else { 0 } } if parser.lexical_attributes.open_code { // here will verify all needed to know if open_code finishes and string in code diff --git a/vlib/strconv/vprintf.v b/vlib/strconv/vprintf.v index 971477d956..18e37b622f 100644 --- a/vlib/strconv/vprintf.v +++ b/vlib/strconv/vprintf.v @@ -113,7 +113,7 @@ pub fn v_sprintf(str string, pt ...voidptr) string { } i++ continue - } else if ch == `\'` { + } else if ch == `'` { i++ continue } else if ch == `.` && fc_ch1 >= `1` && fc_ch1 <= `9` { diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 830d878226..5941be6d2a 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -521,7 +521,11 @@ pub fn (mut f Fmt) expr(node ast.Expr) { f.chan_init(mut node) } ast.CharLiteral { - f.write('`$node.val`') + if node.val == r"\'" { + f.write("`'`") + } else { + f.write('`$node.val`') + } } ast.Comment { f.comment(node, inline: true) diff --git a/vlib/v/fmt/tests/char_literal_backtick_keep.vv b/vlib/v/fmt/tests/char_literal_backtick_keep.vv deleted file mode 100644 index 6979e95bbc..0000000000 --- a/vlib/v/fmt/tests/char_literal_backtick_keep.vv +++ /dev/null @@ -1,3 +0,0 @@ -fn char_backtick() { - println(`\``) -} diff --git a/vlib/v/fmt/tests/char_literal_keep.vv b/vlib/v/fmt/tests/char_literal_keep.vv new file mode 100644 index 0000000000..e00c5e4d49 --- /dev/null +++ b/vlib/v/fmt/tests/char_literal_keep.vv @@ -0,0 +1,3 @@ +println(`"`) +printrn(`'`) +println(`\``) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 154c14a53b..75d9fd02e4 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5074,7 +5074,7 @@ fn (mut g Gen) const_decl_precomputed(mod string, name string, ct_value ast.Comp rune { rune_code := u32(ct_value) if rune_code <= 255 { - if rune_code in [`"`, `\\`, `\'`] { + if rune_code in [`"`, `\\`, `'`] { return false } escval := util.smart_quote(byte(rune_code).ascii_str(), false) diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index f60ee8173f..5a69281932 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -13,7 +13,7 @@ import v.vet import v.errors const ( - single_quote = `\'` + single_quote = `'` double_quote = `"` // char used as number separator num_sep = `_` @@ -677,7 +677,7 @@ fn (mut s Scanner) text_scan() token.Token { // end of `$expr` // allow `'$a.b'` and `'$a.c()'` if s.is_inter_start && next_char == `\\` - && s.look_ahead(2) !in [`x`, `n`, `r`, `\\`, `t`, `e`, `"`, `\'`] { + && s.look_ahead(2) !in [`x`, `n`, `r`, `\\`, `t`, `e`, `"`, `'`] { s.warn('unknown escape sequence \\${s.look_ahead(2)}') } if s.is_inter_start && next_char == `(` { diff --git a/vlib/v/util/util.v b/vlib/v/util/util.v index 28610378df..5052ef8d25 100644 --- a/vlib/v/util/util.v +++ b/vlib/v/util/util.v @@ -75,7 +75,7 @@ pub fn resolve_env_value(str string, check_for_presence bool) ?string { if ch.is_letter() || ch.is_digit() || ch == `_` { env_lit += ch.ascii_str() } else { - if !(ch == `\'` || ch == `)`) { + if !(ch == `'` || ch == `)`) { if ch == `$` { return error('cannot use string interpolation in compile time \$env() expression') } diff --git a/vlib/v/vmod/parser.v b/vlib/v/vmod/parser.v index 3df5315226..c96afeedc9 100644 --- a/vlib/v/vmod/parser.v +++ b/vlib/v/vmod/parser.v @@ -130,7 +130,7 @@ fn (mut s Scanner) scan_all() { continue } } - if c in [`\'`, `\"`] && !s.peek_char(`\\`) { + if c in [`'`, `\"`] && !s.peek_char(`\\`) { s.pos++ str := s.create_string(c) s.tokenize(.str, str)