-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace.go
159 lines (137 loc) · 3.99 KB
/
replace.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
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
package main
import "regexp"
const LineFeed = 10
const ContextLineCount = 3
type Replace struct {
Search, Replace string
Regexp bool
}
func (r *Replace) Execute(in string, callback ReplaceCallback) string {
result := ""
remainder := in
search := r.Search
if !r.Regexp {
search = regexp.QuoteMeta(search)
}
rgx := regexp.MustCompile(search)
var match []int
replacement := []byte{}
replacementInfo := func() ReplacementInfo {
content := result + remainder
matchOffset := len(result)
matchStart := match[0] + matchOffset
matchEnd := match[1] + matchOffset
return newReplacementInfo(content, string(replacement), matchStart, matchEnd)
}
for {
match = rgx.FindStringSubmatchIndex(remainder)
if match == nil {
result += remainder
break
}
replacement = []byte{}
replacement = rgx.ExpandString(replacement, r.Replace, in, match)
if callback != nil && !callback(replacementInfo()) {
result += remainder[0:match[1]]
} else {
result += remainder[0:match[0]] + string(replacement)
}
remainder = remainder[match[1]:]
}
return result
}
type ReplaceCallback func(info ReplacementInfo) bool
type ReplacementInfo struct {
LinesBeforeMatch string
Match string
MatchLine string
MatchLineMatchIndex []int
Repl string
ReplLine string
ReplLineReplIndex []int
LinesAfterMatch string
}
func newReplacementInfo(content, replacement string, matchStart, matchEnd int) ReplacementInfo {
lineStartIndex := findLineStartIndex(content, matchStart)
lineEndIndex := findLineEndIndex(content, matchEnd)
matchLine := content[lineStartIndex : lineEndIndex+1]
lineContentBeforeMatch := matchLine[:matchStart-lineStartIndex]
lineContentAfterMatch := matchLine[matchEnd-lineStartIndex:]
replacementLine := lineContentBeforeMatch + replacement + lineContentAfterMatch
matchLineMatchIndex := []int{
matchStart - lineStartIndex,
matchEnd - lineStartIndex,
}
replacementLineReplacementIndex := []int{
matchStart - lineStartIndex,
matchStart - lineStartIndex + len(replacement),
}
return ReplacementInfo{
LinesBeforeMatch: linesBeforeMatch(content, lineStartIndex),
Match: matchLine[matchLineMatchIndex[0]:matchLineMatchIndex[1]],
MatchLine: matchLine,
MatchLineMatchIndex: matchLineMatchIndex,
Repl: replacementLine[replacementLineReplacementIndex[0]:replacementLineReplacementIndex[1]],
ReplLine: replacementLine,
ReplLineReplIndex: replacementLineReplacementIndex,
LinesAfterMatch: linesAfterMatch(content, lineEndIndex),
}
}
func linesBeforeMatch(content string, lineStartIndex int) string {
from := findPreviousLinesStartIndex(content, lineStartIndex, ContextLineCount)
to := lineStartIndex
return content[from:to]
}
func linesAfterMatch(content string, lineEndIndex int) string {
if lineEndIndex == len(content)-1 {
return ""
}
from := lineEndIndex
to := findNextLinesEndIndex(content, lineEndIndex, ContextLineCount)
if from < len(content)-1 {
from++
}
return content[from : to+1]
}
func findLineStartIndex(content string, fromIndex int) int {
index := fromIndex
if index <= 0 {
return 0
}
for index > 0 {
index--
if content[index] == LineFeed {
return index + 1
}
}
return 0
}
func findLineEndIndex(content string, fromIndex int) int {
index := fromIndex
if index >= len(content)-1 {
return len(content) - 1
}
for index <= len(content)-1 {
if content[index] == LineFeed {
return index
}
index++
}
return len(content) - 1
}
func findPreviousLinesStartIndex(content string, fromIndex int, n int) int {
// go to current lines start index
index := findLineStartIndex(content, fromIndex)
for i := 0; i < n; i++ {
index = findLineStartIndex(content, index-1)
}
return index
}
func findNextLinesEndIndex(content string, fromIndex int, n int) int {
// go to current lines end index
index := findLineEndIndex(content, fromIndex)
for i := 0; i < n; i++ {
index = findLineEndIndex(content, index+1)
}
return index
}