Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix errors on julia 0.7, drop 0.5 support #29

Merged
merged 1 commit into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ os:
- linux
- osx
julia:
- 0.5
- 0.6
- 0.7
- nightly
notifications:
email: false
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

[![Julia 0.5 Status](http://pkg.julialang.org/badges/LegacyStrings_0.5.svg)](http://pkg.julialang.org/?pkg=LegacyStrings&ver=0.5)
[![Julia 0.6 Status](http://pkg.julialang.org/badges/LegacyStrings_0.6.svg)](http://pkg.julialang.org/?pkg=LegacyStrings&ver=0.6)
[![Julia 0.7 Status](http://pkg.julialang.org/badges/LegacyStrings_0.7.svg)](http://pkg.julialang.org/?pkg=LegacyStrings&ver=0.7)

The LegacyStrings package provides compatibility string types from Julia 0.5 (and earlier), which were removed in subsequent versions, including:

Expand Down
4 changes: 2 additions & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
julia 0.5
Compat 0.18.0
julia 0.6
Compat 0.67
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
environment:
matrix:
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.5/julia-0.5-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.5/julia-0.5-latest-win64.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.7/julia-0.7-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.7/julia-0.7-latest-win64.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"

Expand Down
123 changes: 68 additions & 55 deletions src/LegacyStrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ export
import Base:
containsnul,
convert,
endof,
getindex,
isvalid,
lcfirst,
length,
lowercase,
map,
next,
nextind,
pointer,
prevind,
reverse,
reverseind,
rsearch,
Expand All @@ -45,70 +46,82 @@ import Base:
write

using Compat
using Compat: IOBuffer
import Compat:
lastindex,
codeunit,
ncodeunits

if isdefined(Base, :lastidx)
import Base: lastidx
end
if isdefined(Base, :iterate)
import Base: iterate
end

if isdefined(Base, :DirectIndexString)
using Base: DirectIndexString
else
include("directindex.jl")
end
if isdefined(Base, :UnicodeError)
import Base: UnicodeError
else
include("unicodeerror.jl")
end

if VERSION >= v"0.5.0-"
immutable ASCIIString <: DirectIndexString
data::Vector{UInt8}
ASCIIString(data::String) = new(Vector{UInt8}(data))
ASCIIString(data) = new(data)
end
if isdefined(Base, :DirectIndexString)
using Base: DirectIndexString
else
include("directindex.jl")
end

immutable UTF8String <: AbstractString
data::Vector{UInt8}
UTF8String(data::String) = new(Vector{UInt8}(data))
UTF8String(data) = new(data)
end
struct ASCIIString <: DirectIndexString
data::Vector{UInt8}
ASCIIString(data::String) = new(Vector{UInt8}(codeunits(data)))
ASCIIString(data) = new(data)
end

immutable UTF16String <: AbstractString
data::Vector{UInt16} # includes 16-bit NULL termination after string chars
function UTF16String(data::Vector{UInt16})
if length(data) < 1 || data[end] != 0
throw(UnicodeError(UTF_ERR_NULL_16_TERMINATE, 0, 0))
end
new(data)
end
struct UTF8String <: AbstractString
data::Vector{UInt8}
UTF8String(data::String) = new(Vector{UInt8}(codeunits(data)))
UTF8String(data) = new(data)
end

struct UTF16String <: AbstractString
data::Vector{UInt16} # includes 16-bit NULL termination after string chars
function UTF16String(data::Vector{UInt16})
if length(data) < 1 || data[end] != 0
throw(UnicodeError(UTF_ERR_NULL_16_TERMINATE, 0, 0))
end
new(data)
end
end

immutable UTF32String <: DirectIndexString
data::Vector{UInt32} # includes 32-bit NULL termination after string chars
function UTF32String(data::Vector{UInt32})
if length(data) < 1 || data[end] != 0
throw(UnicodeError(UTF_ERR_NULL_32_TERMINATE, 0, 0))
end
new(data)
end
struct UTF32String <: DirectIndexString
data::Vector{UInt32} # includes 32-bit NULL termination after string chars
function UTF32String(data::Vector{UInt32})
if length(data) < 1 || data[end] != 0
throw(UnicodeError(UTF_ERR_NULL_32_TERMINATE, 0, 0))
end
new(data)
end
end

const ByteString = Union{ASCIIString,UTF8String}
const ByteString = Union{ASCIIString,UTF8String}

include("support.jl")
include("ascii.jl")
include("utf8.jl")
include("utf16.jl")
include("utf32.jl")
else
using Base: UTF_ERR_SHORT, checkstring
end
include("support.jl")
include("ascii.jl")
include("utf8.jl")
include("utf16.jl")
include("utf32.jl")
include("rep.jl")

if isdefined(Base, :RepString)
using Base: RepString
else
include("rep.jl")
end
if isdefined(Base, :RevString)
using Base: RevString
else
include("rev.jl")
end

const AllLegacyStringTypes = Union{ASCIIString,UTF8String,UTF16String,UTF32String,RepString,RevString}

codeunit(s::SubString{<:AllLegacyStringTypes}) = codeunit(s.string)
ncodeunits(s::SubString{<:AllLegacyStringTypes}) = isdefined(s, :ncodeunits) ? s.ncodeunits : s.endof

if !isdefined(Base, :iterate)
iterate(s::Union{String,SubString,AllLegacyStringTypes}, i::Int) = next(s, i)
end

if isdefined(Base, :RevString)
using Base: RevString
else
include("rev.jl")
end
end # module
20 changes: 17 additions & 3 deletions src/ascii.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@

## required core functionality ##

endof(s::ASCIIString) = length(s.data)
lastindex(s::ASCIIString) = length(s.data)
getindex(s::ASCIIString, i::Int) = (x=s.data[i]; ifelse(x < 0x80, Char(x), '\ufffd'))

codeunit(s::ASCIIString) = UInt8
ncodeunits(s::ASCIIString) = length(s.data)

if isdefined(Base, :iterate)
import Base: iterate
function iterate(s::ASCIIString, i::Int = firstindex(s))
i > ncodeunits(s) && return nothing
return next(s, i)
end
end

## overload methods for efficiency ##

bytestring(s::ASCIIString) = s
Expand All @@ -29,7 +40,7 @@ function string(c::ASCIIString...)
for s in c
n += length(s.data)
end
v = Vector{UInt8}(n)
v = Vector{UInt8}(undef, n)
o = 1
for s in c
ls = length(s.data)
Expand Down Expand Up @@ -97,12 +108,15 @@ write(io::IO, s::ASCIIString) = write(io, s.data)

ascii(x) = convert(ASCIIString, x)
convert(::Type{ASCIIString}, s::ASCIIString) = s
convert(::Type{ASCIIString}, s::String) = ascii(Vector{UInt8}(s))
convert(::Type{ASCIIString}, s::String) = ascii(codeunits(s))
convert(::Type{ASCIIString}, s::UTF8String) = ascii(s.data)
convert(::Type{ASCIIString}, a::Vector{UInt8}) = begin
isvalid(ASCIIString,a) || throw(ArgumentError("invalid ASCII sequence"))
return ASCIIString(a)
end
if isdefined(Base, :codeunits)
convert(::Type{ASCIIString}, a::Base.CodeUnits{UInt8,String}) = convert(ASCIIString, Vector{UInt8}(a))
end

ascii(p::Ptr{UInt8}) =
ascii(p, p == C_NULL ? Csize_t(0) : ccall(:strlen, Csize_t, (Ptr{UInt8},), p))
Expand Down
12 changes: 7 additions & 5 deletions src/directindex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ next(s::DirectIndexString, i::Int) = (s[i],i+1)

length(s::DirectIndexString) = endof(s)

isvalid(s::DirectIndexString, i::Integer) = (start(s) <= i <= endof(s))
isvalid(s::DirectIndexString, i::Integer) = (firstindex(s) <= i <= lastindex(s))

prevind(s::DirectIndexString, i::Integer) = Int(i)-1
nextind(s::DirectIndexString, i::Integer) = Int(i)+1
prevind(s::DirectIndexString, i::Int) = i-1
nextind(s::DirectIndexString, i::Int) = i+1
prevind(s::DirectIndexString, i::Integer) = prevind(s, i)
nextind(s::DirectIndexString, i::Integer) = nextind(s, i)

function prevind(s::DirectIndexString, i::Integer, nchar::Integer)
nchar > 0 || throw(ArgumentError("nchar must be greater than 0"))
Expand All @@ -24,9 +26,9 @@ end
ind2chr(s::DirectIndexString, i::Integer) = begin checkbounds(s,i); i end
chr2ind(s::DirectIndexString, i::Integer) = begin checkbounds(s,i); i end

length(s::SubString{<:DirectIndexString}) = endof(s)
length(s::SubString{<:DirectIndexString}) = lastindex(s)

isvalid(s::SubString{<:DirectIndexString}, i::Integer) = (start(s) <= i <= endof(s))
isvalid(s::SubString{<:DirectIndexString}, i::Integer) = (firstindex(s) <= i <= ncodeunits(s))

ind2chr(s::SubString{<:DirectIndexString}, i::Integer) = begin checkbounds(s,i); i end
chr2ind(s::SubString{<:DirectIndexString}, i::Integer) = begin checkbounds(s,i); i end
Expand Down
33 changes: 26 additions & 7 deletions src/rep.jl
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
# This file includes code that was formerly a part of Julia. License is MIT: http://julialang.org/license

immutable RepString <: AbstractString
struct RepString <: AbstractString
string::AbstractString
repeat::Integer
end

function endof(s::RepString)
e = endof(s.string)
(next(s.string,e)[2]-1) * (s.repeat-1) + e
function lastindex(s::RepString)
e = lastindex(s.string)
(iterate(s.string,e)[2]-1) * (s.repeat-1) + e
end
length(s::RepString) = length(s.string)*s.repeat
sizeof(s::RepString) = sizeof(s.string)*s.repeat

function isvalid(s::RepString, i::Int)
1 ≤ i ≤ ncodeunits(s) || return false
j = 1
while j < i
_, j = iterate(s, j)
end
return j == i
end

function next(s::RepString, i::Int)
if i < 1
throw(BoundsError(s, i))
end
e = endof(s.string)
sz = next(s.string,e)[2]-1
e = lastindex(s.string)
sz = iterate(s.string,e)[2]-1

r, j = divrem(i-1, sz)
j += 1
Expand All @@ -26,8 +35,18 @@ function next(s::RepString, i::Int)
throw(BoundsError(s, i))
end

c, k = next(s.string, j)
c, k = iterate(s.string, j)
c, k-j+i
end

codeunit(s::RepString) = codeunit(s.string)
ncodeunits(s::RepString) = ncodeunits(s.string) * s.repeat

if isdefined(Base, :iterate)
function iterate(s::RepString, i::Int = firstindex(s))
i > ncodeunits(s) && return nothing
return next(s, i)
end
end

convert(::Type{RepString}, s::AbstractString) = RepString(s,1)
27 changes: 23 additions & 4 deletions src/rev.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,37 @@

## reversed strings without data movement ##

immutable RevString{T<:AbstractString} <: AbstractString
struct RevString{T<:AbstractString} <: AbstractString
string::T
end

endof(s::RevString) = endof(s.string)
lastindex(s::RevString) = lastindex(s.string)
length(s::RevString) = length(s.string)
sizeof(s::RevString) = sizeof(s.string)

function next(s::RevString, i::Int)
n = endof(s); j = n-i+1
n = lastindex(s); j = n-i+1
(s.string[j], n-prevind(s.string,j)+1)
end

codeunit(s::RevString) = codeunit(s.string)
ncodeunits(s::RevString) = ncodeunits(s.string)

if isdefined(Base, :iterate)
function iterate(s::RevString, i::Int = firstindex(s))
i > lastindex(s) && return nothing
return next(s, i)
end
end

function isvalid(s::RevString, i::Int)
1 ≤ i ≤ ncodeunits(s) || return false
j = 1
while j < i
_, j = iterate(s, j)
end
return j == i
end

reverse(s::RevString) = s.string
reverseind(s::RevString, i::Integer) = endof(s) - i + 1
reverseind(s::RevString, i::Integer) = lastindex(s) - i + 1
Loading