summaryrefslogtreecommitdiff
path: root/io.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-09-15 00:12:05 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-09-15 00:12:05 +0000
commit61ee785c61da48a23112727e763bd465a81a37ba (patch)
treef79f270f0a84ccb68c419553c86ef2b3316e5753 /io.c
parent3f4a73f0e1d79184c51d3d696ec06376c323e0b5 (diff)
* io.c (rb_io_puts): fix for wide char encoding strings.
[ruby-dev:42212] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29256 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'io.c')
-rw-r--r--io.c18
1 files changed, 17 insertions, 1 deletions
diff --git a/io.c b/io.c
index 7a4be8a447..2a5a6452f7 100644
--- a/io.c
+++ b/io.c
@@ -6074,6 +6074,22 @@ rb_f_putc(VALUE recv, VALUE ch)
return rb_funcall2(rb_stdout, rb_intern("putc"), 1, &ch);
}
+
+static int
+str_end_with_asciichar(VALUE str, int c)
+{
+ long len = RSTRING_LEN(str);
+ const char *ptr = RSTRING_PTR(str);
+ rb_encoding *enc = rb_enc_from_index(ENCODING_GET(str));
+ int n;
+
+ if (len == 0) return 0;
+ if ((n = rb_enc_mbminlen(enc)) == 1) {
+ return ptr[len - 1] == c;
+ }
+ return rb_enc_ascget(ptr + ((len - 1) / n) * n, ptr + len, &n, enc) == c;
+}
+
static VALUE
io_puts_ary(VALUE ary, VALUE out, int recur)
{
@@ -6137,7 +6153,7 @@ rb_io_puts(int argc, VALUE *argv, VALUE out)
string:
rb_io_write(out, line);
if (RSTRING_LEN(line) == 0 ||
- RSTRING_PTR(line)[RSTRING_LEN(line)-1] != '\n') {
+ !str_end_with_asciichar(line, '\n')) {
rb_io_write(out, rb_default_rs);
}
}