-
Notifications
You must be signed in to change notification settings - Fork 28
/
summary.go
102 lines (88 loc) · 2.44 KB
/
summary.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
94
95
96
97
98
99
100
101
102
package klog
import (
"errors"
"regexp"
)
// RecordSummary contains the summary lines of the overall summary that
// appears underneath the date of a record.
type RecordSummary []string
// EntrySummary contains the summary line that appears behind the time value
// of an entry.
type EntrySummary []string
var recordSummaryLinePattern = regexp.MustCompile(`^[\p{Zs}\t]`)
// NewRecordSummary creates a new RecordSummary from individual lines of text.
// None of the lines can start with blank characters, and none of the lines
// can be empty or blank.
func NewRecordSummary(line ...string) (RecordSummary, error) {
for _, l := range line {
if len(l) == 0 || recordSummaryLinePattern.MatchString(l) {
return nil, errors.New("MALFORMED_SUMMARY")
}
}
return line, nil
}
var entrySummaryLinePattern = regexp.MustCompile("^[\\p{Zs}\t]*$")
// NewEntrySummary creates an EntrySummary from individual lines of text.
// Except for the first line, none of the lines can be empty or blank.
func NewEntrySummary(line ...string) (EntrySummary, error) {
for i, l := range line {
if i == 0 {
continue
}
if len(l) == 0 || entrySummaryLinePattern.MatchString(l) {
return nil, errors.New("MALFORMED_SUMMARY")
}
}
return line, nil
}
func (s RecordSummary) Lines() []string {
return s
}
func (s EntrySummary) Lines() []string {
return RecordSummary(s).Lines()
}
func (s RecordSummary) Tags() *TagSet {
tags := NewEmptyTagSet()
for _, l := range s {
for _, m := range HashTagPattern.FindAllStringSubmatch(l, -1) {
tag, _ := NewTagFromString(m[0])
tags.Put(tag)
}
}
return &tags
}
// Tags returns the tags that the entry summary contains.
func (s EntrySummary) Tags() *TagSet {
return RecordSummary(s).Tags()
}
func (s RecordSummary) Equals(summary RecordSummary) bool {
if len(s) != len(summary) {
return false
}
for i, l := range s {
if l != summary[i] {
return false
}
}
return true
}
func (s EntrySummary) Equals(summary EntrySummary) bool {
if len(s) == 1 && s[0] == "" && summary == nil {
// In the case of entry summary, an empty one matches nil.
return true
}
return RecordSummary(s).Equals(RecordSummary(summary))
}
// Append appends a text to an entry summary
func (s EntrySummary) Append(appendableText string) EntrySummary {
if len(s) == 0 {
return []string{appendableText}
}
delimiter := ""
lastLine := s[len(s)-1]
if len(lastLine) > 0 {
delimiter = " "
}
s[len(s)-1] = lastLine + delimiter + appendableText
return s
}