Skip to content

Commit

Permalink
Merge pull request #437 from nolta/string
Browse files Browse the repository at this point in the history
add strip,lstrip,rstrip,split(x),uppercase,lowercase string functions
  • Loading branch information
StefanKarpinski committed Feb 21, 2012
2 parents e73680d + 133a9f6 commit 7ff014a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
31 changes: 31 additions & 0 deletions j/string.j
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,9 @@ lc(s::String) = TransformedString((c,i)->lc(c), s)
ucfirst(s::String) = TransformedString((c,i)->i==1 ? uc(c) : c, s)
lcfirst(s::String) = TransformedString((c,i)->i==1 ? lc(c) : c, s)

uppercase = uc
lowercase = lc

## string map ##

function map(f::Function, s::String)
Expand Down Expand Up @@ -781,6 +784,7 @@ function split(s::String, delims, include_empty::Bool)
strs
end

split(s::String) = split(s, " ", false)
split(s::String, x) = split(s, x, true)
split(s::String, x::Char, incl::Bool) = split(s, (x,), incl)

Expand Down Expand Up @@ -816,6 +820,33 @@ chop(s::String) = s[1:thisind(s,length(s))-1]
chomp(s::String) = (i=thisind(s,length(s)); s[i]=='\n' ? s[1:i-1] : s)
chomp(s::ByteString) = s.data[end]==0x0a ? s[1:end-1] : s

function lstrip(s::String)
i = start(s)
while !done(s,i)
c, j = next(s,i)
if !iswspace(c)
return s[i:thisind(s,length(s))]
end
i = j
end
""
end

function rstrip(s::String)
r = reverse(s)
i = start(r)
while !done(r,i)
c, j = next(r,i)
if !iswspace(c)
return s[1:thisind(s,length(s))-i+1]
end
i = j
end
""
end

strip(s::String) = lstrip(rstrip(s))

## string to integer functions ##

function parse_int{T<:Integer}(::Type{T}, s::String, base::Integer)
Expand Down
3 changes: 3 additions & 0 deletions test/strings.j
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,6 @@ end
@assert int2str(typemin(Int64), 10) == "-9223372036854775808"
@assert int2str(typemin(Int16), 10) == "-32768"
@assert int2str(typemin(Int8 ), 10) == "-128"

# string manipulation
@assert strip("\t hi \n") == "hi"

0 comments on commit 7ff014a

Please sign in to comment.