Repository navigation
Expand file tree
/
Copy pathsection.go
More file actions
165 lines (159 loc) · 4.18 KB
/
Copy pathsection.go
File metadata and controls
165 lines (159 loc) · 4.18 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
package syntax
import (
"strings"
"unicode"
"unicode/utf8"
)
func (p *parser) parseSections(start int) *ParseError {
s := p.source
limit := contentEnd(s, len(s))
if start >= limit {
return nil
}
end, next := lineEnd(s, start)
if end != start {
if !p.recover {
return &ParseError{InvalidSectionSeparator, start}
}
p.diagnostics = append(p.diagnostics, ParseError{InvalidSectionSeparator, start})
} else {
start = next
}
for start < limit {
end, next = lineEnd(s, start)
if end != start {
break
}
start = next
}
count, first, breaking, err := p.scanFooters(start, limit, nil)
if err != nil {
return err
}
if count == 0 {
p.commit.body = Span{start, limit}
return nil
}
if first > start {
p.commit.body = Span{start, contentEnd(s, first)}
}
p.commit.footer = Span{first, limit}
p.commit.footerBreaking = breaking
p.commit.footers = make([]Footer, count)
p.scanFooters(start, limit, p.commit.footers)
return nil
}
// contentEnd removes terminal line endings, preserving all other whitespace.
func contentEnd(s string, end int) int {
for end > 0 && s[end-1] == '\n' {
end--
if end > 0 && s[end-1] == '\r' {
end--
}
}
return end
}
// scanFooters counts entries first, then fills exactly sized storage. A value
// lookahead ends at the next lexical token/separator pair, so each line is
// visited a bounded number of times even when candidate values are empty.
func (p *parser) scanFooters(start, limit int, out []Footer) (count, first int, breaking bool, err *ParseError) {
s := p.source
boundary := true
for pos := start; pos < limit; {
end, next := lineEnd(s, pos)
if count > 0 || boundary {
f, ok := footerLine(s, pos, end)
if ok {
token := s[f.token.Start:f.token.End]
isBreaking := token == "BREAKING CHANGE" || token == "BREAKING-CHANGE"
if !footerHasValue(s, f.value.Start, end, next, limit) {
if count > 0 || isBreaking {
if !p.recover {
return count, first, breaking, &ParseError{InvalidFooterValue, f.value.Start}
}
if out == nil {
p.diagnostics = append(p.diagnostics, ParseError{InvalidFooterValue, f.value.Start})
}
} else {
boundary = false
pos = next
continue
}
}
if count == 0 {
first = pos
}
if out != nil {
if count > 0 {
valueEnd := pos - 1 // Exclude just the previous line ending.
if valueEnd > 0 && s[valueEnd-1] == '\r' {
valueEnd--
}
out[count-1].value.End = valueEnd
}
out[count] = f
}
breaking = breaking || isBreaking
count++
}
}
boundary = pos == end
pos = next
}
if out != nil && count > 0 {
out[count-1].value.End = limit
}
return count, first, breaking, nil
}
func footerHasValue(s string, start, end, next, limit int) bool {
if nonblank(s[start:end]) {
return true
}
for pos := next; pos < limit; {
end, next = lineEnd(s, pos)
if _, ok := footerLine(s, pos, end); ok {
return false
}
if nonblank(s[pos:end]) {
return true
}
pos = next
}
return false
}
// footerLine recognises syntax only. Value completeness is checked separately.
func footerLine(s string, start, end int) (Footer, bool) {
line := s[start:end]
if strings.HasPrefix(line, "BREAKING CHANGE: ") {
return Footer{source: s, token: Span{start, start + 15}, value: Span{start + 17, end}, separator: Colon}, true
}
i, noun := 0, false
for i < len(line) && line[i] != ':' && line[i] != ' ' {
r, size := utf8.DecodeRuneInString(line[i:])
if r < utf8.RuneSelf {
if !((r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || r == '-') {
return Footer{}, false
}
} else if !unicode.IsLetter(r) && !unicode.IsNumber(r) && !unicode.IsMark(r) {
return Footer{}, false
}
noun = noun || unicode.IsLetter(r) || unicode.IsNumber(r)
i += size
}
if !noun || i+1 >= len(line) {
return Footer{}, false
}
var sep Separator
switch {
case line[i] == ':' && line[i+1] == ' ':
sep = Colon
case line[i] == ' ' && line[i+1] == '#':
if line[:i] == "BREAKING-CHANGE" {
return Footer{}, false
}
sep = Hash
default:
return Footer{}, false
}
return Footer{source: s, token: Span{start, start + i}, value: Span{start + i + 2, end}, separator: sep}, true
}