summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-05-27 09:55:14 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-05-27 09:55:14 +0000
commite661670f2c085f82e15c7cc767be60421d40459d (patch)
treed431cd57a9eb5679602d30554005294ba8d3fe9f
parente37b7b77a8dd4b984551fc4c913e84b133936cea (diff)
* ext/stringio/stringio.c (strio_each_char, Init_stringio): Add
StringIO#{each_char,chars}. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@16633 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--NEWS7
-rw-r--r--ext/stringio/stringio.c39
3 files changed, 50 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index dabf9831d2..730489c48b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Tue May 27 18:54:02 2008 Akinori MUSHA <knu@iDaemons.org>
+
+ * ext/stringio/stringio.c (strio_each_char, Init_stringio): Add
+ StringIO#{each_char,chars}.
+
Tue May 27 17:59:34 2008 Akinori MUSHA <knu@iDaemons.org>
* ext/stringio/stringio.c (strio_each): Return an enumerator if no
diff --git a/NEWS b/NEWS
index 3cc5e800c0..63b8ab26b3 100644
--- a/NEWS
+++ b/NEWS
@@ -331,7 +331,12 @@ with all sufficient information, see the ChangeLog file.
* StringIO#getbyte
* StringIO#readbyte
- Add new methods. (aliases for compatibility with 1.9)
+ New methods. (aliases for compatibility with 1.9)
+
+ * StringIO#each_char
+ * StringIO#chars
+
+ New methods.
* StringIO#each
* StringIO#each_line
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c
index 899a835af1..1121ce08f6 100644
--- a/ext/stringio/stringio.c
+++ b/ext/stringio/stringio.c
@@ -14,6 +14,7 @@
#include "ruby.h"
#include "rubyio.h"
+#include "re.h"
#if defined(HAVE_FCNTL_H) || defined(_WIN32)
#include <fcntl.h>
#elif defined(HAVE_SYS_FCNTL_H)
@@ -162,6 +163,7 @@ static VALUE strio_rewind _((VALUE));
static VALUE strio_seek _((int, VALUE *, VALUE));
static VALUE strio_get_sync _((VALUE));
static VALUE strio_each_byte _((VALUE));
+static VALUE strio_each_char _((VALUE));
static VALUE strio_getc _((VALUE));
static VALUE strio_ungetc _((VALUE, VALUE));
static VALUE strio_readchar _((VALUE));
@@ -806,6 +808,41 @@ strio_readchar(self)
return c;
}
+/*
+ * call-seq:
+ * strio.each_char {|char| block } -> strio
+ *
+ * See IO#each_char.
+ */
+static VALUE
+strio_each_char(self)
+ VALUE self;
+{
+ struct StringIO *sio;
+ VALUE str;
+ const char *ptr;
+ size_t len;
+
+ RETURN_ENUMERATOR(self, 0, 0);
+
+ sio = readable(StringIO(self));
+ str = sio->string;
+ ptr = RSTRING_PTR(str);
+ len = RSTRING_LEN(str);
+
+ while (sio->pos < len) {
+ int pos = sio->pos;
+ char c = ptr[pos];
+ int n = mbclen(c);
+
+ if (len < pos + n) n = len - pos;
+
+ sio->pos += n;
+ rb_yield(rb_str_substr(str, pos, n));
+ }
+ return self;
+}
+
static void
bm_init_skip(skip, pat, m)
long *skip;
@@ -1301,6 +1338,8 @@ Init_stringio()
rb_define_method(StringIO, "each", strio_each, -1);
rb_define_method(StringIO, "each_byte", strio_each_byte, 0);
rb_define_method(StringIO, "bytes", strio_each_byte, 0);
+ rb_define_method(StringIO, "each_char", strio_each_char, 0);
+ rb_define_method(StringIO, "chars", strio_each_char, 0);
rb_define_method(StringIO, "each_line", strio_each, -1);
rb_define_method(StringIO, "lines", strio_each, -1);
rb_define_method(StringIO, "getc", strio_getc, 0);