-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbars_parse.go
More file actions
166 lines (154 loc) · 4.22 KB
/
bars_parse.go
File metadata and controls
166 lines (154 loc) · 4.22 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// bars: generate a bar chart in the terminal or as HTML snippet
// Copyright © 2021 Alexander Kulbartsch
// License: AGPL-3.0-or-later (GNU Affero General Public License 3 or later)
/*
This file is part of bars.
bars is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of
the License, or any later version.
bars is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public
License along with bars. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"fmt"
"log"
"strconv"
"strings"
"unicode/utf8"
)
// WhiteSpacePlusTrim trims space, tabs and new lines
func WhiteSpacePlusTrim(in string) string {
cutset := " \t\n" + *myParam.trimChars
return strings.Trim(in, cutset)
}
// RemoveInvalidChars from text checking against set of valid chars
func RemoveInvalidChars(text, valid string) string {
textLen := len(text)
validLen := len(valid)
if textLen == 0 {
return text
}
if validLen == 0 {
return ""
}
var result string
for _, l := range text {
letter := string(l)
if strings.ContainsAny(letter, valid) {
result = result + letter
}
}
return result
}
// PurifyNumber removes non numerical characters from text
func PurifyNumber(numberText string, comma bool) string {
var numberChars string
if comma {
numberChars = "0123456789+-,E"
} else {
numberChars = "0123456789+-.E"
}
re := RemoveInvalidChars(numberText, numberChars)
if comma {
re = strings.ReplaceAll(re, ",", ".")
}
return re
}
// SplitLabelNumber separates the label from the value
func SplitLabelNumber(text string, numChars string, fromRight bool, comma bool) (label string, valueText string, value float64, err error) {
if len(text) == 0 || len(numChars) == 0 {
return text, "", 0, nil
}
runes := []rune(text)
l := len(runes)
var r rune
var nt string // number text
var lbl string // label
isNum := true
for i := 0; i < l; i += 1 {
if fromRight {
r = runes[l-i-1]
} else {
r = runes[i]
}
sr := string(r)
if isNum {
if strings.ContainsAny(sr, numChars) {
if fromRight {
nt = sr + nt
} else {
nt = nt + sr
}
} else { // no number char
isNum = false
}
}
if !isNum {
if fromRight {
lbl = sr + lbl
} else {
lbl = lbl + sr
}
}
}
nv, err := strconv.ParseFloat(PurifyNumber(nt, comma), 64)
return WhiteSpacePlusTrim(lbl), nt, nv, err
}
func parseLine(text string) {
numberChars := "0123456789+-.,E" + *myParam.addNumChars
// numStart, numEnd, numCount := NumberCharsLength(text, numberChars, *myParam.valueAtEnd)
label, valTxt, value, err := SplitLabelNumber(text, numberChars, *myParam.valueAtEnd, *myParam.comma)
if len(valTxt) == 0 {
if *myParam.verbose {
log.Println("Line " + strconv.Itoa(myValues.lines) + " has no valid number.")
}
return
}
if err != nil {
if *myParam.verbose {
log.Println("Line " + strconv.Itoa(myValues.lines) + " Parse error:")
log.Println(err)
}
return
}
valueText := fmt.Sprintf("%."+strconv.Itoa(*myParam.decimals)+"f", value)
valTxtLen := len(valueText)
myValues.sum += value
// ...
labelLength := utf8.RuneCountInString(label)
if *myParam.verbose {
log.Println("label: \"" + label + "\" value: " + strconv.FormatFloat(value, 'G', -1, 64))
}
chartData = append(chartData, chartDataType{value, valueText, label})
myValues.linesValid += 1
if myValues.linesValid == 1 {
myValues.labelMaxLen = labelLength
myValues.labelMinLen = labelLength
myValues.valueMax = value
myValues.valueMin = value
myValues.valueTxtLen = valTxtLen
} else {
if myValues.labelMaxLen < labelLength {
myValues.labelMaxLen = labelLength
}
if myValues.labelMinLen > labelLength {
myValues.labelMinLen = labelLength
}
if myValues.valueMax < value {
myValues.valueMax = value
}
if myValues.valueMin > value {
myValues.valueMin = value
}
if myValues.valueTxtLen < valTxtLen {
myValues.valueTxtLen = valTxtLen
}
}
}
// EOF