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

specialize length(SubString) for DirectIndexString #7145

Merged
merged 3 commits into from
Jun 6, 2014
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: 2 additions & 0 deletions base/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,8 @@ print(io::IOBuffer, s::SubString) = write(io, s)

sizeof{T<:ByteString}(s::SubString{T}) = s.endof==0 ? 0 : next(s,s.endof)[2]-1

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

function next(s::SubString, i::Int)
if i < 1 || i > s.endof
error(BoundsError)
Expand Down
2 changes: 2 additions & 0 deletions doc/stdlib/test.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ ________
To use the default handler, the macro :func:`@test` can be used directly::

# Julia code
julia> using Base.Test

julia> @test 1 == 1

julia> @test 1 == 0
Expand Down
20 changes: 20 additions & 0 deletions test/strings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,26 @@ for i1 = 1:length(u8str2)
end
end

str="tempus fugit" #length(str)==12
ss=SubString(str,1,length(str)) #match source string
@test length(ss)==length(str)

ss=SubString(str,1,0) #empty SubString
@test length(ss)==0

ss=SubString(str,14,20) #start indexed beyond source string length
@test length(ss)==0

ss=SubString(str,10,16) #end indexed beyond source string length
@test length(ss)==3

str2=""
ss=SubString(str2,1,4) #empty source string
@test length(ss)==0

ss=SubString(str2,1,1) #empty source string, identical start and end index
@test length(ss)==0

str = "aa\u2200\u2222bb"
u = SubString(str, 3, 6)
@test length(u)==2
Expand Down