Skip to content

Commit 81e1e90

Browse files
committed
restore support for escaped quoted char
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 82a567a commit 81e1e90

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

dotenv/fixtures/quoted.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ OPTION_J = 'first line
1111
second line
1212
third line
1313
and so on'
14+
OPTION_K='Let\'s go!'
1415
OPTION_Z = "last value"

dotenv/godotenv_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ func TestLoadQuotedEnv(t *testing.T) {
171171
second line
172172
third line
173173
and so on`,
174+
"OPTION_K": "Let's go!",
174175
"OPTION_Z": "last value",
175176
}
176177

dotenv/parser.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,27 +159,34 @@ func (p *parser) extractVarValue(src string, envMap map[string]string, lookupFn
159159

160160
previousCharIsEscape := false
161161
// lookup quoted string terminator
162+
var chars []byte
162163
for i := 1; i < len(src); i++ {
163-
if src[i] == '\n' {
164+
char := src[i]
165+
if char == '\n' {
164166
p.line++
165167
}
166-
if char := src[i]; char != quote {
168+
if char != quote {
167169
if !previousCharIsEscape && char == '\\' {
168170
previousCharIsEscape = true
169-
} else {
171+
continue
172+
}
173+
if previousCharIsEscape {
170174
previousCharIsEscape = false
175+
chars = append(chars, '\\')
171176
}
177+
chars = append(chars, char)
172178
continue
173179
}
174180

175181
// skip escaped quote symbol (\" or \', depends on quote)
176182
if previousCharIsEscape {
177183
previousCharIsEscape = false
184+
chars = append(chars, char)
178185
continue
179186
}
180187

181188
// trim quotes
182-
value := string(src[1:i])
189+
value := string(chars)
183190
if quote == prefixDoubleQuote {
184191
// expand standard shell escape sequences & then interpolate
185192
// variables on the result

0 commit comments

Comments
 (0)