Skip to content

Commit aca1976

Browse files
authored
more updates for 0.7 (#20)
* more updates for 0.7 * fix codeunits for 0.6 * fix method ambiguities
1 parent eab80a3 commit aca1976

File tree

3 files changed

+72
-47
lines changed

3 files changed

+72
-47
lines changed

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
julia 0.6
2-
Compat 0.8.0
2+
Compat 0.62.0

src/LaTeXStrings.jl

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,87 @@
11
__precompile__()
22

3+
"""
4+
The LaTeXStrings module exists mainly to make LaTeX equations easier to type as
5+
literal strings, and so that the resulting strings display as formatted equations
6+
in supporting environments like IJulia.
7+
8+
See in particular the `LaTeXString` type and the `L"..."` constructor macro.
9+
"""
310
module LaTeXStrings
411
export LaTeXString, latexstring, @L_str, @L_mstr
5-
612
using Compat
7-
import Compat.String
813

914
# IJulia supports LaTeX output for any object with a text/latex
1015
# writemime method, but these are annoying to type as string literals
1116
# in Julia because of all the escaping required, e.g. "\$\\alpha +
1217
# \\beta\$". To simplify this, we add a new string type with a macro
1318
# constructor, so that one can simply do L"$\alpha + \beta$".
1419

20+
@doc raw"""
21+
A `LaTeXString` is a string type whose contents represent a fragment of LaTeX code,
22+
typically containing an equation (`$...$`). In certain environments (e.g. IJulia)
23+
this will display with LaTeX-like formatting. For the most part, you can use
24+
a `LaTeXString` object in any context that expects an `AbstractString` object.
25+
26+
The `L"..."` macro is convenient for constructing `LaTeXString` objects, because
27+
it eliminates the need to escape backslashes and dollar signs, and implicitly inserts
28+
dollar signs around the string if none are present. For example, `L"$\alpha$"`, `L"\alpha"`,
29+
and `LaTeXString("\$\\alpha\$")` are all equivalent.
30+
"""
1531
struct LaTeXString <: AbstractString
1632
s::String
1733
end
1834

19-
# coercing constructor:
35+
"""
36+
latexstring(args...)
37+
38+
Similar to `string(args...)`, but generates a `LaTeXString` instead of a `String`.
39+
"""
40+
latexstring(args...) = latexstring(string(args...))
2041
function latexstring(s::String)
2142
# the only point of using LaTeXString to represent equations, since
2243
# IJulia doesn't support LaTeX output other than equations, so add $'s
2344
# around the string if they aren't there (ignoring \$)
24-
return ismatch(r"[^\\]\$|^\$", s) ?
25-
LaTeXString(s) : LaTeXString(string("\$", s, "\$"))
45+
return occursin(r"[^\\]\$|^\$", s) ? LaTeXString(s) : LaTeXString(string('\$', s, '\$'))
2646
end
2747
latexstring(s::AbstractString) = latexstring(String(s))
28-
latexstring(args...) = latexstring(string(args...))
2948

3049
macro L_str(s, flags...) latexstring(s) end
3150
macro L_mstr(s, flags...) latexstring(s) end
3251

33-
import Base: write, endof, getindex, sizeof, search, rsearch, isvalid, next, length, IOBuffer, pointer
34-
@compat import Base.show
35-
36-
write(io::IO, s::LaTeXString) = write(io, s.s)
37-
@compat show(io::IO, ::MIME"application/x-latex", s::LaTeXString) = write(io, s)
38-
@compat show(io::IO, ::MIME"text/latex", s::LaTeXString) = write(io, s)
39-
40-
function show(io::IO, s::LaTeXString)
52+
Base.write(io::IO, s::LaTeXString) = write(io, s.s)
53+
Base.show(io::IO, ::MIME"application/x-latex", s::LaTeXString) = write(io, s)
54+
Base.show(io::IO, ::MIME"text/latex", s::LaTeXString) = write(io, s)
55+
function Base.show(io::IO, s::LaTeXString)
4156
print(io, "L")
4257
Base.print_quoted_literal(io, s.s)
4358
end
4459

45-
if isdefined(Base, :bytestring)
46-
import Base: bytestring
47-
bytestring(s::LaTeXString) = bytestring(s.s)
60+
Compat.firstindex(s::LaTeXString) = Compat.firstindex(s.s)
61+
Compat.lastindex(s::LaTeXString) = Compat.lastindex(s.s)
62+
Base.start(s::LaTeXString) = start(s.s)
63+
Base.next(s::LaTeXString, i) = next(s.s, i)
64+
Base.done(s::LaTeXString, i) = done(s.s, i)
65+
if isdefined(Base, :iterate)
66+
Base.iterate(s::LaTeXString, i::Int) = iterate(s.s, i)
4867
end
49-
50-
endof(s::LaTeXString) = endof(s.s)
51-
next(s::LaTeXString, i::Int) = next(s.s, i)
52-
length(s::LaTeXString) = length(s.s)
53-
getindex(s::LaTeXString, i::Int) = getindex(s.s, i)
54-
getindex(s::LaTeXString, i::Integer) = getindex(s.s, i)
55-
getindex(s::LaTeXString, i::Real) = getindex(s.s, i)
56-
getindex(s::LaTeXString, i::UnitRange{Int}) = getindex(s.s, i)
57-
getindex{T<:Integer}(s::LaTeXString, i::UnitRange{T}) = getindex(s.s, i)
58-
getindex(s::LaTeXString, i::AbstractVector) = getindex(s.s, i)
59-
sizeof(s::LaTeXString) = sizeof(s.s)
60-
search(s::LaTeXString, c::Char, i::Integer) = search(s.s, c, i)
61-
rsearch(s::LaTeXString, c::Char, i::Integer) = rsearch(s.s, c, i)
62-
isvalid(s::LaTeXString, i::Integer) = isvalid(s.s, i)
63-
pointer(s::LaTeXString) = pointer(s.s)
64-
IOBuffer(s::LaTeXString) = IOBuffer(s.s)
65-
66-
import Base.convert
67-
const unsafe_convert = Base.convert
68-
69-
@compat unsafe_convert(T::Union{Type{Ptr{UInt8}},Type{Ptr{Int8}}}, s::LaTeXString) = convert(T, s.s)
70-
unsafe_convert(::Type{Cstring}, s::LaTeXString) = unsafe_convert(Cstring, s.s)
68+
Base.nextind(s::LaTeXString, i::Int) = nextind(s.s, i)
69+
Base.prevind(s::LaTeXString, i::Int) = prevind(s.s, i)
70+
Base.eachindex(s::LaTeXString) = eachindex(s.s)
71+
Base.length(s::LaTeXString) = length(s.s)
72+
Base.getindex(s::LaTeXString, i::Integer) = getindex(s.s, i)
73+
Base.getindex(s::LaTeXString, i::Int) = getindex(s.s, i) # for method ambig in Julia 0.6
74+
Base.getindex(s::LaTeXString, i::UnitRange{Int}) = getindex(s.s, i)
75+
Base.getindex(s::LaTeXString, i::UnitRange{<:Integer}) = getindex(s.s, i)
76+
Base.getindex(s::LaTeXString, i::AbstractVector{<:Integer}) = getindex(s.s, i)
77+
Compat.codeunit(s::LaTeXString, i::Integer) = codeunit(s.s, i)
78+
Compat.codeunit(s::LaTeXString) = codeunit(s.s)
79+
Compat.ncodeunits(s::LaTeXString) = ncodeunits(s.s)
80+
Compat.codeunits(s::LaTeXString) = codeunits(s.s)
81+
Base.sizeof(s::LaTeXString) = sizeof(s.s)
82+
Base.isvalid(s::LaTeXString, i::Integer) = isvalid(s.s, i)
83+
Base.pointer(s::LaTeXString) = pointer(s.s)
84+
Base.IOBuffer(s::LaTeXString) = IOBuffer(s.s)
85+
Base.unsafe_convert(T::Union{Type{Ptr{UInt8}},Type{Ptr{Int8}},Cstring}, s::LaTeXString) = Base.unsafe_convert(T, s.s)
7186

7287
end # module

test/runtests.jl

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
using LaTeXStrings, Compat
2-
using Base.Test
2+
using Compat.Test
33

4-
# write your own tests here
54
tst1 = L"an equation: $\alpha^2$"
6-
tst1u = Compat.UTF8String(tst1)
7-
@test tst1 == "an equation: \$\\alpha^2\$" == tst1u
5+
tst1s = String(tst1)
6+
@test tst1 == "an equation: \$\\alpha^2\$" == tst1s
87
@test L"\alpha^2" == "\$\\alpha^2\$"
9-
@test stringmime("text/latex", tst1) == tst1u
10-
@test sprint(show, "text/latex", tst1) == tst1u
11-
@test latexstring(tst1u) == tst1 == LaTeXString(tst1u)
8+
@test repr("text/latex", tst1) == tst1s
9+
@test sprint(show, "text/latex", tst1) == tst1s
10+
@test latexstring(tst1s) == tst1 == LaTeXString(tst1s)
1211

1312
@test ccall(:strlen, Csize_t, (Cstring,), tst1) == ccall(:strlen, Csize_t, (Ptr{UInt8},), tst1) == sizeof(tst1)
13+
14+
for f in (length, eachindex, pointer, collect, ncodeunits, codeunits)
15+
@test f(tst1) == f(tst1.s)
16+
end
17+
@test nextind(tst1, 1) == prevind(tst1, 3) == 2
18+
@test isvalid(tst1, 1)
19+
@test findnext(isequal('o'), L"foo", 1) == 3
20+
@test tst1[1] == 'a'
21+
@test codeunit(tst1, 1) == UInt8('a')
22+
@test L"foo"[1] == '$'
23+
@test tst1[1:2] == tst1[0x01:0x02] == tst1[[1,2]] == "an"

0 commit comments

Comments
 (0)