just use Vector{UInt8} instead of raw pointer loads, since it's not clear we care about performance here

This commit is contained in:
Steven G. Johnson 2017-08-03 15:55:09 -04:00
parent 26bfe8d705
commit 93ecaf5ccf
1 changed files with 21 additions and 20 deletions

View File

@ -30,14 +30,14 @@ from the `arg`.
function search_latexdirective(s::Union{String,SubString{String}}, istart::Int=1)
e = sizeof(s)
0 < istart e || return 0,0,0
p = pointer(s)
p = Vector{UInt8}(s)
i = istart
allspaces=true
# find \foo directive or {...}:
c = UInt8(0)
while i e
c = unsafe_load(p, i)
c = p[i]
(c == BACKSLASH || c == BRACEOPEN || c == CARET || c == UNDERSCORE) && break
c != SPACE && (allspaces = false)
i += 1
@ -47,8 +47,8 @@ function search_latexdirective(s::Union{String,SubString{String}}, istart::Int=1
if c == BACKSLASH
i += 2
i-1 > e && return 0,0,0
if isalpha8(unsafe_load(p, i-1))
while i e && isalpha8(unsafe_load(p, i))
if isalpha8(p[i-1])
while i e && isalpha8(p[i])
i += 1
end
end
@ -59,19 +59,19 @@ function search_latexdirective(s::Union{String,SubString{String}}, istart::Int=1
end
# look for optional opening brace
while i e && unsafe_load(p, i) == SPACE
while i e && p[i] == SPACE
i += 1
end
i > e && return directive_start, directive_end, e
inbrace = unsafe_load(p, i) == BRACEOPEN
inbrace = p[i] == BRACEOPEN
if !inbrace
# search backwards from \foo to look for { \foo ...}
j = directive_start - 1
while j istart && unsafe_load(p, j) == SPACE
while j istart && p[j] == SPACE
j -= 1
end
if j < istart || unsafe_load(p, j) != BRACEOPEN
if unsafe_load(p, i) == BACKSLASH
if j < istart || p[j] != BRACEOPEN
if p[i] == BACKSLASH
# argument is another latex directive
ds,de,ae = search_latexdirective(s, i)
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
else
# argument is not in braces … get next token
while i e && isalnum8(unsafe_load(p, i))
while i e && isalnum8(p[i])
i += 1
end
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)
nbraces = 1
while i e
c = unsafe_load(p, i)
c = p[i]
if c == BRACEOPEN
nbraces += 1
elseif c == BRACECLOSE
@ -127,22 +127,23 @@ Return the substring of `s` corresponding to the argument from `argstart:argend`
leading/trailing whitespace and braces.
"""
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)
(1 argstart e && 1 argend e) || throw(BoundsError())
p = pointer(s)
if unsafe_load(p, argend) == BRACECLOSE
p = Vector{UInt8}(s)
if p[argend] == BRACECLOSE
argend -= 1 # omit brace
while argstart argend && unsafe_load(p, argstart) != BRACEOPEN
while argstart argend && p[argstart] != BRACEOPEN
argstart += 1
end
argstart > argend && error("malformed argument")
argstart += 1 # omit brace
end
while argstart argend && unsafe_load(p, argend) == SPACE
while argstart argend && p[argend] == SPACE
argend -= 1
end
while argstart argend && unsafe_load(p, argstart) == SPACE
while argstart argend && p[argstart] == SPACE
argstart += 1
end
return SubString(s, argstart, argend)
@ -239,7 +240,7 @@ const superscripts = Dict{Char,Char}(
'A'=>'ᴬ', 'B'=>'ᴮ', 'C'=>'ᶜ', 'D'=>'ᴰ', 'E'=>'ᴱ', 'G'=>'ᴳ', 'H'=>'ᴴ', 'I'=>'ᴵ', 'J'=>'ᴶ',
'K'=>'ᴷ', 'L'=>'ᴸ', 'M'=>'ᴹ', 'N'=>'ᴺ', 'O'=>'ᴼ', 'P'=>'ᴾ', 'R'=>'ᴿ', 'S'=>'ˢ', 'T'=>'ᵀ',
'U'=>'ᵁ', 'V'=>'ⱽ', 'W'=>'ᵂ', 'β'=>'ᵝ', 'γ'=>'ᵞ', 'δ'=>'ᵟ', 'ψ'=>'ᵠ', 'χ'=>'ᵡ', 'Θ'=>'ᶿ',
'+'=>'⁺', '-'=>'⁻', '='=>'⁼', '('=>'⁽', ')'=>'⁾', ' '=>' ',
'+'=>'⁺', '-'=>'⁻', '='=>'⁼', '('=>'⁽', ')'=>'⁾', ' '=>' ', '∘'=>'°',
)
const subscripts = Dict{Char,Char}(
'0'=>'₀', '1'=>'₁', '2'=>'₂', '3'=>'₃', '4'=>'₄', '5'=>'₅', '6'=>'₆', '7'=>'₇', '8'=>'₈', '9'=>'₉',
@ -296,10 +297,10 @@ replace_directives(s::AbstractString, extra_directives::Associative{String,Strin
# strip unescaped $ signs from s
function strip_dollars(s::Union{String,SubString{String}})
buf = IOBuffer()
p = pointer(s)
p = Vector{UInt8}(s)
for i = 1:sizeof(s)
c = unsafe_load(p, i)
if c == BACKSLASH && i < sizeof(s) && unsafe_load(p, i+1) == DOLLAR
c = p[i]
if c == BACKSLASH && i < sizeof(s) && p[i+1] == DOLLAR
write(buf, DOLLAR) # \$ -> $
elseif c != DOLLAR
write(buf, c)