From 45d51c76dace3898122f3dd86d1841114dba8b7c Mon Sep 17 00:00:00 2001 From: zakuro Date: Sun, 7 Mar 2021 18:24:05 +0900 Subject: [PATCH] v.util: add an iabs(x) helper (#9172) --- vlib/v/fmt/fmt.v | 5 ----- vlib/v/fmt/struct.v | 3 ++- vlib/v/util/util.v | 5 +++++ 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 0530041fd6..25b5c19317 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -568,11 +568,6 @@ pub fn (mut f Fmt) sum_type_decl(node ast.SumTypeDecl) { f.comments(node.comments, has_nl: false) } -[inline] -fn abs(v int) int { - return if v >= 0 { v } else { -v } -} - pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) { if node.is_pub { f.write('pub ') diff --git a/vlib/v/fmt/struct.v b/vlib/v/fmt/struct.v index 8e7d2f83ad..f972e38c29 100644 --- a/vlib/v/fmt/struct.v +++ b/vlib/v/fmt/struct.v @@ -75,7 +75,8 @@ fn (mut list []CommentAndExprAlignInfo) add_info(attrs_len int, type_len int, li list.add_new_info(attrs_len, type_len, line) return } - d_len := abs(list[i].max_attrs_len - attrs_len) + abs(list[i].max_type_len - type_len) + d_len := util.iabs(list[i].max_attrs_len - attrs_len) + + util.iabs(list[i].max_type_len - type_len) if !(d_len < fmt.threshold_to_align_struct) { list.add_new_info(attrs_len, type_len, line) return diff --git a/vlib/v/util/util.v b/vlib/v/util/util.v index 28d1c11a17..dbf83fb14e 100644 --- a/vlib/v/util/util.v +++ b/vlib/v/util/util.v @@ -363,6 +363,11 @@ pub fn imax(a int, b int) int { return if a > b { a } else { b } } +[inline] +pub fn iabs(v int) int { + return if v > 0 { v } else { -v } +} + pub fn replace_op(s string) string { if s.len == 1 { last_char := s[s.len - 1]