Skip to content

Commit

Permalink
Merge pull request #180 from stevengj/newstring
Browse files Browse the repository at this point in the history
compatibility for JuliaLang/julia#19449
  • Loading branch information
TotalVerb authored Jan 4, 2017
2 parents e24fc41 + ed49433 commit 416cfbf
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/JSON.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function print_escaped(io::IO, s::AbstractString)
end

function print_escaped(io::IO, s::Compat.UTF8String)
@inbounds for c in s.data
@inbounds for c in Vector{UInt8}(s)
Base.write(io, ESCAPED_ARRAY[c + 0x01])
end
end
Expand Down
18 changes: 5 additions & 13 deletions src/Parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import Compat: String

export parse


# A string constructor function (not necessarily a type)
const _String = VERSION < v"0.4" ? utf8 : Compat.UTF8String

"""
Like `isspace`, but work on bytes and includes only the four whitespace
characters defined by the JSON standard: space, tab, line feed, and carriage
Expand Down Expand Up @@ -272,7 +268,7 @@ function parse_string(ps::ParserState)
if c == BACKSLASH
c = advance!(ps)
if c == LATIN_U # Unicode escape
append!(b, string(read_unicode_escape!(ps)).data)
append!(b, Vector{UInt8}(string(read_unicode_escape!(ps))))
else
c = get(ESCAPES, c, 0x00)
c == 0x00 && _error(E_BAD_ESCAPE, ps)
Expand All @@ -282,7 +278,7 @@ function parse_string(ps::ParserState)
elseif c < SPACE
_error(E_BAD_CONTROL, ps)
elseif c == STRING_DELIM
return _String(b)
return Compat.UTF8String(b)
end

push!(b, c)
Expand All @@ -308,12 +304,8 @@ byte before `to`. Bytes enclosed should all be ASCII characters.
function float_from_bytes(bytes::Vector{UInt8}, from::Int, to::Int)
# The ccall is not ideal (Base.tryparse would be better), but it actually
# makes an 2× difference to performance
@static if VERSION v"0.4"
ccall(:jl_try_substrtod, Nullable{Float64},
(Ptr{UInt8}, Csize_t, Csize_t), bytes, from - 1, to - from + 1)
else
tryparse(Float64, Compat.ASCIIString(bytes[from:to]))
end
ccall(:jl_try_substrtod, Nullable{Float64},
(Ptr{UInt8}, Csize_t, Csize_t), bytes, from - 1, to - from + 1)
end

"""
Expand Down Expand Up @@ -378,7 +370,7 @@ function unparameterize_type{T}(::Type{T})
end

function parse{T<:Associative}(str::AbstractString; dicttype::Type{T}=Dict{Compat.UTF8String,Any})
ps = MemoryParserState(_String(str).data, 1)
ps = MemoryParserState(Vector{UInt8}(Compat.UTF8String(str)), 1)
v = parse_value(ps, unparameterize_type(T))
chomp_space!(ps)
if hasmore(ps)
Expand Down
6 changes: 3 additions & 3 deletions src/specialized.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function parse_string(ps::MemoryParserState)
parse_string(ps, b)
end

_String(b)
Compat.UTF8String(b)
end

"""
Expand Down Expand Up @@ -58,7 +58,7 @@ function predict_string(ps::MemoryParserState)
if ps.utf8data[s] == LATIN_U # Unicode escape
t = ps.s
ps.s = s + 1
len += length(string(read_unicode_escape!(ps)).data)
len += sizeof(string(read_unicode_escape!(ps)))
s = ps.s
ps.s = t
continue
Expand Down Expand Up @@ -97,7 +97,7 @@ function parse_string(ps::MemoryParserState, b)
c = ps.utf8data[s]
if c == LATIN_U # Unicode escape
ps.s = s + 1
for c in string(read_unicode_escape!(ps)).data
for c in Vector{UInt8}(string(read_unicode_escape!(ps)))
i += 1
b[i] = c
end
Expand Down

0 comments on commit 416cfbf

Please sign in to comment.