summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-10-02 16:39:21 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-10-02 16:39:21 +0000
commit4392e94d96e5278390c49f3fd1c1110dc33a3938 (patch)
treef8fdfbf1daf2fbf64d4abbbed9fb160df7740b2b /string.c
parent27841c008e0d22c4ebaed88af90f850c931da49d (diff)
* string.c (rb_str_lines): returns an Enumerator instead of an
array of lines. * string.c (rb_str_bytes): a new method. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11067 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r--string.c43
1 files changed, 20 insertions, 23 deletions
diff --git a/string.c b/string.c
index e8468b0791..50a83e49fa 100644
--- a/string.c
+++ b/string.c
@@ -3550,37 +3550,33 @@ rb_str_split(VALUE str, const char *sep0)
/*
* call-seq:
- * str.lines => anArray
+ * str.lines => anEnumerator
*
- * Divides <i>str</i> into lines terminated by newlines.
+ * Returns an enumerator that gives each line in the string.
*
- * "foo\nbar\n".lines #=> ["foo\n", "bar\n"]
- * "foo\nb ar".lines #=> ["foo\n", "b ar"]
+ * "foo\nbar\n".lines.to_a #=> ["foo\n", "bar\n"]
+ * "foo\nb ar".lines.sort #=> ["b ar", "foo\n"]
*/
static VALUE
rb_str_lines(VALUE str)
{
- VALUE ary = rb_ary_new();
- char *p = RSTRING_PTR(str);
- char *pend = p + RSTRING_LEN(str);
- char *s = p;
- VALUE line;
+ return rb_enumeratorize(str, ID2SYM(rb_intern("each_line")), 0, 0);
+}
- while (p < pend) {
- if (*p == '\n') {
- p++;
- line = rb_str_new(s, p-s);
- rb_ary_push(ary, line);
- s = p;
- }
- p++;
- }
- if (p == pend && s < p) {
- line = rb_str_new(s, p-s);
- rb_ary_push(ary, line);
- }
- return ary;
+/*
+ * call-seq:
+ * str.bytes => anEnumerator
+ *
+ * Returns an enumerator that gives each byte in the string.
+ *
+ * "hello".bytes.to_a #=> [104, 101, 108, 108, 111]
+ */
+
+static VALUE
+rb_str_bytes(VALUE str)
+{
+ return rb_enumeratorize(str, ID2SYM(rb_intern("each_byte")), 0, 0);
}
/*
@@ -4889,6 +4885,7 @@ Init_String(void)
rb_define_method(rb_cString, "oct", rb_str_oct, 0);
rb_define_method(rb_cString, "split", rb_str_split_m, -1);
rb_define_method(rb_cString, "lines", rb_str_lines, 0);
+ rb_define_method(rb_cString, "bytes", rb_str_bytes, 0);
rb_define_method(rb_cString, "reverse", rb_str_reverse, 0);
rb_define_method(rb_cString, "reverse!", rb_str_reverse_bang, 0);
rb_define_method(rb_cString, "concat", rb_str_concat, 1);