From 1e41069754ef69b02a9e0ed16a7a668cfab01dd0 Mon Sep 17 00:00:00 2001 From: akr Date: Fri, 25 Jan 2008 07:35:27 +0000 Subject: * include/ruby/intern.h (rb_str_buf_cat_ascii): declared. * string.c (rb_str_buf_cat_ascii): defined. * re.c (rb_reg_s_union): use rb_str_buf_cat_ascii to support ASCII incompatible encoding. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15232 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 9 +++++++++ include/ruby/intern.h | 1 + re.c | 5 ++++- string.c | 20 ++++++++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index ad8e9e5663..b422149fa8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +Fri Jan 25 16:31:47 2008 Tanaka Akira + + * include/ruby/intern.h (rb_str_buf_cat_ascii): declared. + + * string.c (rb_str_buf_cat_ascii): defined. + + * re.c (rb_reg_s_union): use rb_str_buf_cat_ascii to support ASCII + incompatible encoding. + Fri Jan 25 16:11:16 2008 Nobuyoshi Nakada * ruby.c (process_options, load_file, rb_load_file): propagates script diff --git a/include/ruby/intern.h b/include/ruby/intern.h index 1f531a71c5..e552c1f0e0 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -504,6 +504,7 @@ void rb_str_shared_replace(VALUE, VALUE); VALUE rb_str_buf_append(VALUE, VALUE); VALUE rb_str_buf_cat(VALUE, const char*, long); VALUE rb_str_buf_cat2(VALUE, const char*); +VALUE rb_str_buf_cat_ascii(VALUE, const char*); VALUE rb_obj_as_string(VALUE); VALUE rb_check_string_type(VALUE); VALUE rb_str_dup(VALUE); diff --git a/re.c b/re.c index dced068d98..a03bea5751 100644 --- a/re.c +++ b/re.c @@ -2668,7 +2668,7 @@ rb_reg_s_union(VALUE self, VALUE args0) VALUE e = rb_ary_entry(args0, i); if (0 < i) - rb_str_buf_cat2(source, "|"); /* xxx: UTF-16 */ + rb_str_buf_cat_ascii(source, "|"); v = rb_check_regexp_type(e); if (!NIL_P(v)) { @@ -2726,6 +2726,9 @@ rb_reg_s_union(VALUE self, VALUE args0) } } + if (i == 0) { + rb_enc_copy(source, v); + } rb_str_append(source, v); } diff --git a/string.c b/string.c index 3ac56b56c5..4b27066539 100644 --- a/string.c +++ b/string.c @@ -1116,6 +1116,26 @@ rb_str_cat2(VALUE str, const char *ptr) return rb_str_cat(str, ptr, strlen(ptr)); } +VALUE +rb_str_buf_cat_ascii(VALUE str, const char *ptr) +{ + rb_encoding *enc = rb_enc_get(str); + if (rb_enc_asciicompat(enc)) { + return rb_str_buf_cat(str, ptr, strlen(ptr)); + } + else { + char *buf = ALLOCA_N(char, rb_enc_mbmaxlen(enc)); + while (*ptr) { + int c = (unsigned char)*ptr; + int len = rb_enc_codelen(c, enc); + rb_enc_mbcput(c, buf, enc); + rb_str_buf_cat(str, buf, len); + ptr++; + } + return str; + } +} + static VALUE rb_enc_cr_str_buf_cat(VALUE str, const char *ptr, long len, int ptr_encindex, int ptr_cr, int *ptr_cr_ret) -- cgit v1.2.3