@@ -98,14 +98,8 @@ func Readln(r *bufio.Reader) (string, error) {
98
98
99
99
// Checks if there is a match in content, based on search options
100
100
func searchMatch (content string ) (bool ) {
101
- if opts .Regex {
102
- if opts .SearchRegex .MatchString (content ) {
103
- return true
104
- }
105
- } else {
106
- if strings .Contains (content , opts .Search ) {
107
- return true
108
- }
101
+ if opts .SearchRegex .MatchString (content ) {
102
+ return true
109
103
}
110
104
111
105
return false
@@ -117,12 +111,7 @@ func replaceText(content string) (string, bool) {
117
111
118
112
if searchMatch (content ) {
119
113
status = true
120
-
121
- if opts .Regex {
122
- content = opts .SearchRegex .ReplaceAllString (content , opts .Replace )
123
- } else {
124
- content = strings .Replace (content , opts .Search , opts .Replace , - 1 )
125
- }
114
+ content = opts .SearchRegex .ReplaceAllString (content , opts .Replace )
126
115
}
127
116
128
117
return content , status
@@ -144,6 +133,8 @@ func writeContentToFile(filepath string, content string) {
144
133
if err != nil {
145
134
panic (err )
146
135
}
136
+
137
+ logMessage (fmt .Sprintf ("%s found and replaced match" , filepath ))
147
138
}
148
139
}
149
140
@@ -159,18 +150,27 @@ func logError(err error) {
159
150
fmt .Printf ("Error: %s\n " , err )
160
151
}
161
152
162
- // Process search option
153
+ // Process search term
163
154
// Compiles regexp if regexp is used
164
- func processSearch () {
155
+ func processSearchTerm () {
156
+ var regex string
157
+
165
158
if opts .Regex {
166
- regex := opts .Search
159
+ regex = opts .Search
160
+ } else {
161
+ regex = regexp .QuoteMeta (opts .Search )
162
+ }
167
163
168
- if opts .IgnoreCase {
169
- regex = "(?i:" + regex + ")"
170
- }
171
164
172
- opts .SearchRegex = regexp .MustCompile (regex )
165
+ if opts .IgnoreCase {
166
+ regex = "(?i:" + regex + ")"
173
167
}
168
+
169
+ if opts .Verbose {
170
+ logMessage (fmt .Sprintf ("Using regular expression: %s" , regex ))
171
+ }
172
+
173
+ opts .SearchRegex = regexp .MustCompile (regex )
174
174
}
175
175
176
176
func handleSpecialOptions (argparser * flags.Parser , args []string ) {
@@ -208,7 +208,7 @@ func main() {
208
208
209
209
handleSpecialOptions (argparser , args )
210
210
211
- processSearch ()
211
+ processSearchTerm ()
212
212
213
213
for i := range args {
214
214
var file string
0 commit comments