From 00399b49ab7a8a572d690b2db0c3deebd8108b08 Mon Sep 17 00:00:00 2001 From: zakuro Date: Tue, 9 Mar 2021 20:03:25 +0900 Subject: [PATCH] v.util: add util.tabs() (#9211) --- vlib/v/fmt/fmt.v | 12 +----------- vlib/v/gen/c/cgen.v | 27 ++++++--------------------- vlib/v/gen/c/comptime.v | 2 +- vlib/v/gen/c/fn.v | 2 +- vlib/v/gen/c/index.v | 5 +++-- vlib/v/gen/js/js.v | 4 +--- vlib/v/util/util.v | 19 +++++++++++++++++++ 7 files changed, 32 insertions(+), 39 deletions(-) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 25b5c19317..309979af4e 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -11,9 +11,6 @@ import v.pref 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', - '\t\t\t\t\t\t\t\t', - ] // when to break a line dependant on penalty max_len = [0, 35, 60, 85, 93, 100] ) @@ -110,14 +107,7 @@ pub fn (mut f Fmt) writeln(s string) { } fn (mut f Fmt) write_indent() { - if f.indent < fmt.tabs.len { - f.out.write_string(fmt.tabs[f.indent]) - } else { - // too many indents, do it the slow way: - for _ in 0 .. f.indent { - f.out.write_string('\t') - } - } + f.out.write_string(util.tabs(f.indent)) f.line_len += f.indent * 4 } diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index df70878adb..7239001298 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -24,9 +24,6 @@ const ( cmp_str = ['eq', 'ne', 'gt', 'lt', 'ge', 'le'] // when operands are switched cmp_rev = ['eq', 'ne', 'lt', 'gt', 'le', 'ge'] - 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', - ] ) struct Gen { @@ -844,13 +841,7 @@ pub fn (mut g Gen) write(s string) { eprintln('gen file: ${g.file.path:-30} | last_fn_c_name: ${g.last_fn_c_name:-45} | write: $s') } if g.indent > 0 && g.empty_line { - if g.indent < c.tabs.len { - g.out.write_string(c.tabs[g.indent]) - } else { - for _ in 0 .. g.indent { - g.out.write_string('\t') - } - } + g.out.write_string(util.tabs(g.indent)) } g.out.write_string(s) g.empty_line = false @@ -861,13 +852,7 @@ pub fn (mut g Gen) writeln(s string) { eprintln('gen file: ${g.file.path:-30} | last_fn_c_name: ${g.last_fn_c_name:-45} | writeln: $s') } if g.indent > 0 && g.empty_line { - if g.indent < c.tabs.len { - g.out.write_string(c.tabs[g.indent]) - } else { - for _ in 0 .. g.indent { - g.out.write_string('\t') - } - } + g.out.write_string(util.tabs(g.indent)) } g.out.writeln(s) g.empty_line = true @@ -2088,7 +2073,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { } if right_sym.kind == .function && is_decl { if is_inside_ternary && is_decl { - g.out.write_string(c.tabs[g.indent - g.inside_ternary]) + g.out.write_string(util.tabs(g.indent - g.inside_ternary)) } func := right_sym.info as table.FnType ret_styp := g.typ(func.func.return_type) @@ -2100,7 +2085,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { } else { if is_decl { if is_inside_ternary { - g.out.write_string(c.tabs[g.indent - g.inside_ternary]) + g.out.write_string(util.tabs(g.indent - g.inside_ternary)) } g.write('$styp ') } @@ -2116,7 +2101,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { } if is_inside_ternary && is_decl { g.write(';\n$cur_line') - g.out.write_string(c.tabs[g.indent]) + g.out.write_string(util.tabs(g.indent)) g.expr(left) } g.is_assign_lhs = false @@ -2811,7 +2796,7 @@ fn (mut g Gen) expr(node ast.Expr) { is_gen_or_and_assign_rhs := gen_or && !g.discard_or_result cur_line := if is_gen_or_and_assign_rhs { line := g.go_before_stmt(0) - g.out.write_string(c.tabs[g.indent]) + g.out.write_string(util.tabs(g.indent)) line } else { '' diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 90e3be7df0..a7713a557a 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -154,7 +154,7 @@ fn (mut g Gen) comp_at(node ast.AtExpr) { fn (mut g Gen) comp_if(node ast.IfExpr) { line := if node.is_expr { stmt_str := g.go_before_stmt(0) - g.write(tabs[g.indent]) + g.write(util.tabs(g.indent)) stmt_str.trim_space() } else { '' diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 8241c159da..852feb95ad 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -415,7 +415,7 @@ fn (mut g Gen) call_expr(node ast.CallExpr) { // `x := foo() or { ...}` // cut everything that has been generated to prepend optional variable creation line := g.go_before_stmt(0) - g.out.write_string(tabs[g.indent]) + g.out.write_string(util.tabs(g.indent)) // g.write('/*is_gen_or_and_assign_rhs*/') line } else { diff --git a/vlib/v/gen/c/index.v b/vlib/v/gen/c/index.v index d0bb489d1e..a893fd4fdb 100644 --- a/vlib/v/gen/c/index.v +++ b/vlib/v/gen/c/index.v @@ -5,6 +5,7 @@ module c import v.ast import v.table +import v.util fn (mut g Gen) index_expr(node ast.IndexExpr) { if node.index is ast.RangeExpr { @@ -178,7 +179,7 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym table.TypeSymbol) { is_gen_or_and_assign_rhs := gen_or && !g.discard_or_result cur_line := if is_gen_or_and_assign_rhs { line := g.go_before_stmt(0) - g.out.write_string(tabs[g.indent]) + g.out.write_string(util.tabs(g.indent)) line } else { '' @@ -358,7 +359,7 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym table.TypeSymbol) { is_gen_or_and_assign_rhs := gen_or && !g.discard_or_result cur_line := if is_gen_or_and_assign_rhs { line := g.go_before_stmt(0) - g.out.write_string(tabs[g.indent]) + g.out.write_string(util.tabs(g.indent)) line } else { '' diff --git a/vlib/v/gen/js/js.v b/vlib/v/gen/js/js.v index 9a09b05f00..9ebf4ec909 100644 --- a/vlib/v/gen/js/js.v +++ b/vlib/v/gen/js/js.v @@ -21,8 +21,6 @@ const ( 'int_literal', 'float_literal', 'size_t', 'bool', 'string', 'map', 'array'] shallow_equatables = [table.Kind.i8, .i16, .int, .i64, .byte, .u16, .u32, .u64, .f32, .f64, .int_literal, .float_literal, .size_t, .bool, .string] - 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\t', '\t\t\t\t\t\t\t\t\t', '\t\t\t\t\t\t\t\t\t'] ) struct Namespace { @@ -261,7 +259,7 @@ fn verror(msg string) { [inline] pub fn (mut g JsGen) gen_indent() { if g.ns.indent > 0 && g.empty_line { - g.ns.out.write_string(js.tabs[g.ns.indent]) + g.ns.out.write_string(util.tabs(g.ns.indent)) } g.empty_line = false } diff --git a/vlib/v/util/util.v b/vlib/v/util/util.v index 2ec63f6cb2..d6eb9de456 100644 --- a/vlib/v/util/util.v +++ b/vlib/v/util/util.v @@ -25,6 +25,25 @@ pub const ( } ) +const ( + const_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\t', + ] +) + +pub fn tabs(n int) string { + return if n < util.const_tabs.len { util.const_tabs[n] } else { '\t'.repeat(n) } +} + // vhash() returns the build string C.V_COMMIT_HASH . See cmd/tools/gen_vc.v . pub fn vhash() string { mut buf := [50]byte{}