diff --git a/base/ascii.jl b/base/ascii.jl index 0ea7a803f1d2e..ac0e63c95d6ba 100644 --- a/base/ascii.jl +++ b/base/ascii.jl @@ -14,7 +14,7 @@ ref(s::ASCIIString, i::Int) = char(s.data[i]) ref(s::ASCIIString, r::Vector) = ASCIIString(ref(s.data,r)) ref(s::ASCIIString, r::Range1{Int}) = ASCIIString(ref(s.data,r)) -ref(s::ASCIIString, indx::Array{Int,1}) = ASCIIString(s.data[indx]) +ref(s::ASCIIString, indx::AbstractVector{Int}) = ASCIIString(s.data[indx]) strchr(s::ASCIIString, c::Char) = c < 0x80 ? memchr(s.data, c) : 0 strcat(a::ASCIIString, b::ASCIIString, c::ASCIIString...) = ASCIIString(memcat(a,b,c...)) diff --git a/base/intfuncs.jl b/base/intfuncs.jl index 9669f636c9723..207de769d3cdd 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -3,11 +3,11 @@ isodd(n::Integer) = bool(rem(n,2)) iseven(n::Integer) = !isodd(n) -sign{T<:Integer}(x::T) = convert(T,(x > 0)-(x < 0)) -sign{T<:Unsigned}(x::T) = convert(T,(x > 0)) +sign{T<:Integer}(x::T) = convert(T,(x>0)-(x<0)) +sign{T<:Unsigned}(x::T) = convert(T,(x>0)) signbit(x::Unsigned) = 0 -signbit(x::Int8 ) = int(x>>>7 ) +signbit(x::Int8 ) = int(x>>>7) signbit(x::Int16) = int(x>>>15) signbit(x::Int32) = int(x>>>31) signbit(x::Int64) = int(x>>>63) @@ -17,14 +17,14 @@ flipsign(x::Int64, y::Int64) = boxsi64(flipsign_int64(unbox64(x),unbox64(y))) flipsign{T<:Signed}(x::T,y::T) = flipsign(int(x),int(y)) flipsign(x::Signed, y::Signed) = flipsign(promote(x,y)...) -flipsign(x::Signed, y::Real) = flipsign(x, -oftype(x,signbit(y))) flipsign(x::Signed, y::Float32) = flipsign(x, reinterpret(Int32,y)) flipsign(x::Signed, y::Float64) = flipsign(x, reinterpret(Int64,y)) +flipsign(x::Signed, y::Real) = flipsign(x, -oftype(x,signbit(y))) copysign(x::Signed, y::Signed) = flipsign(x, x$y) -copysign(x::Signed, y::Real) = copysign(x, -oftype(x,signbit(y))) copysign(x::Signed, y::Float32) = copysign(x, reinterpret(Int32,y)) copysign(x::Signed, y::Float64) = copysign(x, reinterpret(Int64,y)) +copysign(x::Signed, y::Real) = copysign(x, -oftype(x,signbit(y))) abs(x::Unsigned) = x abs(x::Signed) = flipsign(x,x) diff --git a/base/linalg.jl b/base/linalg.jl index da561d234bc2f..027d811a56d7e 100644 --- a/base/linalg.jl +++ b/base/linalg.jl @@ -389,22 +389,19 @@ diagmm(A::Matrix, b::Vector) = diagmm(b::Vector, A::Matrix) = diagmm!(Array(promote_type(eltype(A),eltype(b)),size(A)), b, A) -^(A::AbstractMatrix, p::Integer) = p < 0 ? inv(A^-p) : power_by_squaring(A,p) +^(A::AbstractMatrix, p::Integer) = + size(A,1) != size(A,2) ? error("matrix must be square") : + p < 0 ? A^float(p) : power_by_squaring(A,p) function ^(A::AbstractMatrix, p::Number) - if integer_valued(p) - ip = integer(real(p)) - if ip < 0 - return inv(power_by_squaring(A, -ip)) - else - return power_by_squaring(A, ip) - end + if p >= 0 && integer_valued(p) + return power_by_squaring(A, integer(real(p))) end if size(A,1) != size(A,2) error("matrix must be square") end - (v, X) = eig(A) - if isreal(v) && any(v<0) + v, X = eig(A) + if isreal(v) && any(v < 0) v = complex(v) end if ishermitian(A) diff --git a/base/regex.jl b/base/regex.jl index 577d40086de5c..81c98ba750c8a 100644 --- a/base/regex.jl +++ b/base/regex.jl @@ -129,17 +129,7 @@ function split(s::String, regex::Regex, include_empty::Bool, limit::Integer) end return strs end - -split(s::String, x::String, incl::Bool, limit::Integer) = - strwidth(x) == 1 ? split(s, x[1], incl, limit) : - split(s, Regex(strcat("\\Q",x)), incl, limit) - -split(s::String, regex::Regex, include_empty::Bool) = - split(s, regex, include_empty, 0) - -split(s::String, x::String, incl::Bool) = - strwidth(x) == 1 ? split(s, x[1], incl) : - split(s, Regex(strcat("\\Q",x)), incl) +split(s::String, regex::Regex, include_empty::Bool) = split(s, regex, include_empty, 0) replace(s::String, regex::Regex, repl::String, limit::Integer) = join(split(s, regex, true, limit), repl) diff --git a/base/string.jl b/base/string.jl index 56eaf4b7e5cf4..0a3ea4ddc00ca 100644 --- a/base/string.jl +++ b/base/string.jl @@ -14,7 +14,7 @@ ref(s::String, i::Int) = next(s,i)[1] ref(s::String, i::Integer) = s[int(i)] ref(s::String, x::Real) = s[iround(x)] ref{T<:Integer}(s::String, r::Range1{T}) = s[int(first(r)):int(last(r))] -ref(s::String, v::Vector) = +ref(s::String, v::AbstractVector) = print_to_string(length(v), @thunk for i in v; print(s[i]); end) symbol(s::String) = symbol(cstring(s)) @@ -773,33 +773,73 @@ rpad(s, n::Integer, p) = rpad(string(s), n, string(p)) lpad(s, n::Integer) = lpad(string(s), n, " ") rpad(s, n::Integer) = rpad(string(s), n, " ") +# split on a single character in a collection function split(s::String, delims, include_empty::Bool) - i = 1 + i = start(s) len = length(s) strs = String[] while true tokstart = tokend = i while !done(s,i) - (c,i) = next(s,i) + c,i = next(s,i) if contains(delims, c) break end tokend = i end - tok = s[tokstart:(tokend-1)] - if include_empty || !isempty(tok) - push(strs, tok) + if include_empty || tokstart < tokend + push(strs, s[tokstart:tokend-1]) end - if !((i <= len) || (i==len+1 && tokend!=i)) + if !(i <= len || i==len+1 && tokend!=i) break end end - strs + return strs end split(s::String) = split(s, (' ','\t','\n','\v','\f','\r'), false) split(s::String, x) = split(s, x, true) +# split on a string literal +function split(s::String, delim::String, include_empty::Bool) + i = start(s) + len = length(s) + strs = String[] + jj = start(delim) + d1, jj = next(delim,jj) + tokstart = tokend = i + while !done(s,i) + c,i = next(s,i) + if c == d1 + j = jj + matched = true + while !done(delim,j) + if done(s,i) + matched = false + break + end + c,i = next(s,i) + d,j = next(delim,j) + if c != d + matched = false + break + end + end + if matched + if include_empty || tokstart < tokend + push(strs, s[tokstart:tokend-1]) + end + tokstart = i + end + end + tokend = i + end + if include_empty || tokstart < tokend + push(strs, s[tokstart:tokend-1]) + end + return strs +end + function print_joined(strings, delim, last) i = start(strings) if done(strings,i)