v.util: add an iabs(x) helper (#9172)

pull/9176/head
zakuro 2021-03-07 18:24:05 +09:00 committed by GitHub
parent 4c7b6ce2fd
commit 45d51c76da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 6 deletions

View File

@ -568,11 +568,6 @@ pub fn (mut f Fmt) sum_type_decl(node ast.SumTypeDecl) {
f.comments(node.comments, has_nl: false) 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) { pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) {
if node.is_pub { if node.is_pub {
f.write('pub ') f.write('pub ')

View File

@ -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) list.add_new_info(attrs_len, type_len, line)
return 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) { if !(d_len < fmt.threshold_to_align_struct) {
list.add_new_info(attrs_len, type_len, line) list.add_new_info(attrs_len, type_len, line)
return return

View File

@ -363,6 +363,11 @@ pub fn imax(a int, b int) int {
return if a > b { a } else { b } 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 { pub fn replace_op(s string) string {
if s.len == 1 { if s.len == 1 {
last_char := s[s.len - 1] last_char := s[s.len - 1]