fmt: improve string quotes (#8075)

pull/8071/head
zakuro 2021-01-13 14:05:27 +09:00 committed by GitHub
parent 3e3d45b2b1
commit 254df0ca62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 36 additions and 24 deletions

View File

@ -474,9 +474,9 @@ pub fn (a []string) str() string {
sb.write('[') sb.write('[')
for i in 0 .. a.len { for i in 0 .. a.len {
val := a[i] val := a[i]
sb.write("\'") sb.write("'")
sb.write(val) sb.write(val)
sb.write("\'") sb.write("'")
if i < a.len - 1 { if i < a.len - 1 {
sb.write(', ') sb.write(', ')
} }

View File

@ -137,7 +137,7 @@ fn test_finalize_returns_error_for_unknown_flags() {
mut fp := flag.new_flag_parser(['--known', '--unknown']) mut fp := flag.new_flag_parser(['--known', '--unknown'])
fp.bool('known', 0, false, '') fp.bool('known', 0, false, '')
finalized := fp.finalize() or { finalized := fp.finalize() or {
assert err == "Unknown argument \'unknown\'" assert err == "Unknown argument 'unknown'"
return return
} }
assert finalized.len < 0 // expect error to be returned assert finalized.len < 0 // expect error to be returned

View File

@ -35,7 +35,7 @@ pub fn cp_all(src string, dst string, overwrite bool) ? {
source_path := real_path(src) source_path := real_path(src)
dest_path := real_path(dst) dest_path := real_path(dst)
if !exists(source_path) { if !exists(source_path) {
return error("Source path doesn\'t exist") return error("Source path doesn't exist")
} }
// single file copy // single file copy
if !is_dir(source_path) { if !is_dir(source_path) {

View File

@ -510,7 +510,7 @@ fn (mut v Builder) cc() {
if v.pref.os == .ios { if v.pref.os == .ios {
ios_sdk := if v.pref.is_ios_simulator { 'iphonesimulator' } else { 'iphoneos' } ios_sdk := if v.pref.is_ios_simulator { 'iphonesimulator' } else { 'iphoneos' }
ios_sdk_path_res := os.exec('xcrun --sdk $ios_sdk --show-sdk-path') or { ios_sdk_path_res := os.exec('xcrun --sdk $ios_sdk --show-sdk-path') or {
panic("Couldn\'t find iphonesimulator") panic("Couldn't find iphonesimulator")
} }
mut isysroot := ios_sdk_path_res.output.replace('\n', '') mut isysroot := ios_sdk_path_res.output.replace('\n', '')
ccompiler = 'xcrun --sdk iphoneos clang -isysroot $isysroot' ccompiler = 'xcrun --sdk iphoneos clang -isysroot $isysroot'

View File

@ -10,6 +10,7 @@ import strings
import v.util import v.util
const ( const (
bs = '\\'
tabs = ['', '\t', '\t\t', '\t\t\t', '\t\t\t\t', '\t\t\t\t\t', '\t\t\t\t\t\t', '\t\t\t\t\t\t\t', tabs = ['', '\t', '\t\t', '\t\t\t', '\t\t\t\t', '\t\t\t\t\t', '\t\t\t\t\t\t', '\t\t\t\t\t\t\t',
'\t\t\t\t\t\t\t\t', '\t\t\t\t\t\t\t\t',
] ]
@ -1185,15 +1186,28 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
f.write('}') f.write('}')
} }
ast.StringLiteral { ast.StringLiteral {
use_double_quote := node.val.contains("'") && !node.val.contains('"')
if node.is_raw { if node.is_raw {
f.write('r') f.write('r')
} else if node.language == table.Language.c { } else if node.language == table.Language.c {
f.write('c') f.write('c')
} }
if node.val.contains("'") && !node.val.contains('"') { if node.is_raw {
f.write('"$node.val"') if use_double_quote {
f.write('"$node.val"')
} else {
f.write("'$node.val'")
}
} else { } else {
f.write("'$node.val'") unescaped_val := node.val.replace('$bs$bs', '\x01').replace_each(["$bs'", "'",
'$bs"', '"'])
if use_double_quote {
s := unescaped_val.replace_each(['\x01', '$bs$bs', '"', '$bs"'])
f.write('"$s"')
} else {
s := unescaped_val.replace_each(['\x01', '$bs$bs', "'", "$bs'"])
f.write("'$s'")
}
} }
} }
ast.StringInterLiteral { ast.StringInterLiteral {

View File

@ -3,9 +3,8 @@ fn main() {
println('This is correct !') println('This is correct !')
println("It's okay") println("It's okay")
println('This is "too"') println('This is "too"')
// TODO println("I'm correctly formatted")
// 'I\'m not correctly formatted' => "I'm not correctly formatted" println('"Everything on the internet is true" - Albert Einstein, 1965')
// "\"Everything on the internet is true\" - Albert Einstein, 1965" => '"Everything on the internet is true" - Albert Einstein, 1965'
println('I\'m out of idea "_"') println('I\'m out of idea "_"')
// "Definitely out \":'(\"" => 'Definitely out ":\'("' println('Definitely out ":\'("')
} }

View File

@ -3,9 +3,8 @@ fn main() {
println('This is correct !') println('This is correct !')
println("It's okay") println("It's okay")
println('This is "too"') println('This is "too"')
// TODO println('I\'m correctly formatted')
// 'I\'m not correctly formatted' => "I'm not correctly formatted" println("\"Everything on the internet is true\" - Albert Einstein, 1965")
// "\"Everything on the internet is true\" - Albert Einstein, 1965" => '"Everything on the internet is true" - Albert Einstein, 1965'
println('I\'m out of idea "_"') println('I\'m out of idea "_"')
// "Definitely out \":'(\"" => 'Definitely out ":\'("' println("Definitely out \":'(\"")
} }

View File

@ -44,7 +44,7 @@ fn (g &Gen) type_to_fmt(typ table.Type) string {
[.struct_, .array, .array_fixed, .map, .bool, .enum_, .interface_, .sum_type, .function] { [.struct_, .array, .array_fixed, .map, .bool, .enum_, .interface_, .sum_type, .function] {
return '%.*s\\000' return '%.*s\\000'
} else if sym.kind == .string { } else if sym.kind == .string {
return "\'%.*s\\000\'" return "'%.*s\\000'"
} else if sym.kind in [.f32, .f64] { } else if sym.kind in [.f32, .f64] {
return '%g\\000' // g removes trailing zeros unlike %f return '%g\\000' // g removes trailing zeros unlike %f
} else if sym.kind == .u64 { } else if sym.kind == .u64 {

View File

@ -1438,7 +1438,7 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw table.Type, expected_t
// cestring returns a V string, properly escaped for embeddeding in a C string literal. // cestring returns a V string, properly escaped for embeddeding in a C string literal.
fn cestring(s string) string { fn cestring(s string) string {
return s.replace('\\', '\\\\').replace('"', "\'") return s.replace('\\', '\\\\').replace('"', "'")
} }
// ctoslit returns a '_SLIT("$s")' call, where s is properly escaped. // ctoslit returns a '_SLIT("$s")' call, where s is properly escaped.

View File

@ -1154,7 +1154,7 @@ fn (mut s Scanner) ident_char() string {
} }
} }
// Escapes a `'` character // Escapes a `'` character
return if c == "\'" { return if c == "'" {
'\\' + c '\\' + c
} else { } else {
c c

View File

@ -7,8 +7,8 @@ import os
import strings import strings
const ( const (
str_start = "sb.write(\'" str_start = "sb.write('"
str_end = "\' ) " str_end = "' ) "
) )
// compile_file compiles the content of a file by the given path as a template // compile_file compiles the content of a file by the given path as a template
@ -33,14 +33,14 @@ pub fn compile_template(html_ string, fn_name string) string {
h := os.read_file('templates/header.html') or { h := os.read_file('templates/header.html') or {
panic('reading file templates/header.html failed') panic('reading file templates/header.html failed')
} }
header = h.trim_space().replace("\'", '"') header = h.trim_space().replace("'", '"')
html = header + html html = header + html
} }
if os.exists('templates/footer.html') && html.contains('@footer') { if os.exists('templates/footer.html') && html.contains('@footer') {
f := os.read_file('templates/footer.html') or { f := os.read_file('templates/footer.html') or {
panic('reading file templates/footer.html failed') panic('reading file templates/footer.html failed')
} }
footer = f.trim_space().replace("\'", '"') footer = f.trim_space().replace("'", '"')
html += footer html += footer
} }
mut lines := html.split_into_lines() mut lines := html.split_into_lines()
@ -81,7 +81,7 @@ _ = footer
mut file_content := os.read_file(file_path) or { mut file_content := os.read_file(file_path) or {
panic('reading file $file_name failed') panic('reading file $file_name failed')
} }
file_content = file_content.replace("\'", '"') file_content = file_content.replace("'", '"')
lines2 := file_content.split_into_lines() lines2 := file_content.split_into_lines()
for l in lines2 { for l in lines2 {
lines.insert(i + 1, l) lines.insert(i + 1, l)