Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

some small string function performance tweaks

document optional length argument to bytestring()
  • Loading branch information...
commit ea10a0b6e5c4f8873d3500aa0e09d8875151c143 1 parent 00b6092
@JeffBezanson JeffBezanson authored
View
3  base/ascii.jl
@@ -21,6 +21,9 @@ search(s::ASCIIString, c::Char, i::Integer) = c < 0x80 ? search(s.data,uint8(c),
rsearch(s::ASCIIString, c::Char, i::Integer) = c < 0x80 ? rsearch(s.data,uint8(c),i) : 0
function string(c::ASCIIString...)
+ if length(c) == 1
+ return c[1]
+ end
n = 0
for s in c
n += length(s.data)
View
6 base/iobuffer.jl
@@ -165,9 +165,9 @@ function close(io::IOBuffer)
end
isopen(io::IOBuffer) = io.readable || io.writable || io.seekable || nb_available(io) > 0
function bytestring(io::IOBuffer)
- if !io.readable error("bytestring read failed") end
- if !io.seekable error("bytestring read failed") end
- bytestring(io.data[1:io.size])
+ if !io.readable error("bytestring read failed") end
+ if !io.seekable error("bytestring read failed") end
+ bytestring(pointer(io.data), io.size)
end
function takebuf_array(io::IOBuffer)
if io.seekable
View
3  base/string.jl
@@ -54,7 +54,6 @@ convert(::Type{Symbol}, s::String) = symbol(s)
start(s::String) = 1
done(s::String,i) = (i > endof(s))
-isempty(s::String) = done(s,start(s))
getindex(s::String, i::Int) = next(s,i)[1]
getindex(s::String, i::Integer) = s[int(i)]
getindex(s::String, x::Real) = s[to_index(x)]
@@ -640,8 +639,6 @@ function getindex(s::SubString, i::Int)
getindex(s.string, i+s.offset)
end
-isempty(s::SubString) = s.endof == 0
-
endof(s::SubString) = s.endof
# TODO: length(s::SubString) = ??
# default implementation will work but it's slow
View
3  base/utf8.jl
@@ -131,6 +131,9 @@ function rsearch(s::UTF8String, c::Char, i::Integer)
end
function string(a::ByteString...)
+ if length(a) == 1
+ return a[1]::UTF8String
+ end
# ^^ at least one must be UTF-8 or the ASCII-only method would get called
data = Array(Uint8,0)
for d in a
View
2  doc/manual/performance-tips.rst
@@ -322,7 +322,7 @@ by ``1`` input vector::
julia> x = randn(10000);
- julia> fmt(f) = println("$(rpad(string(f)*": ", 14, ' '))$(@elapsed f(x))")
+ julia> fmt(f) = println(rpad(string(f)*": ", 14, ' '), @elapsed f(x))
julia> map(fmt, {copy_cols, copy_rows, copy_col_row, copy_row_col});
copy_cols: 0.331706323
View
4 doc/manual/style-guide.rst
@@ -210,7 +210,7 @@ If a type is effectively an enumeration, it should be defined as a single
(ideally ``immutable``) type, with the enumeration values being instances
of it. Constructors and conversions can check whether values are valid.
This design is preferred over making the enumeration an abstract type,
-with the ``values`` as subtypes.
+with the "values" as subtypes.
Don't overuse macros
--------------------
@@ -257,7 +257,7 @@ customizing its behavior can make it harder to work with.
Be careful with type equality
-----------------------------
-You generally want to use ``isa`` and ``<:`` (``subtype``) for testing types,
+You generally want to use ``isa`` and ``<:`` (``issubtype``) for testing types,
not ``==``. Checking types for exact equality typically only makes sense
when comparing to a known concrete type (e.g. ``T == Float64``), or if you
*really, really* know what you're doing.
View
4 doc/stdlib/base.rst
@@ -951,9 +951,9 @@ Strings
Create a string from any value using the ``showall`` function.
-.. function:: bytestring(::Ptr{Uint8})
+.. function:: bytestring(::Ptr{Uint8}, [length])
- Create a string from the address of a C (0-terminated) string. A copy is made; the ptr can be safely freed.
+ Create a string from the address of a C (0-terminated) string. A copy is made; the ptr can be safely freed. If ``length`` is specified, the string does not have to be 0-terminated.
.. function:: bytestring(s)
Please sign in to comment.
Something went wrong with that request. Please try again.