summaryrefslogtreecommitdiff
path: root/ext/stringio
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-06-22 08:23:30 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-06-22 08:23:30 +0000
commitfa31dda1f839909a96cdcc6339d4674e80173ac2 (patch)
tree7e4f65dc312f9faf645112c9604304ea79d16593 /ext/stringio
parentb205b5e7b62529435ad566c253ebd5cf9ade7033 (diff)
* ext/stringio/stringio.c (strio_each_codepoint): new method.
[ruby-core:23949] * ext/stringio/stringio.c (strio_each_codepoint): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@23818 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/stringio')
-rw-r--r--ext/stringio/stringio.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c
index 9bb5ed8..7514264 100644
--- a/ext/stringio/stringio.c
+++ b/ext/stringio/stringio.c
@@ -824,6 +824,37 @@ strio_each_char(VALUE self)
return self;
}
+/*
+ * call-seq:
+ * strio.each_codepoint {|c| block } -> strio
+ *
+ * See IO#each_codepoint.
+ */
+static VALUE
+strio_each_codepoint(VALUE self)
+{
+ struct StringIO *ptr;
+ rb_encoding *enc;
+ unsigned int c;
+ int n;
+
+ RETURN_ENUMERATOR(self, 0, 0);
+
+ ptr = readable(StringIO(self));
+ enc = rb_enc_get(ptr->string);
+ for (;;) {
+ if (ptr->pos >= RSTRING_LEN(ptr->string)) {
+ return self;
+ }
+
+ c = rb_enc_codepoint_len(RSTRING_PTR(ptr->string)+ptr->pos,
+ RSTRING_END(ptr->string), &n, enc);
+ rb_yield(UINT2NUM(c));
+ ptr->pos += n;
+ }
+ return self;
+}
+
/* Boyer-Moore search: copied from regex.c */
static void
bm_init_skip(long *skip, const char *pat, long m)
@@ -1359,6 +1390,8 @@ Init_stringio()
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_codepoint", strio_each_codepoint, 0);
+ rb_define_method(StringIO, "codepoints", strio_each_codepoint, 0);
rb_define_method(StringIO, "getc", strio_getc, 0);
rb_define_method(StringIO, "ungetc", strio_ungetc, 1);
rb_define_method(StringIO, "ungetbyte", strio_ungetbyte, 1);