forked from mikeshimura/goreport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
53 lines (49 loc) · 989 Bytes
/
util.go
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
package goreport
import (
//"fmt"
"io/ioutil"
"strings"
)
func AddComma(s string) string {
if strings.Index(s, ".") == -1 {
return addCommaSub(s)
}
ss := strings.Split(s, ".")
ss[0] = addCommaSub(ss[0])
return ss[0] + "." + ss[1]
}
func addCommaSub(s string) string {
res := ""
if len(s) < 4 {
return s
}
pos := len(s) % 3
if pos > 0 {
res += s[0:pos] + ","
}
for i := pos; i < len(s); i += 3 {
res += s[i : i+3]
//fmt.Printf("pos %v \n", i)
if i < len(s)-3 {
res += ","
}
}
return res
}
func ReadTextFile(filename string, colno int) []interface{} {
res, _ := ioutil.ReadFile(filename)
return ReadBytes(res, colno)
}
func ReadBytes(src []byte, colno int) []interface{} {
lines := strings.Split(string(src), "\n")
list := make([]interface{}, 0, 100)
for _, line := range lines {
line = strings.Replace(line, "\r", "", -1)
cols := strings.Split(line, "\t")
if len(cols) < colno {
continue
}
list = append(list, cols)
}
return list
}