Skip to content

Commit

Permalink
Merge pull request #177 from JuliaIO/fw/common-submodule
Browse files Browse the repository at this point in the history
Factor bytes and errors into Common submodule
  • Loading branch information
TotalVerb authored Oct 26, 2016
2 parents 6b08c8d + 402e298 commit e24fc41
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 21 deletions.
11 changes: 11 additions & 0 deletions src/Common.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""
Internal implementation detail.
"""
module Common

using Compat

include("bytes.jl")
include("errors.jl")

end
22 changes: 4 additions & 18 deletions src/JSON.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ using Compat

export json # returns a compact (or indented) JSON representation as a string

include("Common.jl")
include("Parser.jl")
include("bytes.jl")

using .Common
import .Parser.parse

# These are temporary ways to bypass excess memory allocation
Expand Down Expand Up @@ -63,21 +64,6 @@ lower(x::Real) = Float64(x)

const INDENT=true
const NOINDENT=false
const REVERSE_ESCAPES = Dict(map(reverse, ESCAPES))
const escaped = Array(Vector{UInt8}, 256)
for c in 0x00:0xFF
escaped[c + 1] = if c == SOLIDUS
[SOLIDUS] # don't escape this one
elseif c 0x80
[c] # UTF-8 character copied verbatim
elseif haskey(REVERSE_ESCAPES, c)
[BACKSLASH, REVERSE_ESCAPES[c]]
elseif iscntrl(Char(c)) || !isprint(Char(c))
UInt8[BACKSLASH, LATIN_U, hex(c, 4)...]
else
[c]
end
end

type State{I}
indentstep::Int
Expand Down Expand Up @@ -134,14 +120,14 @@ end

function print_escaped(io::IO, s::AbstractString)
@inbounds for c in s
c <= '\x7f' ? Base.write(io, escaped[UInt8(c) + 0x01]) :
c <= '\x7f' ? Base.write(io, ESCAPED_ARRAY[@compat UInt8(c) + 0x01]) :
Base.print(io, c) #JSON is UTF8 encoded
end
end

function print_escaped(io::IO, s::Compat.UTF8String)
@inbounds for c in s.data
Base.write(io, escaped[c + 0x01])
Base.write(io, ESCAPED_ARRAY[c + 0x01])
end
end

Expand Down
6 changes: 3 additions & 3 deletions src/Parser.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module Parser #JSON
module Parser # JSON

using ..Common

using Compat
import Compat: String

export parse

include("bytes.jl")
include("errors.jl")

# A string constructor function (not necessarily a type)
const _String = VERSION < v"0.4" ? utf8 : Compat.UTF8String
Expand Down
23 changes: 23 additions & 0 deletions src/bytes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,26 @@ const ESCAPES = Dict(
LATIN_N => NEWLINE,
LATIN_R => RETURN,
LATIN_T => TAB)

const REVERSE_ESCAPES = Dict(map(reverse, ESCAPES))
const ESCAPED_ARRAY = Array(Vector{UInt8}, 256)
for c in 0x00:0xFF
ESCAPED_ARRAY[c + 1] = if c == SOLIDUS
[SOLIDUS] # don't escape this one
elseif c 0x80
[c] # UTF-8 character copied verbatim
elseif haskey(REVERSE_ESCAPES, c)
[BACKSLASH, REVERSE_ESCAPES[c]]
elseif iscntrl(@compat Char(c)) || !isprint(@compat Char(c))
UInt8[BACKSLASH, LATIN_U, hex(c, 4)...]
else
[c]
end
end

export BACKSPACE, TAB, NEWLINE, FORM_FEED, RETURN, SPACE, STRING_DELIM,
PLUS_SIGN, DELIMITER, MINUS_SIGN, DECIMAL_POINT, SOLIDUS, DIGIT_ZERO,
DIGIT_NINE, SEPARATOR, LATIN_UPPER_A, LATIN_UPPER_E, LATIN_UPPER_F,
ARRAY_BEGIN, BACKSLASH, ARRAY_END, LATIN_A, LATIN_B, LATIN_E, LATIN_F,
LATIN_L, LATIN_N, LATIN_R, LATIN_S, LATIN_T, LATIN_U, OBJECT_BEGIN,
OBJECT_END, ESCAPES, REVERSE_ESCAPES, ESCAPED_ARRAY
3 changes: 3 additions & 0 deletions src/errors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ const E_BAD_ESCAPE = "Invalid escape sequence"
const E_BAD_CONTROL = "ASCII control character in string"
const E_LEADING_ZERO = "Invalid leading zero in number"
const E_BAD_NUMBER = "Invalid number"

export E_EXPECTED_EOF, E_UNEXPECTED_EOF, E_UNEXPECTED_CHAR, E_BAD_KEY,
E_BAD_ESCAPE, E_BAD_CONTROL, E_LEADING_ZERO, E_BAD_NUMBER

0 comments on commit e24fc41

Please sign in to comment.