Skip to content

Commit

Permalink
refactor: small cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
luisdavim committed May 21, 2022
1 parent cc6b967 commit b7ad35e
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions gotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const (

// Pattern for detecting valid variable within a value
variablePattern = `(\\)?(\$)(\{?([A-Z0-9_]+)?\}?)`

// Byte order mark character
bom = "\xef\xbb\xbf"
)

// Env holds key/value pair of valid environment variable
Expand Down Expand Up @@ -121,18 +124,16 @@ func StrictParse(r io.Reader) (Env, error) {
env := make(Env)
scanner := bufio.NewScanner(r)

i := 1
bom := string([]byte{239, 187, 191})
firstLine := true

for scanner.Scan() {
line := scanner.Text()

if i == 1 {
if firstLine {
line = strings.TrimPrefix(line, bom)
firstLine = false
}

i++

err := parseLine(line, env)
if err != nil {
return env, err
Expand Down Expand Up @@ -167,8 +168,8 @@ func parseLine(s string, env Env) error {
val = rq.ReplaceAllString(val, "$2")

if hdq {
val = strings.Replace(val, `\n`, "\n", -1)
val = strings.Replace(val, `\r`, "\r", -1)
val = strings.ReplaceAll(val, `\n`, "\n")
val = strings.ReplaceAll(val, `\r`, "\r")

// Unescape all characters except $ so variables can be escaped properly
re := regexp.MustCompile(`\\([^$])`)
Expand Down Expand Up @@ -252,9 +253,8 @@ func parseVal(val string, env Env, ignoreNewlines bool) string {

if len(kv) > 1 {
val = kv[0]

for i := 1; i < len(kv); i++ {
parseLine(kv[i], env)
for _, l := range kv[1:] {
_ = parseLine(l, env)
}
}
}
Expand Down

0 comments on commit b7ad35e

Please sign in to comment.