This repository has been archived by the owner on May 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlineParser.go
93 lines (81 loc) · 1.88 KB
/
lineParser.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
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
package main
import (
"regexp"
"strconv"
"strings"
)
var TAGS_REGEXP = regexp.MustCompile(`^(\[.+\])+`)
var INFO_REGEXP = regexp.MustCompile(`^\s*(\w+)\s*:(.*)$`)
var TIME_REGEXP = regexp.MustCompile(`^\s*(\d+)\s*:\s*(\d+(\s*[\.:]\s*\d+)?)\s*$`)
type LineType string
const (
INVALID LineType = "INVALID"
INFO LineType = "INFO"
TIME LineType = "TIME"
)
type InvalidLine struct {
Type LineType
}
type TimeLine struct {
Type LineType
Timestamps []float64
Content string
}
type InfoLine struct {
Type LineType
Key string
Value string
}
func parseTags(line string) (tags []string, content string) {
line = strings.TrimSpace(line)
matches := TAGS_REGEXP.FindStringSubmatch(line)
if len(matches) == 0 {
return nil, line
}
tag := matches[0]
content = line[len(tag):]
tags = strings.Split(tag[1:len(tag)-1], "][")
return tags, content
}
func parseTime(tags []string, content string) TimeLine {
timestamps := make([]float64, 0)
for _, tag := range tags {
matches := TIME_REGEXP.FindStringSubmatch(tag)
if len(matches) != 0 {
minutes, _ := strconv.Atoi(matches[1])
seconds, _ := strconv.ParseFloat(strings.ReplaceAll(matches[2], " ", ""), 64)
timestamps = append(timestamps, toFixed(float64(minutes*60)+seconds, 4))
}
}
return TimeLine{
Type: TIME,
Timestamps: timestamps,
Content: strings.TrimSpace(content),
}
}
func parseInfo(tag string) InfoLine {
matches := INFO_REGEXP.FindStringSubmatch(tag)
if len(matches) != 0 {
return InfoLine{
Type: INFO,
Key: strings.TrimSpace(matches[1]),
Value: strings.TrimSpace(matches[2]),
}
}
return InfoLine{
Type: INVALID,
}
}
func parseLine(line string) interface{} {
tags, content := parseTags(line)
if len(tags) > 0 {
if TIME_REGEXP.MatchString(tags[0]) {
return parseTime(tags, content)
} else {
return parseInfo(tags[0])
}
}
return InvalidLine{
Type: INVALID,
}
}