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_each_char): New methods: String#chars and

#each_char.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@16001 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
knu 2008-04-14 05:01:30 +00:00
parent 9fb81662b5
commit 60dde833c3
3 changed files with 59 additions and 0 deletions

View file

@ -1,3 +1,8 @@
Mon Apr 14 13:58:32 2008 Akinori MUSHA <knu@iDaemons.org>
* string.c (rb_str_each_char): New methods: String#chars and
#each_char.
Mon Apr 14 13:42:20 2008 Akinori MUSHA <knu@iDaemons.org>
* string.c (rb_str_each_line, rb_str_each_byte): Reflect

8
NEWS
View file

@ -75,6 +75,8 @@ with all sufficient information, see the ChangeLog file.
* Regexp.union accepts an array of patterns.
* String#chars
* String#each_char
* String#partition
* String#rpartition
* String#start_with?
@ -83,6 +85,12 @@ with all sufficient information, see the ChangeLog file.
New methods. These are $KCODE aware unlike #index, #rindex and
#include?.
* String#each_byte
* String#each
* String#each_lines
Return an enumerator if no block is given.
* StopIteration
New exception class that causes Kernel#loop to stop iteration when

View file

@ -3812,6 +3812,50 @@ rb_str_each_byte(str)
}
/*
* Document-method: chars
* call-seq:
* str.chars => anEnumerator
* str.chars {|substr| block } => str
*
* Returns an enumerator that gives each character in the string.
* If a block is given, it iterates over each character in the string.
*
* "foo".chars.to_a #=> ["f","o","o"]
*/
/*
* Document-method: each_char
* call-seq:
* str.each_char {|cstr| block } => str
*
* Passes each character in <i>str</i> to the given block.
*
* "hello".each_char {|c| print c, ' ' }
*
* <em>produces:</em>
*
* h e l l o
*/
static VALUE
rb_str_each_char(VALUE str)
{
int i, len, n;
const char *ptr;
RETURN_ENUMERATOR(str, 0, 0);
str = rb_str_new4(str);
ptr = RSTRING(str)->ptr;
len = RSTRING(str)->len;
for (i = 0; i < len; i += n) {
n = mbclen(ptr[i]);
rb_yield(rb_str_substr(str, i, n));
}
return str;
}
/*
* call-seq:
* str.chop! => str or nil
@ -4950,9 +4994,11 @@ Init_String()
rb_define_method(rb_cString, "each_line", rb_str_each_line, -1);
rb_define_method(rb_cString, "each", rb_str_each_line, -1);
rb_define_method(rb_cString, "each_byte", rb_str_each_byte, 0);
rb_define_method(rb_cString, "each_char", rb_str_each_char, 0);
rb_define_method(rb_cString, "lines", rb_str_each_line, -1);
rb_define_method(rb_cString, "bytes", rb_str_each_byte, 0);
rb_define_method(rb_cString, "chars", rb_str_each_char, 0);
rb_define_method(rb_cString, "sum", rb_str_sum, -1);