Permalink
Browse files

Small fixes/additions in strings.jl:

*) Added Set{Char} to Chars (also added 'contains' as an alias
   to 'has' for sets, for efficiency)
*) Made search(::String, ::Chars) be consistent with
   search(::String, ::String)
*) Added write to all String types
*) Aliased has == contains for strings to avoid conversion to
   Set
  • Loading branch information...
1 parent 0a641b1 commit fecdeb85ed6d1a97813eec9a2d73d0e3ed8b9b2e @carlobaldassi carlobaldassi committed Sep 3, 2012
Showing with 17 additions and 5 deletions.
  1. +1 −1 base/ascii.jl
  2. +1 −0 base/set.jl
  3. +14 −3 base/string.jl
  4. +1 −1 base/utf8.jl
View
@@ -70,7 +70,7 @@ end
## outputing ASCII strings ##
print(io::IO, s::ASCIIString) = (write(io, s.data);nothing)
-write(io, s::ASCIIString) = write(io, s.data)
+write(io::IO, s::ASCIIString) = write(io, s.data)
## transcoding to ASCII ##
View
@@ -16,6 +16,7 @@ elements(s::Set) = keys(s.hash)
eltype{T}(s::Set{T}) = T
has(s::Set, x) = has(s.hash, x)
+contains(s::Set, x) = has(s, x)
get(s::Set, x, deflt) = get(s.hash, x, false)
add(s::Set, x) = (s.hash[x] = true; s)
View
@@ -63,6 +63,7 @@ ref(s::String, v::AbstractVector) =
symbol(s::String) = symbol(bytestring(s))
print(io::IO, s::String) = for c in s write(io, c) end
+write(io::IO, s::String) = print(io, s)
show(io::IO, s::String) = print_quoted(io, s)
(*)(s::String...) = strcat(s...)
@@ -156,7 +157,7 @@ function chr2ind(s::String, i::Integer)
end
end
-typealias Chars Union(Char,AbstractVector{Char})
+typealias Chars Union(Char,AbstractVector{Char},Set{Char})
function strchr(s::String, c::Chars, i::Integer)
if i < 1 error("index out of range") end
@@ -174,7 +175,15 @@ strchr(s::String, c::Chars) = strchr(s,c,start(s))
contains(s::String, c::Char) = (strchr(s,c)!=0)
-search(s::String, c::Chars, i::Integer) = (i=strchr(s,c,i); (i,nextind(s,i)))
+function search(s::String, c::Chars, i::Integer)
+ if isempty(c)
+ return 1 <= i <= length(s)+1 ? (i,i) :
+ i == length(s)+2 ? (0,0) :
+ error("index out of range")
+ end
+ i=strchr(s,c,i)
+ (i, nextind(s,i))
+end
search(s::String, c::Chars) = search(s,c,start(s))
function search(s::String, t::String, i::Integer)
@@ -447,6 +456,8 @@ strcat(xs...) = string(xs...) # backwards compat
print(io::IO, s::RopeString) = print(io, s.head, s.tail)
+write(io::IO, s::RopeString) = (write(io, s.head); write(io, s.tail))
+
## transformed strings ##
type TransformedString <: String
@@ -500,7 +511,7 @@ function filter(f::Function, s::String)
takebuf_string(out)
end
-has(s::String, c::Char) = has(Set(s...), c)
+has(s::String, c::Char) = contains(s, c)
## string promotion rules ##
View
@@ -94,7 +94,7 @@ lcfirst(s::UTF8String) = string(lowercase(s[1]), s[2:])
## outputing UTF-8 strings ##
print(io::IO, s::UTF8String) = (write(io, s.data);nothing)
-write(io, s::UTF8String) = write(io, s.data)
+write(io::IO, s::UTF8String) = write(io, s.data)
## transcoding to UTF-8 ##

0 comments on commit fecdeb8

Please sign in to comment.