Skip to content

Commit 8ca9d0e

Browse files
committed
Fix errors on julia 0.7, drop 0.5 support
all tests pass without deprecation warnings; some deprecation warnings still remain though
1 parent b4f7df5 commit 8ca9d0e

File tree

15 files changed

+304
-174
lines changed

15 files changed

+304
-174
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ os:
44
- linux
55
- osx
66
julia:
7-
- 0.5
87
- 0.6
8+
- 0.7
99
- nightly
1010
notifications:
1111
email: false

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

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

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

REQUIRE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
julia 0.5
2-
Compat 0.18.0
1+
julia 0.6
2+
Compat 0.67

appveyor.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
environment:
22
matrix:
3-
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.5/julia-0.5-latest-win32.exe"
4-
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.5/julia-0.5-latest-win64.exe"
53
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
64
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
5+
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.7/julia-0.7-latest-win32.exe"
6+
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.7/julia-0.7-latest-win64.exe"
77
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
88
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"
99

src/LegacyStrings.jl

Lines changed: 68 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ export
2323
import Base:
2424
containsnul,
2525
convert,
26-
endof,
2726
getindex,
2827
isvalid,
2928
lcfirst,
3029
length,
3130
lowercase,
3231
map,
3332
next,
33+
nextind,
3434
pointer,
35+
prevind,
3536
reverse,
3637
reverseind,
3738
rsearch,
@@ -45,70 +46,82 @@ import Base:
4546
write
4647

4748
using Compat
49+
using Compat: IOBuffer
50+
import Compat:
51+
lastindex,
52+
codeunit,
53+
ncodeunits
4854

49-
if isdefined(Base, :lastidx)
50-
import Base: lastidx
51-
end
55+
if isdefined(Base, :iterate)
56+
import Base: iterate
57+
end
5258

53-
if isdefined(Base, :DirectIndexString)
54-
using Base: DirectIndexString
55-
else
56-
include("directindex.jl")
57-
end
59+
if isdefined(Base, :UnicodeError)
60+
import Base: UnicodeError
61+
else
62+
include("unicodeerror.jl")
63+
end
5864

59-
if VERSION >= v"0.5.0-"
60-
immutable ASCIIString <: DirectIndexString
61-
data::Vector{UInt8}
62-
ASCIIString(data::String) = new(Vector{UInt8}(data))
63-
ASCIIString(data) = new(data)
64-
end
65+
if isdefined(Base, :DirectIndexString)
66+
using Base: DirectIndexString
67+
else
68+
include("directindex.jl")
69+
end
6570

66-
immutable UTF8String <: AbstractString
67-
data::Vector{UInt8}
68-
UTF8String(data::String) = new(Vector{UInt8}(data))
69-
UTF8String(data) = new(data)
70-
end
71+
struct ASCIIString <: DirectIndexString
72+
data::Vector{UInt8}
73+
ASCIIString(data::String) = new(Vector{UInt8}(codeunits(data)))
74+
ASCIIString(data) = new(data)
75+
end
7176

72-
immutable UTF16String <: AbstractString
73-
data::Vector{UInt16} # includes 16-bit NULL termination after string chars
74-
function UTF16String(data::Vector{UInt16})
75-
if length(data) < 1 || data[end] != 0
76-
throw(UnicodeError(UTF_ERR_NULL_16_TERMINATE, 0, 0))
77-
end
78-
new(data)
79-
end
77+
struct UTF8String <: AbstractString
78+
data::Vector{UInt8}
79+
UTF8String(data::String) = new(Vector{UInt8}(codeunits(data)))
80+
UTF8String(data) = new(data)
81+
end
82+
83+
struct UTF16String <: AbstractString
84+
data::Vector{UInt16} # includes 16-bit NULL termination after string chars
85+
function UTF16String(data::Vector{UInt16})
86+
if length(data) < 1 || data[end] != 0
87+
throw(UnicodeError(UTF_ERR_NULL_16_TERMINATE, 0, 0))
8088
end
89+
new(data)
90+
end
91+
end
8192

82-
immutable UTF32String <: DirectIndexString
83-
data::Vector{UInt32} # includes 32-bit NULL termination after string chars
84-
function UTF32String(data::Vector{UInt32})
85-
if length(data) < 1 || data[end] != 0
86-
throw(UnicodeError(UTF_ERR_NULL_32_TERMINATE, 0, 0))
87-
end
88-
new(data)
89-
end
93+
struct UTF32String <: DirectIndexString
94+
data::Vector{UInt32} # includes 32-bit NULL termination after string chars
95+
function UTF32String(data::Vector{UInt32})
96+
if length(data) < 1 || data[end] != 0
97+
throw(UnicodeError(UTF_ERR_NULL_32_TERMINATE, 0, 0))
9098
end
99+
new(data)
100+
end
101+
end
91102

