Gsub(/\s*$/, "") doubling string

Hello,
I recently downloaded ruby 1.8.0 p3, (2003-06-23) [i686-linux], and
tried it on some code that chopped trailing spaces from a string using
gsub(/\s*$/, “”). I’m seeing some odd behavior and was hoping someone
could shed some light on what’s happening.

In 1.6.7 the code above did what I expected. However, in 1.8.0p3 it
seems to double the string if the string doesn’t end in a space.

#does what I expected in 1.6 and 1.8
irb(main):004:0> " TEST “.gsub(/\s*$/, “”)
=> " TEST”

very odd (at least to me) in 1.8.0p3

irb(main):005:0> " TEST .".gsub(/\s*$/, “”)
=> " TEST . TEST ."

Where does this repeat come from? If I change the * to a + it fixes my
problem but I was hoping someone could help explain why it’s happening.

While looking into this I’ve noticed that there seems to be something
special about 2 repeats.

irb(main):002:0> " string “.gsub(/\s*$/, ‘P’)
” stringPP"

Regardless of how many trailing spaces I add two Ps are always
appended. It seems that one matches all the spaces and then one matches
the zero length string that’s the end itself since a string without
trailing spaces puts in one P. Is the $ getting used twice in this
match?

I’d appreciate any explanations or help.
thank you,
Paul

I don’t know what the problem is, but you should probably be using
gsub(/\s+$/,‘’) anyway :o)

-Kurt

···

On Tue, Jul 22, 2003 at 03:36:56AM +0900, Paul Rubel wrote:

Hello,
I recently downloaded ruby 1.8.0 p3, (2003-06-23) [i686-linux], and
tried it on some code that chopped trailing spaces from a string using
gsub(/\s*$/, “”). I’m seeing some odd behavior and was hoping someone
could shed some light on what’s happening.

In 1.6.7 the code above did what I expected. However, in 1.8.0p3 it
seems to double the string if the string doesn’t end in a space.

#does what I expected in 1.6 and 1.8
irb(main):004:0> " TEST “.gsub(/\s*$/, “”)
=> " TEST”

very odd (at least to me) in 1.8.0p3

irb(main):005:0> " TEST .“.gsub(/\s*$/, “”)
=> " TEST . TEST .”

Where does this repeat come from? If I change the * to a + it fixes my
problem but I was hoping someone could help explain why it’s happening.

While looking into this I’ve noticed that there seems to be something
special about 2 repeats.

irb(main):002:0> " string “.gsub(/\s*$/, ‘P’)
" stringPP”

Regardless of how many trailing spaces I add two Ps are always
appended. It seems that one matches all the spaces and then one matches
the zero length string that’s the end itself since a string without
trailing spaces puts in one P. Is the $ getting used twice in this
match?

I’d appreciate any explanations or help.
thank you,
Paul

======= End of Original Message =======<

# very odd (at least to me) in 1.8.0p3
irb(main):005:0> " TEST .".gsub(/\s*$/, "")
=> " TEST . TEST ."

Can you try this

svg% diff -u string.c~ string.c
--- string.c~ 2003-07-20 19:17:52.000000000 +0200
+++ string.c 2003-07-22 10:34:41.000000000 +0200
@@ -1643,6 +1643,7 @@
        bp += len;
        memcpy(bp, RSTRING(val)->ptr, RSTRING(val)->len);
        bp += RSTRING(val)->len;
+ offset = END(0);
        if (BEG(0) == END(0)) {
            /*
             * Always consume at least one character of the input string
@@ -1654,9 +1655,6 @@
            bp += len;
            offset = END(0) + len;
        }
- else {
- offset = END(0);
- }
        cp = RSTRING(str)->ptr + offset;
        if (offset > RSTRING(str)->len) break;
        beg = rb_reg_search(pat, str, offset, 0);
svg%

svg% ruby -e 'p " TEST .".gsub(/\s*$/, "")'
" TEST ."
svg%

irb(main):002:0> " string ".gsub(/\s*$/, 'P')
" stringPP"

this is normal

Guy Decoux

Hi,

very odd (at least to me) in 1.8.0p3

irb(main):005:0> " TEST .“.gsub(/\s*$/, “”)
=> " TEST . TEST .”

Can you try this

Thank you for the fix, Guy. And thank you for finding a bug, Paul.

irb(main):002:0> " string “.gsub(/\s*$/, ‘P’)
" stringPP”

this is normal

The first try replaces all trailing spaces with “P”, then second try
replaces empty at the end of string with “P”.

						matz.
···

In message “Re: gsub(/\s*$/, “”) doubling string” on 03/07/22, ts decoux@moulon.inra.fr writes:

Hello,

Yukihiro Matsumoto writes:

Hi,

very odd (at least to me) in 1.8.0p3

irb(main):005:0> " TEST .“.gsub(/\s*$/, “”)
=> " TEST . TEST .”

Can you try this

Thank you for the fix, Guy. And thank you for finding a bug, Paul.

Thank you for all your hard work and wonderful language.

irb(main):002:0> " string “.gsub(/\s*$/, ‘P’)
" stringPP”

this is normal

The first try replaces all trailing spaces with “P”, then second try
replaces empty at the end of string with “P”.

Ah ha. That makes sense. My least surprise is now the same as yours.
thank you,
Paul

···

In message “Re: gsub(/\s*$/, “”) doubling string” > on 03/07/22, ts decoux@moulon.inra.fr writes: