[前][次][番号順一覧][スレッド一覧]

ruby-changes:2802

From: ko1@a...
Date: 18 Dec 2007 17:04:51 +0900
Subject: [ruby-changes:2802] nobu - Ruby:r14293 (trunk): * transcode.c (rb_str_transcode_bang, rb_str_transcode): set new

nobu	2007-12-18 17:04:26 +0900 (Tue, 18 Dec 2007)

  New Revision: 14293

  Modified files:
    trunk/ChangeLog
    trunk/transcode.c

  Log:
    * transcode.c (rb_str_transcode_bang, rb_str_transcode): set new
      encoding even if no conversion is done because of 7bit only.
      [ruby-dev:32591]


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=14293&r2=14292
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/transcode.c?r1=14293&r2=14292

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 14292)
+++ ChangeLog	(revision 14293)
@@ -1,3 +1,9 @@
+Tue Dec 18 17:04:25 2007  Nobuyoshi Nakada  <nobu@r...>
+
+	* transcode.c (rb_str_transcode_bang, rb_str_transcode): set new
+	  encoding even if no conversion is done because of 7bit only.
+	  [ruby-dev:32591]
+
 Tue Dec 18 15:43:59 2007  Nobuyoshi Nakada  <nobu@r...>
 
 	* common.mk (encs, ext/ripper/ripper.c): other options must come
Index: transcode.c
===================================================================
--- transcode.c	(revision 14292)
+++ transcode.c	(revision 14293)
@@ -261,10 +261,11 @@
     return RSTRING_PTR(dest_string);
 }
 
-static VALUE
-str_transcode(int argc, VALUE *argv, VALUE str)
+static int
+str_transcode(int argc, VALUE *argv, VALUE *self)
 {
     VALUE dest;
+    VALUE str = *self;
     long blen, slen;
     char *buf, *bp, *sp, *fromp;
     rb_encoding *from_enc, *to_enc;
@@ -301,14 +302,15 @@
     }
 
     if (from_enc && from_enc == to_enc) {
-	return Qnil;
+	return -1;
     }
     if (from_enc && to_enc && rb_enc_asciicompat(from_enc) && rb_enc_asciicompat(to_enc)) {
-	if (ENC_CODERANGE(str) == ENC_CODERANGE_7BIT)
-	    return Qnil;
+	if (ENC_CODERANGE(str) == ENC_CODERANGE_7BIT) {
+	    return to_encidx;
+	}
     }
     if (strcasecmp(from_e, to_e) == 0) {
-	return Qnil;
+	return -1;
     }
 
     while (!final_encoding) /* loop for multistep transcoding */
@@ -333,8 +335,6 @@
 	*bp = '\0';
 	rb_str_set_len(dest, bp - buf);
 
-	rb_enc_associate(dest, to_enc);
-	
 	if (encoding_equal(my_transcoder->to_encoding, to_e)) {
 	    final_encoding = 1;
 	}
@@ -346,10 +346,10 @@
     /* set encoding */
     if (!to_enc) {
 	to_encidx = rb_enc_replicate(to_e, rb_default_encoding());
-	to_enc = rb_enc_from_index(to_encidx);
     }
+    *self = dest;
 
-    return dest;
+    return to_encidx;
 }
 
 /*
@@ -367,10 +367,12 @@
 static VALUE
 rb_str_transcode_bang(int argc, VALUE *argv, VALUE str)
 {
-    VALUE newstr = str_transcode(argc, argv, str);
-    if (NIL_P(newstr)) return str;
+    VALUE newstr = str;
+    int encidx = str_transcode(argc, argv, &newstr);
+
+    if (encidx < 0) return Qnil;
     rb_str_shared_replace(str, newstr);
-    rb_enc_copy(str, newstr);
+    rb_enc_associate_index(str, encidx);
     return str;
 }
 
@@ -387,10 +389,18 @@
 static VALUE
 rb_str_transcode(int argc, VALUE *argv, VALUE str)
 {
-    VALUE newstr = str_transcode(argc, argv, str);
-    if (NIL_P(newstr)) return rb_str_dup(str);
-    RBASIC(newstr)->klass = rb_obj_class(str);
-    OBJ_INFECT(newstr, str);
+    VALUE newstr = str;
+    int encidx = str_transcode(argc, argv, &newstr);
+
+    if (newstr == str) {
+	newstr = rb_str_new3(str);
+	if (encidx >= 0) rb_enc_associate_index(newstr, encidx);
+    }
+    else {
+	RBASIC(newstr)->klass = rb_obj_class(str);
+	OBJ_INFECT(newstr, str);
+	rb_enc_associate_index(newstr, encidx);
+    }
     return newstr;
 }
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml

[前][次][番号順一覧][スレッド一覧]