Skip to content

Commit

Permalink
add endof(), giving the last index (used by A[end]), now crucial for …
Browse files Browse the repository at this point in the history
…strings

make length(String) give character count. string[end] now also works.
closes #1939, closes #1454
  • Loading branch information
JeffBezanson committed Jan 12, 2013
1 parent a318691 commit 51410ae
Show file tree
Hide file tree
Showing 18 changed files with 129 additions and 128 deletions.
1 change: 1 addition & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ ndims{T,n}(::AbstractArray{T,n}) = n
ndims{T,n}(::Type{AbstractArray{T,n}}) = n
ndims{T<:AbstractArray}(::Type{T}) = ndims(super(T))
length(t::AbstractArray) = prod(size(t))
endof(a::AbstractArray) = length(a)
first(a::AbstractArray) = a[1]
last(a::AbstractArray) = a[end]

Expand Down
2 changes: 1 addition & 1 deletion base/ascii.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

## required core functionality ##

length(s::ASCIIString) = length(s.data)
endof(s::ASCIIString) = length(s.data)
ref(s::ASCIIString, i::Int) = (x=s.data[i]; x < 0x80 ? char(x) : '\ufffd')

## overload methods for efficiency ##
Expand Down
1 change: 1 addition & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ end
@deprecate numel length
@deprecate idump xdump
@deprecate cwd pwd
@deprecate strlen length

# aliases
@deprecate chi2rnd randchi2
Expand Down
5 changes: 1 addition & 4 deletions base/export.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export
FileDes,
FileOffset,
Filter,
GenericString,
IO,
IOStream,
IOString,
Expand Down Expand Up @@ -797,6 +796,7 @@ export
keys,
keytype,
length,
endof,
setdiff,
map,
map_to,
Expand Down Expand Up @@ -879,7 +879,6 @@ export
strchr,
string,
strip,
strlen,
strwidth,
thisind,
transform_to_utf8,
Expand Down Expand Up @@ -1368,12 +1367,10 @@ export
bind,
AsyncStream,
Buffer,
IOString,
PipeString,
SpawnNullStream,
Stream,
TcpSocket,
jl_alloca,
systmpdir,
open_any_tcp_port,
connect_to_host,
Expand Down
4 changes: 3 additions & 1 deletion base/iostring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ end

read{T}(from::IOString, ::Type{Ptr{T}}) = convert(Ptr{T}, read(from, Uint))

length(io::IOString) = (io.seekable ? io.size : nb_available(io))
# TODO: IOString is not iterable, so doesn't really have a length.
# This should maybe be sizeof() instead.
#length(io::IOString) = (io.seekable ? io.size : nb_available(io))
nb_available(io::IOString) = io.size - io.ptr + 1
skip(io::IOString, n::Integer) = (io.ptr = min(io.ptr + n, io.size+1))
function seek(io::IOString, n::Integer)
Expand Down
4 changes: 2 additions & 2 deletions base/pcre.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ free_study(extra::Ptr{Void}) =

