Skip to content

Commit

Permalink
assert debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanKarpinski committed Dec 8, 2017
1 parent 55d854a commit 765d6eb
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
6 changes: 4 additions & 2 deletions base/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,10 @@ print(io::IO, s::AbstractString) = (write(io, s); nothing)
write(io::IO, s::AbstractString) = (len = 0; for c in s; len += write(io, c); end; len)
show(io::IO, s::AbstractString) = print_quoted(io, s)

write(to::GenericIOBuffer, s::SubString{String}) =
s.ncodeunits 0 ? 0 : unsafe_write(to, pointer(s.string, s.offset + 1), UInt(s.ncodeunits))
function write(to::GenericIOBuffer, s::SubString{String})
assert(s.ncodeunits  0)
s.ncodeunits 0 ? 0 : unsafe_write(to, pointer(s.string, s.offset+1), UInt(s.ncodeunits))
end

## printing literal quoted string data ##

Expand Down
1 change: 1 addition & 0 deletions base/strings/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ end

function ==(a::String, b::String)
al = sizeof(a)
assert(al  0)
al == sizeof(b) && 0 == ccall(:memcmp, Int32, (Ptr{UInt8}, Ptr{UInt8}, UInt), a, b, al)
end

Expand Down
5 changes: 3 additions & 2 deletions base/strings/substring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ prevind(s::SubString, i::Integer) = prevind(s.string, s.offset + i) - s.offset
function cmp(a::SubString{String}, b::SubString{String})
na = sizeof(a)
nb = sizeof(b)
assert(na  0 && nb 0)
c = ccall(:memcmp, Int32, (Ptr{UInt8}, Ptr{UInt8}, UInt),
pointer(a), pointer(b), min(na,nb))
return c < 0 ? -1 : c > 0 ? +1 : cmp(na,nb)
pointer(a), pointer(b), min(na, nb))
return c < 0 ? -1 : c > 0 ? +1 : cmp(na, nb)
end

# don't make unnecessary copies when passing substrings to C functions
Expand Down
12 changes: 8 additions & 4 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ function endswith(a::AbstractString, b::AbstractString)
end
endswith(str::AbstractString, chars::Chars) = !isempty(str) && last(str) in chars

startswith(a::String, b::String) =
(sizeof(a) >= sizeof(b) && ccall(:memcmp, Int32, (Ptr{UInt8}, Ptr{UInt8}, UInt), a, b, sizeof(b)) == 0)
startswith(a::Vector{UInt8}, b::Vector{UInt8}) =
(length(a) >= length(b) && ccall(:memcmp, Int32, (Ptr{UInt8}, Ptr{UInt8}, UInt), a, b, length(b)) == 0)
# FIXME: check that end of `b` doesn't match a partial character in `a`
function startswith(a::String, b::String)
assert(sizeof(b)  0)
sizeof(a) sizeof(b) &&
ccall(:memcmp, Int32, (Ptr{UInt8}, Ptr{UInt8}, UInt), a, b, sizeof(b)) == 0
end
startswith(a::Vector{UInt8}, b::Vector{UInt8}) = length(a) length(b) &&
ccall(:memcmp, Int32, (Ptr{UInt8}, Ptr{UInt8}, UInt), a, b, length(b)) == 0

# TODO: fast endswith

Expand Down

0 comments on commit 765d6eb

Please sign in to comment.