Skip to content

Commit

Permalink
restore support for escaped quoted char
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Dec 18, 2023
1 parent 82a567a commit 15f9475
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions dotenv/fixtures/quoted.env
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ OPTION_J = 'first line
second line
third line
and so on'
OPTION_K='Let\'s go!'
OPTION_Z = "last value"
1 change: 1 addition & 0 deletions dotenv/godotenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func TestLoadQuotedEnv(t *testing.T) {
second line
third line
and so on`,
"OPTION_K": "Let's go!",
"OPTION_Z": "last value",
}

Expand Down
15 changes: 11 additions & 4 deletions dotenv/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,27 +159,34 @@ func (p *parser) extractVarValue(src string, envMap map[string]string, lookupFn

previousCharIsEscape := false
// lookup quoted string terminator
var chars []byte
for i := 1; i < len(src); i++ {
if src[i] == '\n' {
char := src[i]
if char == '\n' {
p.line++
}
if char := src[i]; char != quote {
if char != quote {
if !previousCharIsEscape && char == '\\' {
previousCharIsEscape = true
} else {
continue
}
if previousCharIsEscape {
previousCharIsEscape = false
chars = append(chars, '\\')
}
chars = append(chars, char)
continue
}

// skip escaped quote symbol (\" or \', depends on quote)
if previousCharIsEscape {
previousCharIsEscape = false
chars = append(chars, char)
continue
}

// trim quotes
value := string(src[1:i])
value := string(chars)
if quote == prefixDoubleQuote {
// expand standard shell escape sequences & then interpolate
// variables on the result
Expand Down

0 comments on commit 15f9475

Please sign in to comment.