aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/v/fmt/align.v
blob: f767abdb939598c33428dace1615d7e7e7eeea5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module fmt

import math.mathutil

const struct_field_align_threshold = 8

struct AlignInfo {
mut:
	line_nr      int
	max_len      int
	max_type_len int
}

struct AddInfoConfig {
	use_threshold bool
}

fn (mut infos []AlignInfo) add_new_info(len int, type_len int, line int) {
	infos << AlignInfo{
		line_nr: line
		max_len: len
		max_type_len: type_len
	}
}

[direct_array_access]
fn (mut infos []AlignInfo) add_info(len int, type_len int, line int, cfg AddInfoConfig) {
	if infos.len == 0 {
		infos.add_new_info(len, type_len, line)
		return
	}
	i := infos.len - 1
	if line - infos[i].line_nr > 1 {
		infos.add_new_info(len, type_len, line)
		return
	}
	if cfg.use_threshold {
		len_diff := mathutil.abs(infos[i].max_len - len) +
			mathutil.abs(infos[i].max_type_len - type_len)

		if len_diff >= fmt.struct_field_align_threshold {
			infos.add_new_info(len, type_len, line)
			return
		}
	}
	infos[i].line_nr = line
	if len > infos[i].max_len {
		infos[i].max_len = len
	}
	if type_len > infos[i].max_type_len {
		infos[i].max_type_len = type_len
	}
}