From 92334cd23f8a4b921464fc9ebbf7aba234748aec Mon Sep 17 00:00:00 2001 From: Mike Nolta Date: Tue, 21 Feb 2012 17:12:55 -0500 Subject: [PATCH 1/2] add strip,lstrip,rstrip,uppercase,lowercase string functions --- j/string.j | 31 +++++++++++++++++++++++++++++++ test/strings.j | 3 +++ 2 files changed, 34 insertions(+) diff --git a/j/string.j b/j/string.j index bacc5edd01e37..bfa7b4c9f80a8 100644 --- a/j/string.j +++ b/j/string.j @@ -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) @@ -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) @@ -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:end] + 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:end-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) diff --git a/test/strings.j b/test/strings.j index 9af287dd7630d..b932a8b26f13a 100644 --- a/test/strings.j +++ b/test/strings.j @@ -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" From 133a9f655f336f1c21b1388f6f5431d2f4e37314 Mon Sep 17 00:00:00 2001 From: Mike Nolta Date: Tue, 21 Feb 2012 17:33:55 -0500 Subject: [PATCH 2/2] replace end -> thisind(s,length(s)) in [lr]strip --- j/string.j | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/j/string.j b/j/string.j index bfa7b4c9f80a8..c2bf017708564 100644 --- a/j/string.j +++ b/j/string.j @@ -825,7 +825,7 @@ function lstrip(s::String) while !done(s,i) c, j = next(s,i) if !iswspace(c) - return s[i:end] + return s[i:thisind(s,length(s))] end i = j end @@ -838,7 +838,7 @@ function rstrip(s::String) while !done(r,i) c, j = next(r,i) if !iswspace(c) - return s[1:end-i+1] + return s[1:thisind(s,length(s))-i+1] end i = j end