Skip to content

Commit

Permalink
do all unescaping and interpolation handling for normal strings in th…
Browse files Browse the repository at this point in the history
…e parser

for custom strings, handle only \\ and \" in the parser
b_str no longer has interpolation (b_str should maybe be removed)
remove str, I_str, E_str, L_str, L_mstr, I_mstr, E_mstr
fixes #107
  • Loading branch information
JeffBezanson committed Mar 8, 2013
1 parent cdb60fd commit 7123ad3
Show file tree
Hide file tree
Showing 8 changed files with 962 additions and 984 deletions.
7 changes: 0 additions & 7 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1129,17 +1129,10 @@ export
unsafe_pointer_to_objref,

# Macros
@str,
@I_str,
@E_str,
@b_str,
@L_str,
@r_str,
@v_str,
@mstr,
@L_mstr,
@I_mstr,
@E_mstr,
@unexpected,
@assert,
@cmd,
Expand Down
37 changes: 9 additions & 28 deletions base/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ end
## string escaping & unescaping ##

escape_nul(s::String, i::Int) =
!done(s,i) && '0' <= next(s,i)[1] <= '7' ? L"\x00" : L"\0"
!done(s,i) && '0' <= next(s,i)[1] <= '7' ? "\\x00" : "\\0"

isxdigit(c::Char) = '0'<=c<='9' || 'a'<=c<='f' || 'A'<=c<='F'
need_full_hex(s::String, i::Int) = !done(s,i) && isxdigit(next(s,i)[1])
Expand All @@ -499,14 +499,14 @@ function print_escaped(io, s::String, esc::String)
while !done(s,i)
c, j = next(s,i)
c == '\0' ? print(io, escape_nul(s,j)) :
c == '\e' ? print(io, L"\e") :
c == '\e' ? print(io, "\\e") :
c == '\\' ? print(io, "\\\\") :
contains(esc,c) ? print(io, '\\', c) :
7 <= c <= 13 ? print(io, '\\', "abtnvfr"[int(c-6)]) :
isprint(c) ? print(io, c) :
c <= '\x7f' ? print(io, L"\x", hex(c, 2)) :
c <= '\uffff' ? print(io, L"\u", hex(c, need_full_hex(s,j) ? 4 : 2)) :
print(io, L"\U", hex(c, need_full_hex(s,j) ? 8 : 4))
c <= '\x7f' ? print(io, "\\x", hex(c, 2)) :
c <= '\uffff' ? print(io, "\\u", hex(c, need_full_hex(s,j) ? 4 : 2)) :
print(io, "\\U", hex(c, need_full_hex(s,j) ? 8 : 4))
i = j
end
end
Expand Down Expand Up @@ -659,7 +659,7 @@ function unindent(s::String, indent::Int)
takebuf_string(buf)
end

function triplequoted(unescape::Function, args...)
function triplequoted(args...)
sx = { isa(arg,ByteString) ? arg : esc(arg) for arg in args }

indent = 0
Expand All @@ -684,7 +684,7 @@ function triplequoted(unescape::Function, args...)

for i in 1:length(sx)
if isa(sx[i],ByteString)
sx[i] = unescape(unindent(sx[i], indent))
sx[i] = unindent(sx[i], indent)
end
end

Expand All @@ -700,28 +700,9 @@ end

## core string macros ##

function singlequoted(unescape::Function, args...)
if length(args) == 1 return unescape(args[1]) end
sx = { isa(arg,ByteString) ? unescape(arg) : esc(arg) for arg in args }
Expr(:call, :string, sx...)
end

macro str(s...); singlequoted(unescape_string, s...); end
macro I_str(s...); singlequoted(x->unescape_chars(x,"\""), s...); end
macro E_str(s); check_utf8(unescape_string(s)); end

function byteliteral(args...)
sx = { isa(arg,ByteString) ? unescape_string(arg) : esc(arg) for arg in args }
writer(io,x...) = for w=x; write(io,w); end
Expr(:call, :sprint, writer, sx...)
end

macro b_str(s...); ex = byteliteral(s...); :(($ex).data); end
macro b_str(s); :($(unescape_string(s)).data); end

macro mstr(s...); triplequoted(unescape_string, s...); end
macro L_mstr(s); s; end
macro I_mstr(s...); triplequoted(x->unescape_chars(x,"\""), s...); end
macro E_mstr(s); triplequoted(unescape_string, s); end
macro mstr(s...); triplequoted(s...); end

## shell-like command parsing ##

Expand Down
2 changes: 1 addition & 1 deletion base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ end

end # baremodule Base

using Base.@str, Base.get, Base.ENV
using Base.get, Base.ENV

let JL_PRIVATE_LIBDIR = get(ENV, "JL_PRIVATE_LIBDIR", "lib/julia")
# create system image file
Expand Down
9 changes: 5 additions & 4 deletions base/version.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,17 @@ end
begin
const version_string = "Version $VERSION"
const banner_plain =
I" _
"""
_
_ _ _(_)_ | A fresh approach to technical computing
(_) | (_) (_) | Documentation: http://docs.julialang.org
_ _ _| |_ __ _ | Type \"help()\" to list help topics
_ _ _| |_ __ _ | Type "help()" to list help topics
| | | | | | |/ _` | |
| | |_| | | | (_| | | $version_string
_/ |\__'_|_|_|\__'_| | $commit_string
_/ |\\__'_|_|_|\\__'_| | $commit_string
|__/ |
"
"""
local tx = "\033[0m\033[1m" # text
local jl = "\033[0m\033[1m" # julia
local d1 = "\033[34m" # first dot
Expand Down
Loading

0 comments on commit 7123ad3

Please sign in to comment.