just use Vector{UInt8} instead of raw pointer loads, since it's not clear we care about performance here
This commit is contained in:
parent
26bfe8d705
commit
93ecaf5ccf
41
src/latex.jl
41
src/latex.jl
|
@ -30,14 +30,14 @@ from the `arg`.
|
||||||
function search_latexdirective(s::Union{String,SubString{String}}, istart::Int=1)
|
function search_latexdirective(s::Union{String,SubString{String}}, istart::Int=1)
|
||||||
e = sizeof(s)
|
e = sizeof(s)
|
||||||
0 < istart ≤ e || return 0,0,0
|
0 < istart ≤ e || return 0,0,0
|
||||||
p = pointer(s)
|
p = Vector{UInt8}(s)
|
||||||
i = istart
|
i = istart
|
||||||
allspaces=true
|
allspaces=true
|
||||||
|
|
||||||
# find \foo directive or {...}:
|
# find \foo directive or {...}:
|
||||||
c = UInt8(0)
|
c = UInt8(0)
|
||||||
while i ≤ e
|
while i ≤ e
|
||||||
c = unsafe_load(p, i)
|
c = p[i]
|
||||||
(c == BACKSLASH || c == BRACEOPEN || c == CARET || c == UNDERSCORE) && break
|
(c == BACKSLASH || c == BRACEOPEN || c == CARET || c == UNDERSCORE) && break
|
||||||
c != SPACE && (allspaces = false)
|
c != SPACE && (allspaces = false)
|
||||||
i += 1
|
i += 1
|
||||||
|
@ -47,8 +47,8 @@ function search_latexdirective(s::Union{String,SubString{String}}, istart::Int=1
|
||||||
if c == BACKSLASH
|
if c == BACKSLASH
|
||||||
i += 2
|
i += 2
|
||||||
i-1 > e && return 0,0,0
|
i-1 > e && return 0,0,0
|
||||||
if isalpha8(unsafe_load(p, i-1))
|
if isalpha8(p[i-1])
|
||||||
while i ≤ e && isalpha8(unsafe_load(p, i))
|
while i ≤ e && isalpha8(p[i])
|
||||||
i += 1
|
i += 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -59,19 +59,19 @@ function search_latexdirective(s::Union{String,SubString{String}}, istart::Int=1
|
||||||
end
|
end
|
||||||
|
|
||||||
# look for optional opening brace
|
# look for optional opening brace
|
||||||
while i ≤ e && unsafe_load(p, i) == SPACE
|
while i ≤ e && p[i] == SPACE
|
||||||
i += 1
|
i += 1
|
||||||
end
|
end
|
||||||
i > e && return directive_start, directive_end, e
|
i > e && return directive_start, directive_end, e
|
||||||
inbrace = unsafe_load(p, i) == BRACEOPEN
|
inbrace = p[i] == BRACEOPEN
|
||||||
if !inbrace
|
if !inbrace
|
||||||
# search backwards from \foo to look for { \foo ...}
|
# search backwards from \foo to look for { \foo ...}
|
||||||
j = directive_start - 1
|
j = directive_start - 1
|
||||||
while j ≥ istart && unsafe_load(p, j) == SPACE
|
while j ≥ istart && p[j] == SPACE
|
||||||
j -= 1
|
j -= 1
|
||||||
end
|
end
|
||||||
if j < istart || unsafe_load(p, j) != BRACEOPEN
|
if j < istart || p[j] != BRACEOPEN
|
||||||
if unsafe_load(p, i) == BACKSLASH
|
if p[i] == BACKSLASH
|
||||||
# argument is another latex directive
|
# argument is another latex directive
|
||||||
ds,de,ae = search_latexdirective(s, i)
|
ds,de,ae = search_latexdirective(s, i)
|
||||||
return directive_start, directive_end, ae
|
return directive_start, directive_end, ae
|
||||||
|
@ -86,7 +86,7 @@ function search_latexdirective(s::Union{String,SubString{String}}, istart::Int=1
|
||||||
return directive_start, directive_end, e
|
return directive_start, directive_end, e
|
||||||
else
|
else
|
||||||
# argument is not in braces … get next token
|
# argument is not in braces … get next token
|
||||||
while i ≤ e && isalnum8(unsafe_load(p, i))
|
while i ≤ e && isalnum8(p[i])
|
||||||
i += 1
|
i += 1
|
||||||
end
|
end
|
||||||
return directive_start, directive_end, i-1
|
return directive_start, directive_end, i-1
|
||||||
|
@ -106,7 +106,7 @@ function search_latexdirective(s::Union{String,SubString{String}}, istart::Int=1
|
||||||
# search for end of argument (closing brace)
|
# search for end of argument (closing brace)
|
||||||
nbraces = 1
|
nbraces = 1
|
||||||
while i ≤ e
|
while i ≤ e
|
||||||
c = unsafe_load(p, i)
|
c = p[i]
|
||||||
if c == BRACEOPEN
|
if c == BRACEOPEN
|
||||||
nbraces += 1
|
nbraces += 1
|
||||||
elseif c == BRACECLOSE
|
elseif c == BRACECLOSE
|
||||||
|
@ -127,22 +127,23 @@ Return the substring of `s` corresponding to the argument from `argstart:argend`
|
||||||
leading/trailing whitespace and braces.
|
leading/trailing whitespace and braces.
|
||||||
"""
|
"""
|
||||||
function striparg(s::Union{String,SubString{String}}, argstart::Int=start(s), argend::Int=endof(s))
|
function striparg(s::Union{String,SubString{String}}, argstart::Int=start(s), argend::Int=endof(s))
|
||||||
|
argstart > argend && return SubString(s, 1, 0)
|
||||||
e = endof(s)
|
e = endof(s)
|
||||||
(1 ≤ argstart ≤ e && 1 ≤ argend ≤ e) || throw(BoundsError())
|
(1 ≤ argstart ≤ e && 1 ≤ argend ≤ e) || throw(BoundsError())
|
||||||
|
|
||||||
p = pointer(s)
|
p = Vector{UInt8}(s)
|
||||||
if unsafe_load(p, argend) == BRACECLOSE
|
if p[argend] == BRACECLOSE
|
||||||
argend -= 1 # omit brace
|
argend -= 1 # omit brace
|
||||||
while argstart ≤ argend && unsafe_load(p, argstart) != BRACEOPEN
|
while argstart ≤ argend && p[argstart] != BRACEOPEN
|
||||||
argstart += 1
|
argstart += 1
|
||||||
end
|
end
|
||||||
argstart > argend && error("malformed argument")
|
argstart > argend && error("malformed argument")
|
||||||
argstart += 1 # omit brace
|
argstart += 1 # omit brace
|
||||||
end
|
end
|
||||||
while argstart ≤ argend && unsafe_load(p, argend) == SPACE
|
while argstart ≤ argend && p[argend] == SPACE
|
||||||
argend -= 1
|
argend -= 1
|
||||||
end
|
end
|
||||||
while argstart ≤ argend && unsafe_load(p, argstart) == SPACE
|
while argstart ≤ argend && p[argstart] == SPACE
|
||||||
argstart += 1
|
argstart += 1
|
||||||
end
|
end
|
||||||
return SubString(s, argstart, argend)
|
return SubString(s, argstart, argend)
|
||||||
|
@ -239,7 +240,7 @@ const superscripts = Dict{Char,Char}(
|
||||||
'A'=>'ᴬ', 'B'=>'ᴮ', 'C'=>'ᶜ', 'D'=>'ᴰ', 'E'=>'ᴱ', 'G'=>'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', '9'=>'₉',
|
||||||
|
@ -296,10 +297,10 @@ replace_directives(s::AbstractString, extra_directives::Associative{String,Strin
|
||||||
# strip unescaped $ signs from s
|
# strip unescaped $ signs from s
|
||||||
function strip_dollars(s::Union{String,SubString{String}})
|
function strip_dollars(s::Union{String,SubString{String}})
|
||||||
buf = IOBuffer()
|
buf = IOBuffer()
|
||||||
p = pointer(s)
|
p = Vector{UInt8}(s)
|
||||||
for i = 1:sizeof(s)
|
for i = 1:sizeof(s)
|
||||||
c = unsafe_load(p, i)
|
c = p[i]
|
||||||
if c == BACKSLASH && i < sizeof(s) && unsafe_load(p, i+1) == DOLLAR
|
if c == BACKSLASH && i < sizeof(s) && p[i+1] == DOLLAR
|
||||||
write(buf, DOLLAR) # \$ -> $
|
write(buf, DOLLAR) # \$ -> $
|
||||||
elseif c != DOLLAR
|
elseif c != DOLLAR
|
||||||
write(buf, c)
|
write(buf, c)
|
||||||
|
|
Loading…
Reference in New Issue