92-
const ByteString = Union{ASCIIString,UTF8String}
103+
const ByteString = Union{ASCIIString,UTF8String}
93104

94-
include("support.jl")
95-
include("ascii.jl")
96-
include("utf8.jl")
97-
include("utf16.jl")
98-
include("utf32.jl")
99-
else
100-
using Base: UTF_ERR_SHORT, checkstring
101-
end
105+
include("support.jl")
106+
include("ascii.jl")
107+
include("utf8.jl")
108+
include("utf16.jl")
109+
include("utf32.jl")
110+
include("rep.jl")
102111

103-
if isdefined(Base, :RepString)
104-
using Base: RepString
105-
else
106-
include("rep.jl")
107-
end
112+
if isdefined(Base, :RevString)
113+
using Base: RevString
114+
else
115+
include("rev.jl")
116+
end
117+
118+
const AllLegacyStringTypes = Union{ASCIIString,UTF8String,UTF16String,UTF32String,RepString,RevString}
119+
120+
codeunit(s::SubString{<:AllLegacyStringTypes}) = codeunit(s.string)
121+
ncodeunits(s::SubString{<:AllLegacyStringTypes}) = isdefined(s, :ncodeunits) ? s.ncodeunits : s.endof
122+
123+
if !isdefined(Base, :iterate)
124+
iterate(s::Union{String,SubString,AllLegacyStringTypes}, i::Int) = next(s, i)
125+
end
108126

109-
if isdefined(Base, :RevString)
110-
using Base: RevString
111-
else
112-
include("rev.jl")
113-
end
114127
end # module

src/ascii.jl

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@
22

33
## required core functionality ##
44

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

8+
codeunit(s::ASCIIString) = UInt8
9+
ncodeunits(s::ASCIIString) = length(s.data)
10+
11+
if isdefined(Base, :iterate)
12+
import Base: iterate
13+
function iterate(s::ASCIIString, i::Int = firstindex(s))
14+
i > ncodeunits(s) && return nothing
15+
return next(s, i)
16+
end
17+
end
18+
819
## overload methods for efficiency ##
920

1021
bytestring(s::ASCIIString) = s
@@ -29,7 +40,7 @@ function string(c::ASCIIString...)
2940
for s in c
3041
n += length(s.data)
3142
end
32-
v = Vector{UInt8}(n)
43+
v = Vector{UInt8}(undef, n)
3344
o = 1
3445
for s in c
3546
ls = length(s.data)
@@ -97,12 +108,15 @@ write(io::IO, s::ASCIIString) = write(io, s.data)
97108

98109
ascii(x) = convert(ASCIIString, x)
99110
convert(::Type{ASCIIString}, s::ASCIIString) = s
100-
convert(::Type{ASCIIString}, s::String) = ascii(Vector{UInt8}(s))
111+
convert(::Type{ASCIIString}, s::String) = ascii(codeunits(s))
101112
convert(::Type{ASCIIString}, s::UTF8String) = ascii(s.data)
102113
convert(::Type{ASCIIString}, a::Vector{UInt8}) = begin
103114
isvalid(ASCIIString,a) || throw(ArgumentError("invalid ASCII sequence"))
104115
return ASCIIString(a)
105116
end
117+
if isdefined(Base, :codeunits)
118+
convert(::Type{ASCIIString}, a::Base.CodeUnits{UInt8,String}) = convert(ASCIIString, Vector{UInt8}(a))
119+
end
106120

107121
ascii(p::Ptr{UInt8}) =
108122
ascii(p, p == C_NULL ? Csize_t(0) : ccall(:strlen, Csize_t, (Ptr{UInt8},), p))

src/directindex.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ next(s::DirectIndexString, i::Int) = (s[i],i+1)
66

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

9-
isvalid(s::DirectIndexString, i::Integer) = (start(s) <= i <= endof(s))
9+
isvalid(s::DirectIndexString, i::Integer) = (firstindex(s) <= i <= lastindex(s))
1010

