Skip to content

Commit

Permalink
Merge pull request #90 from djherbis/master
Browse files Browse the repository at this point in the history
#89 move regexp.MustCompile to globals
  • Loading branch information
joho authored Oct 20, 2019
2 parents 5c0e6c6 + 992ab0e commit b09de68
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions godotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ func readFile(filename string) (envMap map[string]string, err error) {
return Parse(file)
}

var exportRegex = regexp.MustCompile(`^\s*(?:export\s+)?(.*?)\s*$`)

func parseLine(line string, envMap map[string]string) (key string, value string, err error) {
if len(line) == 0 {
err = errors.New("zero length string")
Expand Down Expand Up @@ -258,26 +260,30 @@ func parseLine(line string, envMap map[string]string) (key string, value string,
}
key = strings.TrimSpace(key)

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

// Parse the value
value = parseValue(splitString[1], envMap)
return
}

var (
singleQuotesRegex = regexp.MustCompile(`\A'(.*)'\z`)
doubleQuotesRegex = regexp.MustCompile(`\A"(.*)"\z`)
escapeRegex = regexp.MustCompile(`\\.`)
unescapeCharsRegex = regexp.MustCompile(`\\([^$])`)
)

func parseValue(value string, envMap map[string]string) string {

// trim
value = strings.Trim(value, " ")

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

rd := regexp.MustCompile(`\A"(.*)"\z`)
doubleQuotes := rd.FindStringSubmatch(value)
doubleQuotes := doubleQuotesRegex.FindStringSubmatch(value)

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

if doubleQuotes != nil {
// expand newlines
escapeRegex := regexp.MustCompile(`\\.`)
value = escapeRegex.ReplaceAllStringFunc(value, func(match string) string {
c := strings.TrimPrefix(match, `\`)
switch c {
Expand All @@ -299,8 +304,7 @@ func parseValue(value string, envMap map[string]string) string {
}
})
// unescape characters
e := regexp.MustCompile(`\\([^$])`)
value = e.ReplaceAllString(value, "$1")
value = unescapeCharsRegex.ReplaceAllString(value, "$1")
}

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

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

return r.ReplaceAllStringFunc(v, func(s string) string {
submatch := r.FindStringSubmatch(s)
func expandVariables(v string, m map[string]string) string {
return expandVarRegex.ReplaceAllStringFunc(v, func(s string) string {
submatch := expandVarRegex.FindStringSubmatch(s)

if submatch == nil {
return s
Expand Down

0 comments on commit b09de68

Please sign in to comment.