1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* string.c (rb_str_startwith): a new method to check if a string

starts with given prefix.

* string.c (rb_str_endwith): the opposite of String#startwith?.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10984 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2006-09-21 08:47:03 +00:00
parent ee9bae8cd7
commit f5ec2b0d75
2 changed files with 55 additions and 1 deletions

View file

@ -1,3 +1,10 @@
Thu Sep 21 17:44:49 2006 Yukihiro Matsumoto <matz@ruby-lang.org>
* string.c (rb_str_startwith): a new method to check if a string
starts with given prefix.
* string.c (rb_str_endwith): the opposite of String#startwith?.
Thu Sep 21 16:29:02 2006 WATANABE Hirofumi <eban@ruby-lang.org> Thu Sep 21 16:29:02 2006 WATANABE Hirofumi <eban@ruby-lang.org>
* rubytest.rb: use each_line instead of each. * rubytest.rb: use each_line instead of each.

View file

@ -3118,7 +3118,7 @@ tr_setup_table(VALUE str, char table[256], int init)
/* /*
* call-seq: * call-seq:
* str.delete!([other_str]+>) => str or nil * str.delete!([other_str]+) => str or nil
* *
* Performs a <code>delete</code> operation in place, returning <i>str</i>, or * Performs a <code>delete</code> operation in place, returning <i>str</i>, or
* <code>nil</code> if <i>str</i> was not modified. * <code>nil</code> if <i>str</i> was not modified.
@ -4498,6 +4498,51 @@ rb_str_rpartition(VALUE str, VALUE sep)
RSTRING_LEN(str)-pos-RSTRING_LEN(sep))); RSTRING_LEN(str)-pos-RSTRING_LEN(sep)));
} }
/*
* call-seq:
* str.startwith?([prefix]+) => true or false
*
* Returns true if <i>str</i> starts with the prefix given.
*/
static VALUE
rb_str_startwith(int argc, VALUE *argv, VALUE str)
{
int i;
for (i=0; i<argc; i++) {
VALUE tmp = rb_check_string_type(argv[i]);
if (NIL_P(tmp)) continue;
if (RSTRING_LEN(str) < RSTRING_LEN(tmp)) continue;
if (rb_memcmp(RSTRING_PTR(str), RSTRING_PTR(tmp), RSTRING_LEN(tmp)) == 0)
return Qtrue;
}
return Qfalse;
}
/*
* call-seq:
* str.endwith?([suffix]+) => true or false
*
* Returns true if <i>str</i> ends with the suffix given.
*/
static VALUE
rb_str_endwith(int argc, VALUE *argv, VALUE str)
{
int i;
for (i=0; i<argc; i++) {
VALUE tmp = rb_check_string_type(argv[i]);
if (NIL_P(tmp)) continue;
if (RSTRING_LEN(str) < RSTRING_LEN(tmp)) continue;
if (rb_memcmp(RSTRING_PTR(str) + RSTRING_LEN(str) - RSTRING_LEN(tmp),
RSTRING_PTR(tmp), RSTRING_LEN(tmp)) == 0)
return Qtrue;
}
return Qfalse;
}
void void
rb_str_setter(VALUE val, ID id, VALUE *var) rb_str_setter(VALUE val, ID id, VALUE *var)
{ {
@ -4842,6 +4887,8 @@ Init_String(void)
rb_define_method(rb_cString, "ord", rb_str_ord, 0); rb_define_method(rb_cString, "ord", rb_str_ord, 0);
rb_define_method(rb_cString, "include?", rb_str_include, 1); rb_define_method(rb_cString, "include?", rb_str_include, 1);
rb_define_method(rb_cString, "startwith?", rb_str_startwith, -1);
rb_define_method(rb_cString, "endwith?", rb_str_endwith, -1);
rb_define_method(rb_cString, "scan", rb_str_scan, 1); rb_define_method(rb_cString, "scan", rb_str_scan, 1);