Bug #592

String#rstrip sometimes strips NULLs, sometimes doesn't - encoding dependent

Added by Michael Selig over 6 years ago. Updated almost 4 years ago.

[ruby-core:18844]
Status:Closed
Priority:Normal
Assignee:-
ruby -v: Backport:

Description

=begin
ruby -ve 'p "abc \x00\x00".rstrip'
ruby 1.9.0 (2008-09-24 revision 19507) [i686-linux]
"abc"

but:
ruby -ve 'p "\u0635abc \x00\x00".rstrip'
ruby 1.9.0 (2008-09-24 revision 19507) [i686-linux]
"صabc \x00\x00"
=end

History

#1 Updated by Usaku NAKAMURA over 6 years ago

=begin
Hello,

In message " [Bug #592] String#rstrip sometimes strips NULLs, sometimes doesn't - encoding dependent"
on Sep.24,2008 17:04:11, redmine@ruby-lang.org wrote:

Bug #592: String#rstrip sometimes strips NULLs, sometimes doesn't - encoding dependent
ruby -ve 'p "\u0635abc \x00\x00".rstrip'
ruby 1.9.0 (2008-09-24 revision 19507) [i686-linux]
"?abc \x00\x00"

There is a patch.
Matz, is this ok?

Index: string.c
===================================================================
--- string.c (revision 19526)
+++ string.c (working copy)
@@ -5856,19 +5856,23 @@ rb_str_rstrip_bang(VALUE str)
char *s, *t, *e;

  enc = STR_ENC_GET(str);
  • if (rb_enc_dummy_p(enc)) {
  • rb_raise(rb_eEncCompatError, "incompatible encoding with this operation: %s", rb_enc_name(enc));
  • }
    s = RSTRING_PTR(str);
    if (!s || RSTRING_LEN(str) == 0) return Qnil;
    t = e = RSTRING_END(str);

  • /* remove trailing spaces or '\0's */
    if (single_byte_optimizable(str)) {

  • /* remove trailing spaces or '\0's /
    while (s < t && (
    (t-1) == '\0' || rb_enc_isspace(*(t-1), enc))) t--;
    }
    else {
    char *tp;

      while ((tp = rb_enc_prev_char(s, t, e, enc)) != NULL) {
    
  •  if (!rb_enc_isspace(rb_enc_codepoint(tp, e, enc), enc)) break;
    
  •  unsigned int c = rb_enc_codepoint(tp, e, enc);
    
  •  if (c && !rb_enc_isspace(c, enc)) break;
    t = tp;
    

    }
    }

    Regards,

    U.Nakamura usa@garbagecollect.jp

=end

#2 Updated by Yukihiro Matsumoto over 6 years ago

=begin
Hi,

In message "Re: Re: [Bug #592] String#rstrip sometimes strips NULLs, sometimes doesn't - encoding dependent"
on Wed, 24 Sep 2008 19:43:54 +0900, "U.Nakamura" usa@garbagecollect.jp writes:

|In message " [Bug #592] String#rstrip sometimes strips NULLs, sometimes doesn't - encoding dependent"
| on Sep.24,2008 17:04:11, redmine@ruby-lang.org wrote:
|> Bug #592: String#rstrip sometimes strips NULLs, sometimes doesn't - encoding dependent
|> ruby -ve 'p "\u0635abc \x00\x00".rstrip'
|> ruby 1.9.0 (2008-09-24 revision 19507) [i686-linux]
|> "?abc \x00\x00"
|
|There is a patch.
|Matz, is this ok?

I made almost identical patch. Please commit.

                        matz.

=end

#3 Updated by Usaku NAKAMURA over 6 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

=begin
Applied in changeset r19529.
=end

Also available in: Atom PDF