Should String#strip take a parameter?

W

Warren Brown

All,

Several times I have run across the need to strip characters other than
whitespaces from the beginning and/or end of a string. I have always
accomplished this using String#sub (or String#gsub) and an appropriate
pattern. For example:

price = field.sub(/^0+/,'')
descr = field.gsub(/(^_+)|(_+$)/,'')

It occurs to me that this would be a natural extension to the
String#strip methods (1.8 now includes String#lstrip and String#rstrip as
well as the "!" variations) by adding an optional String parameter to them.
The default value of the parameter would be nil to preserve backwards
compatibility, but if the parameter was not nil, the functions would strip
that string (or the string returned by that object's to_str method).

Example behavior:

class String

def new_lstrip(str = nil)
return self.lstrip if str.nil?
self.sub(/^#{str}+/,'')
end

def new_rstrip(str = nil)
return self.rstrip if str.nil?
self.sub(/#{str}+$/,'')
end

def new_strip(str = nil)
return self.lstrip if str.nil?
self.gsub(/(^#{str}+)|(#{str}+$)/,'')
end

end

If there is an interest in this, I can provide patches for 1.6.8 and/or
the latest 1.8 snapshot.

- Warren Brown
 
Y

Yukihiro Matsumoto

Hi,

In message "Should String#strip take a parameter?"

| If there is an interest in this, I can provide patches for 1.6.8 and/or
|the latest 1.8 snapshot.

We need to define the behavior first. Python 2.3's strip takes a
parameter, but it strips off all characters in the string, i.e.

a = "<><<>>>><abc>><><>"
a.strip("<>") # => "abc"

matz.
 
M

Martin DeMello

Yukihiro Matsumoto said:
Hi,

In message "Should String#strip take a parameter?"

| If there is an interest in this, I can provide patches for 1.6.8 and/or
|the latest 1.8 snapshot.

We need to define the behavior first. Python 2.3's strip takes a
parameter, but it strips off all characters in the string, i.e.

a = "<><<>>>><abc>><><>"
a.strip("<>") # => "abc"

That's unintuitive, IMO. I think if strip takes a parameter, it should
be a regex (a string can be autoconverted to its Regex.quoted form), and
implicitly wrap the regex in a (...)*. e.g.

s.strip(/[<>]/) ->
s.gsub!(/^([<>])*/,'')
s.gsub!(/([<>])*$/,'')

whereas
s.strip('<>') ->
s.gsub!(/^(<>)*/,'')
s.gsub!(/(<>)*$/,'')

Treating a string as a character class is not convenient enough to
justify its limitingness.

(Do ruby regexes support a noncapturing, nonbacktracking form of ()*?)

martin
 
X

Xiangrong Fang

Hi,

I have a question regarding strip. I wrote a DLL in other language, and
used the following to call the dll function in ruby:

arg = " " * 100
myfunc.Call(arg)

after that arg may contain a string such as:

"return value\000"

I used strip, but the strip does not strip away terminating null
character...

Am I doing something wrong? If I suggest that strip should also strip
the \000, is it a good idea or not?

Shannon


On Tue, 22 Jul 2003 11:07:08 +0900
 
M

Martin DeMello

Bj?rn Lindstr?m said:
Couldn't you just support both. That is, the argument could be either a
string or a RegExp.

My argument was that if the parameter is regexp oriented, it makes more
sense for a string to match itself than "any of its characters".

martin
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Should String#strip take a parameter?"

|Am I doing something wrong? If I suggest that strip should also strip
|the \000, is it a good idea or not?

Maybe good. I'd like to hear from others.

matz.
 
U

U.Nakamura

Hello,

In message "Re: Should String#strip take a parameter?"
| |Am I doing something wrong? If I suggest that strip should also strip
| |the \000, is it a good idea or not?
|
| Maybe good. I'd like to hear from others.

I think it's good.

Regards,
 
R

Robert Klemme

daz said:
Xiangrong Fang said:
Hi,

I have a question regarding strip. I wrote a DLL in other language, and
used the following to call the dll function in ruby:

arg = " " * 100
myfunc.Call(arg)

after that arg may contain a string such as:

"return value\000"

I used strip, but the strip does not strip away terminating null
character...

Am I doing something wrong? If I suggest that strip should also strip
the \000, is it a good idea or not?

Shannon

The only reason for thinking #strip might be relevant
in this situation is that you initialised your buffer
with spaces.


class String
def sz_cut! # cut at string-zero terminator
replace(self[/\A[^\0]*/])
end
end

p "return value\000 garbage".sz_cut!

There is a more efficient implementation of sz_cut!:

class String
def sz_cut! # cut at string-zero terminator
gsub!( /\0.*$/, '' )
end
end

Cheers

robert


user system total real
test1 change 5.235000 0.031000 5.266000 ( 5.281000)
test2 change 5.828000 0.032000 5.860000 ( 5.875000)
test1 no change 1.891000 0.000000 1.891000 ( 1.922000)
test2 no change 7.078000 0.000000 7.078000 ( 7.125000)

require 'benchmark'

REP=500000

STR1="foo\0garbage"
STR1.freeze

STR2="foo.garbage"
STR2.freeze

def test1(s)
s.gsub!( /\0.*$/, '' )
end

def test2(s)
s.replace(s[/\A[^\0]*/])
end

Benchmark.bm(10) do |x|
x.report "test1 change" do
REP.times { test1 STR1.dup }
end

x.report "test2 change" do
REP.times { test2 STR1.dup }
end

x.report "test1 no change" do
REP.times { test1 STR2.dup }
end

x.report "test2 no change" do
REP.times { test2 STR2.dup }
end
end
 
D

daz

In message "Re: Should String#strip take a parameter?"

|Am I doing something wrong? If I suggest that strip should also strip
|the \000, is it a good idea or not?

Maybe good. I'd like to hear from others.

matz.

##### Relates to CVS only #####

------------------------------------------------------------------
Wed Jul 23 15:49:01 2003 Yukihiro Matsumoto <[email protected]>

* string.c (rb_str_lstrip_bang): strip NUL along with white
spaces. [ruby-talk:76659]

* string.c (rb_str_rstrip_bang): ditto.
------------------------------------------------------------------

rstrip - great.

But IMvHO, changing lstrip as well is an entirely different
thing, altogether.

[CHORUS]: " """"Changing lstrip as well is an entirely different
thing."""" "

######### [OT] [OT] [OT] [OT] [OT] [OT] [OT] [OT] [OT] [OT] [OT]
http://www.geocities.com/SouthBeach/Sands/6797/apscript.html
(find on page: altogether) to view the transcripted line from
the "Airplane!" film.
#########


I can see the desire to be consistent, but ...

strip calls strip_bang, which calls lstrip_bang & rstrip_bang
in that order. I can't test it but would ...

p "\0abc\0def\0 ".strip

be handled usefully? The initial "\0" (C-string end) would
also be removed by the new strip (calling lstrip).
In that example, the C-string value was "" (empty).
After strip, "abc\0def" remains with the C-string
value of "abc".


I am prepared to be flicked across the room by a coiled finger,
of course, but I think the choices now might be:

1) rstrip removes "\0", lstrip doesn't. [disharmonic]
2) feature is added elsewhere.
3) change is backed-out and forgotten.


daz
1)3)2)
 
D

daz

Yukihiro Matsumoto said:
Hi,

In message "Re: Should String#strip take a parameter?"

|I am prepared to be flicked across the room by a coiled finger,
|of course, but I think the choices now might be:
|
| 1) rstrip removes "\0", lstrip doesn't. [disharmonic]
| 2) feature is added elsewhere.
| 3) change is backed-out and forgotten.

or
4) rstrip removes sequence of "\0" at the end of string. lstrip
doesn't.

matz.

Sorry, my English and my typing went out-of-sync :<}}
I did look at the change on CVSweb first.

---

4) rstrip removes "\0" or sequence of "\0" from the end of string,
lstrip doesn't remove any "\0" or sequence of "\0" from the start
of string (only ISSPACE() or sequence of).
2) feature is added elsewhere.
3) change is backed-out and forgotten.

There was never a 1), it was just a bad dream.
Now, our list is messy but you've coerced my votes into a sequence.

Thanks,


daz
4)3)2)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top