Project

General

Profile

Feature #15899 ยป 2269.diff

edd314159 (Edd Morgan), 07/09/2019 06:33 PM

View differences:

string.c
9845 9845
					RSTRING_LEN(str)-pos-RSTRING_LEN(sep)));
9846 9846
}
9847 9847

  
9848
/*
9849
 *  call-seq:
9850
 *     str.before(sep)             -> str or nil
9851
 *     str.before(regexp)          -> str or nil
9852
 *
9853
 *  Searches <i>sep</i> or pattern (<i>regexp</i>) in the string
9854
 *  and returns the part before it.
9855
 *  If it is not found, returns nil.
9856
 *
9857
 *     "hello".before("l")         #=> "he"
9858
 *     "hello".before(/ll/i)       #=> "he"
9859
 */
9860
VALUE
9861
rb_str_before(VALUE str, VALUE sep)
9862
{
9863
    if (RSTRING_LEN(str) == 0) {
9864
        failed: return Qnil;
9865
    }
9866

  
9867
    sep = get_pat_quoted(sep, 0);
9868

  
9869
    if (RB_TYPE_P(sep, T_REGEXP)) {
9870
        rb_reg_search(sep, str, 0, 0);
9871
        VALUE match = rb_backref_get();
9872

  
9873
        return rb_reg_match_pre(match);
9874
    }
9875

  
9876
    long pos = rb_str_index(str, sep, 0);
9877
    if (pos < 0) goto failed;
9878

  
9879
    return str_substr(str, 0, pos, TRUE);
9880
}
9881

  
9882
/*
9883
 *  call-seq:
9884
 *     str.after(sep)             -> str or nil
9885
 *     str.after(regexp)          -> str or nil
9886
 *
9887
 *  Searches <i>sep</i> or pattern (<i>regexp</i>) in the string
9888
 *  and returns the part after it.
9889
 *  If it is not found, returns nil.
9890
 *
9891
 *     "hello".after("l")         #=> "lo"
9892
 *     "hello".after(/ll/i)       #=> "o"
9893
 */
9894
VALUE
9895
rb_str_after(VALUE str, VALUE sep)
9896
{
9897
    long len = RSTRING_LEN(str);
9898

  
9899
    if (len == 0) {
9900
        failed: return Qnil;
9901
    }
9902

  
9903
    sep = get_pat_quoted(sep, 0);
9904

  
9905
    if (RB_TYPE_P(sep, T_REGEXP)) {
9906
        rb_reg_search(sep, str, 0, 0);
9907
        VALUE match = rb_backref_get();
9908

  
9909
        return rb_reg_match_post(match);
9910
    }
9911

  
9912
    long pos = rb_str_index(str, sep, 0);
9913
    if (pos < 0) goto failed;
9914

  
9915
    return str_substr(
9916
        str,
9917
        pos + RSTRING_LEN(sep),
9918
        len - pos,
9919
        TRUE
9920
    );
9921
}
9922

  
9848 9923
/*
9849 9924
 *  call-seq:
9850 9925
 *     str.start_with?([prefixes]+)   -> true or false
......
11293 11368
    rb_define_method(rb_cString, "partition", rb_str_partition, 1);
11294 11369
    rb_define_method(rb_cString, "rpartition", rb_str_rpartition, 1);
11295 11370

  
11371
    rb_define_method(rb_cString, "before", rb_str_before, 1);
11372
    rb_define_method(rb_cString, "after", rb_str_after, 1);
11373

  
11296 11374
    rb_define_method(rb_cString, "encoding", rb_obj_encoding, 0); /* in encoding.c */
11297 11375
    rb_define_method(rb_cString, "force_encoding", rb_str_force_encoding, 1);
11298 11376
    rb_define_method(rb_cString, "b", rb_str_b, 0);
test/ruby/test_string.rb
2627 2627
    assert_equal("hello", hello, bug)
2628 2628
  end
2629 2629

  
2630
  def test_before
2631
    assert_equal("hello", "hello world".before(" "))
2632
    assert_equal(nil, "onetwothree".before("four"))
2633
    assert_equal("he", "hello world".before("l"))
2634
    assert_equal("cat", "cats and dogs".before("s"))
2635

  
2636
    assert_equal("foo", "foobarbaz".before(/bar/))
2637
    assert_equal(nil, "cats and dogs".before(/pigs/))
2638
    assert_equal("he", "hello".before(/ll/))
2639
    assert_equal("", "hello".before(//))
2640

  
2641
    assert_equal(nil, "".before(""))
2642
  end
2643

  
2644
  def test_after
2645
    assert_equal("world", "hello world".after(" "))
2646
    assert_equal(nil, "onetwothree".after("four"))
2647
    assert_equal("lo world", "hello world".after("l"))
2648
    assert_equal(" and dogs", "cats and dogs".after("s"))
2649

  
2650
    assert_equal("baz", "foobarbaz".after(/bar/))
2651
    assert_equal(nil, "cats and dogs".after(/pigs/))
2652
    assert_equal("o", "hello".after(/ll/))
2653
    assert_equal("hello", "hello".after(//))
2654

  
2655
    assert_equal(nil, "".after(""))
2656
  end
2657

  
2630 2658
  def test_setter
2631 2659
    assert_raise(TypeError) { $/ = 1 }
2632 2660
    name = "\u{5206 884c}"