function exec(regex::Array{Uint8}, extra::Ptr{Void},
str::ByteString, offset::Integer, options::Integer, cap::Bool)
if offset < 0 || length(str) < offset
if offset < 0 || length(str.data) < offset
error(BoundsError)
end
ncap = info(regex, extra, INFO_CAPTURECOUNT, Int32)
ovec = Array(Int32, 3(ncap+1))
n = ccall((:pcre_exec, :libpcre), Int32,
(Ptr{Void}, Ptr{Void}, Ptr{Uint8}, Int32,
Int32, Int32, Ptr{Int32}, Int32),
regex, extra, str, length(str),
regex, extra, str, length(str.data),
offset, options, ovec, length(ovec))
if n < -1
error("exec: error $n")
Expand Down
6 changes: 3 additions & 3 deletions base/printf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function _gen(s::String)
blk = expr(:block, :(local neg, pt, len, exp))
for x in _parse(s)
if isa(x,String)
push!(blk.args, :(write(out, $(strlen(x)==1 ? x[1] : x))))
push!(blk.args, :(write(out, $(length(x)==1 ? x[1] : x))))
else
c = lc(x[end])
f = c=='f' ? _gen_f :
Expand Down Expand Up @@ -240,7 +240,7 @@ function _gen_d(flags::ASCIIString, width::Int, precision::Int, c::Char)
push!(blk.args, :(neg = NEG[1]))
push!(blk.args, :(pt = POINT[1]))
# calculate padding
width -= strlen(prefix)
width -= length(prefix)
space_pad = width > max(1,precision) && contains(flags,'-') ||
precision < 0 && width > 1 && !contains(flags,'0') ||
precision >= 0 && width > precision
Expand Down Expand Up @@ -374,7 +374,7 @@ function _gen_e(flags::ASCIIString, width::Int, precision::Int, c::Char)
end
# calculate padding
padding = nothing
width -= precision+strlen(expmark)+(precision>0)+4
width -= precision+length(expmark)+(precision>0)+4
# 4 = leading + expsign + 2 exp digits
if contains(flags,'+') || contains(flags,' ')
width -= 1 # for the sign indicator
Expand Down
2 changes: 1 addition & 1 deletion base/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ match(r::Regex, s::String, i::Integer) = match(r, s, i, r.options & PCRE.EXECUTE
match(r::Regex, s::String) = match(r, s, start(s))

function search(str::ByteString, re::Regex, idx::Integer)
len = length(str)
len = length(str.data)
if idx >= len+2
return idx == len+2 ? (0,0) : error(BoundsError)
end
Expand Down
40 changes: 20 additions & 20 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -571,23 +571,23 @@ function showall{T}(io, a::AbstractArray{T,1})
show_comma_array(io, a, opn, cls)
end

alignment(x::Any) = (0, strlen(sprint(showcompact, x)))
alignment(x::Number) = (strlen(sprint(showcompact, x)), 0)
alignment(x::Integer) = (strlen(sprint(showcompact, x)), 0)
alignment(x::Any) = (0, length(sprint(showcompact, x)))
alignment(x::Number) = (length(sprint(showcompact, x)), 0)
alignment(x::Integer) = (length(sprint(showcompact, x)), 0)
function alignment(x::Real)
m = match(r"^(.*?)((?:[\.eE].*)?)$", sprint(showcompact, x))
m == nothing ? (strlen(sprint(showcompact, x)), 0) :
(strlen(m.captures[1]), strlen(m.captures[2]))
m == nothing ? (length(sprint(showcompact, x)), 0) :
(length(m.captures[1]), length(m.captures[2]))
end
function alignment(x::Complex)
m = match(r"^(.*,)(.*)$", sprint(showcompact, x))
m == nothing ? (strlen(sprint(showcompact, x)), 0) :
(strlen(m.captures[1]), strlen(m.captures[2]))
m == nothing ? (length(sprint(showcompact, x)), 0) :
(length(m.captures[1]), length(m.captures[2]))
end
function alignment(x::Rational)
m = match(r"^(.*?/)(/.*)$", sprint(showcompact, x))
m == nothing ? (strlen(sprint(showcompact, x)), 0) :
(strlen(m.captures[1]), strlen(m.captures[2]))
m == nothing ? (length(sprint(showcompact, x)), 0) :
(length(m.captures[1]), length(m.captures[2]))
end

const undef_ref_str = "#undef"
Expand Down Expand Up @@ -651,8 +651,8 @@ function print_matrix_vdots(io,
for k = 1:length(A)
w = A[k][1] + A[k][2]
if k % M == m
l = repeat(" ", max(0, A[k][1]-strlen(vdots)))
r = repeat(" ", max(0, w-strlen(vdots)-strlen(l)))
l = repeat(" ", max(0, A[k][1]-length(vdots)))
r = repeat(" ", max(0, w-length(vdots)-length(l)))
print(io, l, vdots, r)
else
print(io, repeat(" ", w))
Expand All @@ -667,11 +667,11 @@ function print_matrix(io,
hdots::String, vdots::String, ddots::String,
hmod::Integer, vmod::Integer
)
cols -= strlen(pre) + strlen(post)
presp = repeat(" ", strlen(pre))
cols -= length(pre) + length(post)
presp = repeat(" ", length(pre))
postsp = ""
@assert strwidth(hdots) == strwidth(ddots)
ss = strlen(sep)
ss = length(sep)
m, n = size(X)
if m <= rows # rows fit
A = alignment(X,1:m,1:n,cols,cols,ss)
Expand All @@ -683,14 +683,14 @@ function print_matrix(io,
if i != m; println(io, ); end
end
else # rows fit, cols don't
c = div(cols-strlen(hdots)+1,2)+1
c = div(cols-length(hdots)+1,2)+1
R = reverse(alignment(X,1:m,n:-1:1,c,c,ss))
c = cols - sum(map(sum,R)) - (length(R)-1)*ss - strlen(hdots)
c = cols - sum(map(sum,R)) - (length(R)-1)*ss - length(hdots)
L = alignment(X,1:m,1:n,c,c,ss)
for i = 1:m
print(io, i == 1 ? pre : presp)
print_matrix_row(io, X,L,i,1:length(L),sep)
print(io, i % hmod == 1 ? hdots : repeat(" ", strlen(hdots)))
print(io, i % hmod == 1 ? hdots : repeat(" ", length(hdots)))
print_matrix_row(io, X,R,i,n-length(R)+1:n,sep)
print(io, i == m ? post : postsp)
if i != m; println(io, ); end
Expand All @@ -713,15 +713,15 @@ function print_matrix(io,
end
end
else # neither rows nor cols fit
c = div(cols-strlen(hdots)+1,2)+1
c = div(cols-length(hdots)+1,2)+1
R = reverse(alignment(X,I,n:-1:1,c,c,ss))
c = cols - sum(map(sum,R)) - (length(R)-1)*ss - strlen(hdots)
c = cols - sum(map(sum,R)) - (length(R)-1)*ss - length(hdots)
L = alignment(X,I,1:n,c,c,ss)
r = mod((length(R)-n+1),vmod)
for i in I
print(io, i == 1 ? pre : presp)
print_matrix_row(io, X,L,i,1:length(L),sep)
print(io, i % hmod == 1 ? hdots : repeat(" ", strlen(hdots)))
print(io, i % hmod == 1 ? hdots : repeat(" ", length(hdots)))
print_matrix_row(io, X,R,i,n-length(R)+1:n,sep)
print(io, i == m ? post : postsp)
if i != I[end]; println(io, ); end
Expand Down
Loading

0 comments on commit 51410ae

Please sign in to comment.