Skip to content
This repository was archived by the owner on Jan 12, 2022. It is now read-only.

Commit b09de68

Browse files
authored
Merge pull request joho#90 from djherbis/master
joho#89 move regexp.MustCompile to globals
2 parents 5c0e6c6 + 992ab0e commit b09de68

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

godotenv.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ func readFile(filename string) (envMap map[string]string, err error) {
209209
return Parse(file)
210210
}
211211

212+
var exportRegex = regexp.MustCompile(`^\s*(?:export\s+)?(.*?)\s*$`)
213+
212214
func parseLine(line string, envMap map[string]string) (key string, value string, err error) {
213215
if len(line) == 0 {
214216
err = errors.New("zero length string")
@@ -258,26 +260,30 @@ func parseLine(line string, envMap map[string]string) (key string, value string,
258260
}
259261
key = strings.TrimSpace(key)
260262

261-
re := regexp.MustCompile(`^\s*(?:export\s+)?(.*?)\s*$`)
262-
key = re.ReplaceAllString(splitString[0], "$1")
263+
key = exportRegex.ReplaceAllString(splitString[0], "$1")
263264

264265
// Parse the value
265266
value = parseValue(splitString[1], envMap)
266267
return
267268
}
268269

270+
var (
271+
singleQuotesRegex = regexp.MustCompile(`\A'(.*)'\z`)
272+
doubleQuotesRegex = regexp.MustCompile(`\A"(.*)"\z`)
273+
escapeRegex = regexp.MustCompile(`\\.`)
274+
unescapeCharsRegex = regexp.MustCompile(`\\([^$])`)
275+
)
276+
269277
func parseValue(value string, envMap map[string]string) string {
270278

271279
// trim
272280
value = strings.Trim(value, " ")
273281

274282
// check if we've got quoted values or possible escapes
275283
if len(value) > 1 {
276-
rs := regexp.MustCompile(`\A'(.*)'\z`)
277-
singleQuotes := rs.FindStringSubmatch(value)
284+
singleQuotes := singleQuotesRegex.FindStringSubmatch(value)
278285

279-
rd := regexp.MustCompile(`\A"(.*)"\z`)
280-
doubleQuotes := rd.FindStringSubmatch(value)
286+
doubleQuotes := doubleQuotesRegex.FindStringSubmatch(value)
281287

282288
if singleQuotes != nil || doubleQuotes != nil {
283289
// pull the quotes off the edges
@@ -286,7 +292,6 @@ func parseValue(value string, envMap map[string]string) string {
286292

287293
if doubleQuotes != nil {
288294
// expand newlines
289-
escapeRegex := regexp.MustCompile(`\\.`)
290295
value = escapeRegex.ReplaceAllStringFunc(value, func(match string) string {
291296
c := strings.TrimPrefix(match, `\`)
292297
switch c {
@@ -299,8 +304,7 @@ func parseValue(value string, envMap map[string]string) string {
299304
}
300305
})
301306
// unescape characters
302-
e := regexp.MustCompile(`\\([^$])`)
303-
value = e.ReplaceAllString(value, "$1")
307+
value = unescapeCharsRegex.ReplaceAllString(value, "$1")
304308
}
305309

306310
if singleQuotes == nil {
@@ -311,11 +315,11 @@ func parseValue(value string, envMap map[string]string) string {
311315
return value
312316
}
313317

314-
func expandVariables(v string, m map[string]string) string {
315-
r := regexp.MustCompile(`(\\)?(\$)(\()?\{?([A-Z0-9_]+)?\}?`)
318+
var expandVarRegex = regexp.MustCompile(`(\\)?(\$)(\()?\{?([A-Z0-9_]+)?\}?`)
316319

317-
return r.ReplaceAllStringFunc(v, func(s string) string {
318-
submatch := r.FindStringSubmatch(s)
320+
func expandVariables(v string, m map[string]string) string {
321+
return expandVarRegex.ReplaceAllStringFunc(v, func(s string) string {
322+
submatch := expandVarRegex.FindStringSubmatch(s)
319323

320324
if submatch == nil {
321325
return s

0 commit comments

Comments
 (0)