Project

General

Profile

Bug #17467

What makes strip() and lstrip() not stripping \u0000 from beginning of the string?

Added by SouravGoswami (Sourav Goswami) 2 months ago. Updated 13 days ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux]
[ruby-core:101676]

Description

Both strip(), and rstrip() will delete \u0000 from the end of a string. But I have a string that has "\u0000hello\u0000".

Output from strip, lstrip and rstrip:

"\u0000hello\u0000".strip # => "\u0000hello"
"\u0000hello\u0000".lstrip # => "\u0000hello\u0000"
"\u0000hello\u0000".rstrip # => "\u0000hello"

Same goes for \x00 as well.

While it's quite ridiculous to have NULL at the beginning of the string, but what makes it not strip? Is that slow or is that because it's never used?

#1

Updated by SouravGoswami (Sourav Goswami) 2 months ago

  • Subject changed from What makes strip() and lstrip() not stripping \u0000? to What makes strip() and lstrip() not stripping \u0000 from beginning of the string?

Updated by jeremyevans0 (Jeremy Evans) 25 days ago

I agree that this is a bug. The documentation for strip and lstrip specifies that the whitespace should be stripped, and defines whitespace to include null. I have submitted a pull request to fix it: https://github.com/ruby/ruby/pull/4164

#3

Updated by jeremyevans (Jeremy Evans) 15 days ago

  • Status changed from Open to Closed

Applied in changeset git|cfd162d535c7a4f8b1f95255cc6be696a8b75557.


Make String#{strip,lstrip}{,!} strip leading NUL bytes

The documentation already specifies that they strip whitespace
and defines whitespace to include null.

This wraps the new behavior in the appropriate guards in the specs,
but does not specify behavior for previous versions, because this
is a bug that could be backported.

Fixes [Bug #17467]

#4

Updated by znz (Kazuhiro NISHIYAMA) 13 days ago

Japanese reference manual mentions treatment "\0".
(for example: https://docs.ruby-lang.org/ja/latest/method/String/i/strip.html )

If this will backport to released versions, it affects version branching in Japanese reference manual.

Which versions will have backports?

I have no opinion to backport or not.
I only want to know how to mention changed version in Japanese reference manual.

Also available in: Atom PDF