https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/assets/favicon-0e291875.ico2016-08-23T11:39:52ZRuby Issue Tracking SystemRuby - Feature #12694: Want a String method to remove heading substrhttps://bugs.ruby-lang.org/issues/12694?journal_id=602472016-08-23T11:39:52Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>IIRC, there were proposals of <code>lstrip</code> and <code>rstrip</code> enhancement.<br>
I'd expect them to accept char class(es), than a sub-string.</p>
<p>Note that <code>rb_str_index_m</code> accepts a <code>Regexp</code> too.<br>
Calling <code>str_strlen</code> on it will segfault.</p> Ruby - Feature #12694: Want a String method to remove heading substrhttps://bugs.ruby-lang.org/issues/12694?journal_id=602482016-08-23T12:01:43Zsonots (Naotoshi Seo)sonots@gmail.com
<ul></ul><p>Thanks, I found an issue which is 3 years old <a href="https://bugs.ruby-lang.org/issues/6842" class="external">https://bugs.ruby-lang.org/issues/6842</a></p> Ruby - Feature #12694: Want a String method to remove heading substrhttps://bugs.ruby-lang.org/issues/12694?journal_id=602542016-08-24T00:24:58Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul><li><strong>Is duplicate of</strong> <i><a class="issue tracker-2 status-2 priority-4 priority-default" href="https://bugs.ruby-lang.org/issues/6842">Feature #6842</a>: Add Optional Arguments to String#strip</i> added</li></ul> Ruby - Feature #12694: Want a String method to remove heading substrhttps://bugs.ruby-lang.org/issues/12694?journal_id=602592016-08-24T05:34:49Zsonots (Naotoshi Seo)sonots@gmail.com
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Rejected</i></li></ul><p>I will continue this issue on <a href="https://bugs.ruby-lang.org/issues/6842" class="external">https://bugs.ruby-lang.org/issues/6842</a>, let me close.</p>
<p>=> <a href="https://bugs.ruby-lang.org/issues/6842" class="external">https://bugs.ruby-lang.org/issues/6842</a> is too broad. Let us reopen this ticket, and talk about a way removing heading substr at this thread.</p> Ruby - Feature #12694: Want a String method to remove heading substrhttps://bugs.ruby-lang.org/issues/12694?journal_id=603882016-09-07T06:39:57Zsonots (Naotoshi Seo)sonots@gmail.com
<ul><li><strong>Status</strong> changed from <i>Rejected</i> to <i>Open</i></li></ul> Ruby - Feature #12694: Want a String method to remove heading substrhttps://bugs.ruby-lang.org/issues/12694?journal_id=603892016-09-07T06:42:02Zmrkn (Kenta Murata)muraken@gmail.com
<ul></ul><p>This method removes prefix string, so I propose <code>remove_prefix</code> or <code>deprefix</code> (means remove prefix).</p> Ruby - Feature #12694: Want a String method to remove heading substrhttps://bugs.ruby-lang.org/issues/12694?journal_id=603902016-09-07T06:42:34Zsonots (Naotoshi Seo)sonots@gmail.com
<ul></ul><p>We discussed about this ticket on ruby development meeting.</p>
<p>Python lstrip <a href="http://www.tutorialspoint.com/python/string_lstrip.htm" class="external">http://www.tutorialspoint.com/python/string_lstrip.htm</a> takes character classes as an argument, so providing a substring as an argument will introduce confusion.</p>
<p>We need to think of new naming. What about <code>String#lchomp</code> or <code>String#remove_prefix</code> or <code>String#head_chop</code> for example?</p> Ruby - Feature #12694: Want a String method to remove heading substrhttps://bugs.ruby-lang.org/issues/12694?journal_id=603912016-09-07T06:44:46Zsonots (Naotoshi Seo)sonots@gmail.com
<ul></ul><p>It seems python, perl does not have similar methods (which removes heading substr)</p> Ruby - Feature #12694: Want a String method to remove heading substrhttps://bugs.ruby-lang.org/issues/12694?journal_id=603922016-09-07T06:48:20Zsonots (Naotoshi Seo)sonots@gmail.com
<ul><li><strong>Description</strong> updated (<a title="View differences" href="https://bugs.ruby-lang.org/journals/60392/diff?detail_id=42375">diff</a>)</li></ul> Ruby - Feature #12694: Want a String method to remove heading substrhttps://bugs.ruby-lang.org/issues/12694?journal_id=603932016-09-07T06:50:29Zsonots (Naotoshi Seo)sonots@gmail.com
<ul><li><strong>Description</strong> updated (<a title="View differences" href="https://bugs.ruby-lang.org/journals/60393/diff?detail_id=42376">diff</a>)</li></ul> Ruby - Feature #12694: Want a String method to remove heading substrhttps://bugs.ruby-lang.org/issues/12694?journal_id=603952016-09-07T06:58:20Zsonots (Naotoshi Seo)sonots@gmail.com
<ul><li><strong>Description</strong> updated (<a title="View differences" href="https://bugs.ruby-lang.org/journals/60395/diff?detail_id=42378">diff</a>)</li></ul> Ruby - Feature #12694: Want a String method to remove heading substrhttps://bugs.ruby-lang.org/issues/12694?journal_id=604082016-09-07T07:51:06Zsonots (Naotoshi Seo)sonots@gmail.com
<ul></ul><p>PHP has ltrim and rtrim (which is an alias of chop), but they are for removing a character list, not removing a substring <a href="http://php.net/manual/en/function.ltrim.php" class="external">http://php.net/manual/en/function.ltrim.php</a></p>
<pre><code>ltrim('foaofe', 'foa'); #=> 'e'
</code></pre>
<p>Elixir has trim_leading, but it looks different with what we want to do <a href="http://elixir-lang.org/docs/stable/elixir/String.html#trim_leading/2" class="external">http://elixir-lang.org/docs/stable/elixir/String.html#trim_leading/2</a></p>
<pre><code>iex> String.trim_leading("__ abc _", "_")
" abc _"
iex> String.trim_leading("1 abc", "11")
"1 abc"
</code></pre>
<p>Golang has the same one with what we want to do <a href="https://golang.org/pkg/strings/#TrimPrefix" class="external">https://golang.org/pkg/strings/#TrimPrefix</a> and TrimSuffix</p>
<pre><code> var s = "Goodbye,, world!"
s = strings.TrimPrefix(s, "Goodbye,") #=> , world
</code></pre> Ruby - Feature #12694: Want a String method to remove heading substrhttps://bugs.ruby-lang.org/issues/12694?journal_id=604902016-09-13T07:10:03Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul></ul><p>Naotoshi Seo wrote:</p>
<blockquote>
<p>Python lstrip <a href="http://www.tutorialspoint.com/python/string_lstrip.htm" class="external">http://www.tutorialspoint.com/python/string_lstrip.htm</a> takes character classes as an argument, so providing a substring as an argument will introduce confusion.</p>
</blockquote>
<p>"character classes" may be confusing. "a list of characters" might be more precise. There are many classes of characters that can't be expressed easily with a list. In particular because Python lstrip doesn't accept hyphens to group characters.</p>
<p>One way to deal with this would be to allow a Regexp that selects a single character, and apply this repeatedly. But we don't have any such concept yet.</p> Ruby - Feature #12694: Want a String method to remove heading substrhttps://bugs.ruby-lang.org/issues/12694?journal_id=650862017-05-25T12:24:00Zsonots (Naotoshi Seo)sonots@gmail.com
<ul></ul><p>I've created a PR <a href="https://github.com/ruby/ruby/pull/1632" class="external">https://github.com/ruby/ruby/pull/1632</a> with a name String#<strong>remove_prefix</strong>.<br>
I will merge if the name is acceptable.</p> Ruby - Feature #12694: Want a String method to remove heading substrhttps://bugs.ruby-lang.org/issues/12694?journal_id=651952017-05-31T12:37:38Zznz (Kazuhiro NISHIYAMA)
<ul></ul><blockquote>
<p><code>str.sub(/^#{Regexp.escape(substr)}/, '') #=> 'def'</code></p>
</blockquote>
<p>I think <code>^</code> should be <code>\A</code> in description of this ticket. (and maybe in handle_tag_name_mixin.rb of fluentd too)</p>
<p>Which is expected?: first prefix of string only or prefix of each line</p> Ruby - Feature #12694: Want a String method to remove heading substrhttps://bugs.ruby-lang.org/issues/12694?journal_id=651962017-05-31T12:41:05Zsonots (Naotoshi Seo)sonots@gmail.com
<ul></ul><p>\A is correct. I updated the description, thanks.</p> Ruby - Feature #12694: Want a String method to remove heading substrhttps://bugs.ruby-lang.org/issues/12694?journal_id=651972017-05-31T12:46:12Zsonots (Naotoshi Seo)sonots@gmail.com
<ul><li><strong>Description</strong> updated (<a title="View differences" href="https://bugs.ruby-lang.org/journals/65197/diff?detail_id=45276">diff</a>)</li></ul> Ruby - Feature #12694: Want a String method to remove heading substrhttps://bugs.ruby-lang.org/issues/12694?journal_id=653892017-06-16T06:11:30Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>I'd pick the name <code>delete_prefix</code>.</p>
<p>Matz.</p> Ruby - Feature #12694: Want a String method to remove heading substrhttps://bugs.ruby-lang.org/issues/12694?journal_id=653902017-06-16T06:26:10Zsonots (Naotoshi Seo)sonots@gmail.com
<ul></ul><p>Thanks! I've changed the name to delete_prefix. <a href="https://github.com/ruby/ruby/pull/1632" class="external">https://github.com/ruby/ruby/pull/1632</a></p> Ruby - Feature #12694: Want a String method to remove heading substrhttps://bugs.ruby-lang.org/issues/12694?journal_id=654102017-06-16T14:14:33ZHanmac (Hans Mackowiak)hanmac@gmx.de
<ul></ul><p>sonots (Naotoshi Seo) wrote:</p>
<blockquote>
<p>Thanks! I've changed the name to delete_prefix. <a href="https://github.com/ruby/ruby/pull/1632" class="external">https://github.com/ruby/ruby/pull/1632</a></p>
</blockquote>
<p>do we need a delete_suffix too or is that done via another method?</p> Ruby - Feature #12694: Want a String method to remove heading substrhttps://bugs.ruby-lang.org/issues/12694?journal_id=654112017-06-16T14:23:38Zsonots (Naotoshi Seo)sonots@gmail.com
<ul></ul><p>We have String#chomp to delete suffix, but it would be nice to have the method for symmetry.<br>
I will create another thread to discuss about it.</p> Ruby - Feature #12694: Want a String method to remove heading substrhttps://bugs.ruby-lang.org/issues/12694?journal_id=654122017-06-16T14:31:04Zsonots (Naotoshi Seo)sonots@gmail.com
<ul></ul><p>I made a ticket for String#delete_suffix <a href="https://bugs.ruby-lang.org/issues/13665" class="external">https://bugs.ruby-lang.org/issues/13665</a></p> Ruby - Feature #12694: Want a String method to remove heading substrhttps://bugs.ruby-lang.org/issues/12694?journal_id=654342017-06-21T07:43:31Zsonots (Naotoshi Seo)sonots@gmail.com
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Applied in changeset trunk|r59132.</p>
<hr>
<p>string.c: add String#delete_prefix and String#delete_prefix!<br>
to remove leading substr [Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Want a String method to remove heading substr (Closed)" href="https://bugs.ruby-lang.org/issues/12694">#12694</a>] [fix GH-1632]</p>
<ul>
<li>
<p>string.c (rb_str_delete_prefix_bang): add a new method<br>
to remove prefix destuctively.</p>
</li>
<li>
<p>string.c (rb_str_delete_prefix): add a new method<br>
to remove prefix non-destuctively.</p>
</li>
<li>
<p>test/ruby/test_string.rb: add tests.</p>
</li>
</ul>