Project

General

Profile

Feature #11735 ยป 0001-Introduce-String-squish-and-String-squish.patch

Patch in git format-patch format - sikachu (Prem Sichanugrist), 11/24/2015 07:32 PM

View differences:

string.c
8803 8803
    return str;
8804 8804
}
8805 8805

  
8806
/*
8807
 *  call-seq:
8808
 *    str.squish! -> str
8809
 *
8810
 *  Performs a destructive squish. See String#squish.
8811
 *
8812
 *     str = " foo   bar    \n   \t   boo"
8813
 *     str.squish!                         # => "foo bar boo"
8814
 *     str                                 # => "foo bar boo"
8815
 */
8816
static VALUE
8817
rb_str_squish_bang(VALUE str)
8818
{
8819
    static const char before_regex_source[] = "\\A[[:space:]]+";
8820
    static const char after_regex_source[] = "[[:space:]]+\\z";
8821
    static const char between_regex_source[] = "[[:space:]]+";
8822
    VALUE before_argv[] = {
8823
        rb_reg_new(before_regex_source, sizeof before_regex_source - 1, 0),
8824
        rb_str_new_cstr("")
8825
    };
8826
    VALUE after_argv[] = {
8827
        rb_reg_new(after_regex_source, sizeof after_regex_source - 1, 0),
8828
        rb_str_new_cstr("")
8829
    };
8830
    VALUE between_argv[] = {
8831
        rb_reg_new(between_regex_source, sizeof between_regex_source - 1, 0),
8832
        rb_str_new_cstr(" ")
8833
    };
8834

  
8835
    rb_str_gsub_bang(2, before_argv, str);
8836
    rb_str_gsub_bang(2, after_argv, str);
8837
    rb_str_gsub_bang(2, between_argv, str);
8838
    return str;
8839
}
8840

  
8841
/*
8842
 *  call-seq:
8843
 *    str.squish -> str
8844
 *
8845
 *  Returns the string, first removing all whitespace on both ends of the
8846
 *  string, and then changing remaining consecutive whitespace groups into one
8847
 *  space each.
8848
 *
8849
 *  Note that it handles both ASCII and Unicode whitespace.
8850
 *
8851
 *     %{ Multi-line
8852
 *        string }.squish                   # => "Multi-line string"
8853
 *     " foo   bar    \n   \t   boo".squish # => "foo bar boo"
8854
 */
8855
static VALUE
8856
rb_str_squish(VALUE str)
8857
{
8858
    str = rb_str_dup(str);
8859
    return rb_str_squish_bang(str);
8860
}
8861

  
8806 8862
/**********************************************************************
8807 8863
 * Document-class: Symbol
8808 8864
 *
......
9328 9384
    rb_define_method(rb_cString, "strip", rb_str_strip, 0);
9329 9385
    rb_define_method(rb_cString, "lstrip", rb_str_lstrip, 0);
9330 9386
    rb_define_method(rb_cString, "rstrip", rb_str_rstrip, 0);
9387
    rb_define_method(rb_cString, "squish", rb_str_squish, 0);
9331 9388

  
9332 9389
    rb_define_method(rb_cString, "sub!", rb_str_sub_bang, -1);
9333 9390
    rb_define_method(rb_cString, "gsub!", rb_str_gsub_bang, -1);
......
9336 9393
    rb_define_method(rb_cString, "strip!", rb_str_strip_bang, 0);
9337 9394
    rb_define_method(rb_cString, "lstrip!", rb_str_lstrip_bang, 0);
9338 9395
    rb_define_method(rb_cString, "rstrip!", rb_str_rstrip_bang, 0);
9396
    rb_define_method(rb_cString, "squish!", rb_str_squish_bang, 0);
9339 9397

  
9340 9398
    rb_define_method(rb_cString, "tr", rb_str_tr, 2);
9341 9399
    rb_define_method(rb_cString, "tr_s", rb_str_tr_s, 2);
test/ruby/test_string.rb
2304 2304
    end;
2305 2305
  end if [0].pack("l!").bytesize < [nil].pack("p").bytesize
2306 2306
  # enable only when string size range is smaller than memory space
2307

  
2308
  def test_string_squish
2309
    original = %{\u205f\u3000 A string surrounded by various unicode spaces,
2310
      with tabs(\t\t), newlines(\n\n), unicode nextlines(\u0085\u0085) and many spaces(  ). \u00a0\u2007}
2311

  
2312
    expected = "A string surrounded by various unicode spaces, " +
2313
      "with tabs( ), newlines( ), unicode nextlines( ) and many spaces( )."
2314

  
2315
    # Make sure squish returns what we expect:
2316
    assert_equal expected, original.squish
2317
    # But doesn't modify the original string:
2318
    assert_not_equal expected, original
2319

  
2320
    # Make sure squish! returns what we expect:
2321
    assert_equal expected, original.squish!
2322
    # And changes the original string:
2323
    assert_equal expected, original
2324
  end
2307 2325
end
2308 2326

  
2309 2327
class TestString2 < TestString