diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2006-09-21 08:47:03 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2006-09-21 08:47:03 +0000 |
commit | f5ec2b0d75e1f29705aa6b04fdf9e03791b99997 (patch) | |
tree | 5ac497a629839bd4035dfea2d82aa4a16f47f86c /string.c | |
parent | ee9bae8cd7b058a678ec0d0b633ecfb6df3da344 (diff) | |
download | ruby-f5ec2b0d75e1f29705aa6b04fdf9e03791b99997.tar.gz |
* 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
Diffstat (limited to 'string.c')
-rw-r--r-- | string.c | 49 |
1 files changed, 48 insertions, 1 deletions
@@ -3118,7 +3118,7 @@ tr_setup_table(VALUE str, char table[256], int init) /* * 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 * <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))); } +/* + * 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 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, "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); |