11-
prevind(s::DirectIndexString, i::Integer) = Int(i)-1
12-
nextind(s::DirectIndexString, i::Integer) = Int(i)+1
11+
prevind(s::DirectIndexString, i::Int) = i-1
12+
nextind(s::DirectIndexString, i::Int) = i+1
13+
prevind(s::DirectIndexString, i::Integer) = prevind(s, i)
14+
nextind(s::DirectIndexString, i::Integer) = nextind(s, i)
1315

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

27-
length(s::SubString{<:DirectIndexString}) = endof(s)
29+
length(s::SubString{<:DirectIndexString}) = lastindex(s)
2830

29-
isvalid(s::SubString{<:DirectIndexString}, i::Integer) = (start(s) <= i <= endof(s))
31+
isvalid(s::SubString{<:DirectIndexString}, i::Integer) = (firstindex(s) <= i <= ncodeunits(s))
3032

3133
ind2chr(s::SubString{<:DirectIndexString}, i::Integer) = begin checkbounds(s,i); i end
3234
chr2ind(s::SubString{<:DirectIndexString}, i::Integer) = begin checkbounds(s,i); i end

src/rep.jl

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
# This file includes code that was formerly a part of Julia. License is MIT: http://julialang.org/license
22

3-
immutable RepString <: AbstractString
3+
struct RepString <: AbstractString
44
string::AbstractString
55
repeat::Integer
66
end
77

8-
function endof(s::RepString)
9-
e = endof(s.string)
10-
(next(s.string,e)[2]-1) * (s.repeat-1) + e
8+
function lastindex(s::RepString)
9+
e = lastindex(s.string)
10+
(iterate(s.string,e)[2]-1) * (s.repeat-1) + e
1111
end
1212
length(s::RepString) = length(s.string)*s.repeat
1313
sizeof(s::RepString) = sizeof(s.string)*s.repeat
1414

15+
function isvalid(s::RepString, i::Int)
16+
1 i ncodeunits(s) || return false
17+
j = 1
18+
while j < i
19+
_, j = iterate(s, j)
20+
end
21+
return j == i
22+
end
23+
1524
function next(s::RepString, i::Int)
1625
if i < 1
1726
throw(BoundsError(s, i))
1827
end
19-
e = endof(s.string)
20-
sz = next(s.string,e)[2]-1
28+
e = lastindex(s.string)
29+
sz = iterate(s.string,e)[2]-1
2130

2231
r, j = divrem(i-1, sz)
2332
j += 1
@@ -26,8 +35,18 @@ function next(s::RepString, i::Int)
2635
throw(BoundsError(s, i))
2736
end
2837

29-
c, k = next(s.string, j)
38+
c, k = iterate(s.string, j)
3039
c, k-j+i
3140
end
3241

42+
codeunit(s::RepString) = codeunit(s.string)
43+
ncodeunits(s::RepString) = ncodeunits(s.string) * s.repeat
44+
45+
if isdefined(Base, :iterate)
46+
function iterate(s::RepString, i::Int = firstindex(s))
47+
i > ncodeunits(s) && return nothing
48+
return next(s, i)
49+
end
50+
end
51+
3352
convert(::Type{RepString}, s::AbstractString) = RepString(s,1)

src/rev.jl

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,37 @@
22

33
## reversed strings without data movement ##
44

5-
immutable RevString{T<:AbstractString} <: AbstractString
5+
struct RevString{T<:AbstractString} <: AbstractString
66
string::T
77
end
88

9-
endof(s::RevString) = endof(s.string)
9+
lastindex(s::RevString) = lastindex(s.string)
1010
length(s::RevString) = length(s.string)
1111
sizeof(s::RevString) = sizeof(s.string)
1212

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

18+
codeunit(s::RevString) = codeunit(s.string)
19+
ncodeunits(s::RevString) = ncodeunits(s.string)
20+
21+
if isdefined(Base, :iterate)
22+
function iterate(s::RevString, i::Int = firstindex(s))
23+
i > lastindex(s) && return nothing
24+
return next(s, i)
25+
end
26+
end
27+
28+
function isvalid(s::RevString, i::Int)
29+
1 i ncodeunits(s) || return false
30+
j = 1
31+
while j < i
32+
_, j = iterate(s, j)
33+
end
34+
return j == i
35+
end
36+
1837
reverse(s::RevString) = s.string
19-
reverseind(s::RevString, i::Integer) = endof(s) - i + 1
38+
reverseind(s::RevString, i::Integer) = lastindex(s) - i + 1

0 commit comments

Comments
 (0)