Skip to content

Commit

Permalink
Improved $-escape handling in interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
tecosaur committed Feb 19, 2024
1 parent 8c76e73 commit 3e2bd98
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ function interpolate(value::String, dotenv::Dict{String, String}, fallback::Abst
point = nextind(value, point)
if escaped
escaped = false
value[point] == '$' || write(interpval, '\\')
write(interpval, value[point])
continue
elseif value[point] == '\\'
escaped = true
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ end

@testset "Interpolation" begin
@test interpolate("hello", Dict{String, String}(), ENV) == "hello"
@test interpolate("hi\$there", Dict{String, String}(), ENV) == "hi"
@test interpolate("hi\\\$there", Dict{String, String}(), ENV) == "hi\$there"
@test interpolate("hi\\0there", Dict{String, String}(), ENV) == "hi\\0there"
@test interpolate("hello \$USER", Dict{String, String}(), ENV) == "hello $the_user"
@test interpolate("hello \$USER", Dict{String, String}("USER" => "fred"), ENV) == "hello fred"
@test interpolate("hello \${USER:-alice}", Dict{String, String}("USER" => "fred"), ENV) == "hello fred"
@test interpolate("hello \${USER}", Dict{String, String}(), ENV) == "hello $the_user"
@test interpolate("hello \$USER.", Dict{String, String}(), ENV) == "hello $the_user."
@test interpolate("hello \$USERR", Dict{String, String}(), ENV) == "hello "
Expand Down

0 comments on commit 3e2bd98

Please sign in to comment.