Forum: Ruby-dev [Bug #981] String#encode() keeps invalid bytes when converting to US-ASCII

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C4e88907313843cf07f6d85ba8162120?d=identicon&s=25 Martin Dürst (Guest)
on 2009-01-05 02:05
(Received via mailing list)
Bug #981: String#encode() keeps invalid bytes when converting to
US-ASCII
http://redmine.ruby-lang.org/issues/show/981

$B5/I<<T(B: Martin D$(D+d(Brst
$B%9%F!<%?%9(B: Open, $BM%@hEY(B: Normal
$BC4Ev<T(B: Akira Tanaka

String#encode() ($BJQ49$N;XDj$J$7!"(Bdefault_internal $B$KJQ49(B)
$B$O(B
default_internal $B$,(B US-ASCII $B$N>l9g$@$1(B invalid
$B$J%P%$%H$d(B
unknown $B$JJ8;z$r$=$N$^$^%3%T!<$7$^$9!#B>$N%(%s%3!<%G%#%s%0(B
$B$G$O(B '?' $B$J$I$K%l%W%l!<%9$5$l$^$9!#(B

$B<B9TNc(B:

> ruby -E:ascii-8bit -e'puts "\uabcd'.encode.inspect'
"?"

> ruby -E:euc-jp -e'puts "\uabcd'.encode.inspect'
"?"

> ruby -E:us-ascii -e'puts "\uabcd'.encode.inspect'
"\xEA\xAF\x8D"

$B0U?^E*$J$b$N$G$"$k$H$O$I$&$7$F$b;W$$$^$;$s$,!"$b$7$=$&(B
$B$G$7$?$i(B rdoc $B$G=q$$$?J}$,$$$$$+$H;W$$$^$9!#(B

([ruby-dev:37662] $B;2>H(B)

$B$h$m$7$/$*4j$$$7$^$9!#(B    Martin.
C4e88907313843cf07f6d85ba8162120?d=identicon&s=25 Yui NARUSE (Guest)
on 2009-01-15 06:16
(Received via mailing list)
$B%A%1%C%H(B #981 $B$,99?7$5$l$^$7$?!#(B (by Yui NARUSE)

$BC4Ev<T(B Akira Tanaka$B$+$i(BYukihiro Matsumoto$B$KJQ99(B

$B$3$N5sF0$O(B String#encode
$B$,D>@\$N860x$G$O$J$/!"(BEncoding.default_internal
$BB&$,860x$G$9!#(B
% ruby19 -E:us-ascii -e'p Encoding.default_internal'
#<Encoding:UTF-8>

enc_set_default_encoding $B$r8+$k$H!"(B
if (def->index == ENCINDEX_US_ASCII)
    def->index = defindex;
$B$H!"(BUS-ASCII $B$N>l9g$K%G%U%)%k%H$N%(%s%3!<%G%#%s%0(B
(default_internal $B$G$O(B UTF-8) $B$,%;%C%H$5$l$F$$$^$9!#(B
$B8=:_$N;EMM$G$O$3$NJU$N=hM}$OITMW$J5$$,$9$k$N$G!"0J2<$NDL$j$H$+!#(B

Index: encoding.c
===================================================================
--- encoding.c  (revision 21519)
+++ encoding.c  (working copy)
@@ -1051,7 +1051,7 @@ struct default_encoding {

 static int
 enc_set_default_encoding(struct default_encoding *def, VALUE encoding,
-                        const char *name, int defindex)
+                        const char *name)
 {
     int overridden = Qfalse;
     if (def->index != -2)
@@ -1066,8 +1066,6 @@ enc_set_default_encoding(struct default_encoding *
     }
     else {
        def->index = rb_enc_to_index(rb_to_encoding(encoding));
-       if (def->index == ENCINDEX_US_ASCII)
-           def->index = defindex;
        def->enc = 0;
        enc_alias_internal(name, def->index);
     }
@@ -1118,7 +1116,7 @@ rb_enc_set_default_external(VALUE encoding)
         rb_raise(rb_eArgError, "default external can not be nil");
     }
     enc_set_default_encoding(&default_external, encoding,
-                            "external", ENCINDEX_US_ASCII);
+                            "external");
 }

 /*
@@ -1171,7 +1169,7 @@ void
 rb_enc_set_default_internal(VALUE encoding)
 {
     enc_set_default_encoding(&default_internal, encoding,
-                            "internal", ENCINDEX_UTF_8);
+                            "internal");
 }

 /*


----------------------------------------
http://redmine.ruby-lang.org/issues/show/981
C4e88907313843cf07f6d85ba8162120?d=identicon&s=25 Yukihiro Matsumoto (Guest)
on 2009-01-15 08:07
(Received via mailing list)
$B%A%1%C%H(B #981 $B$,99?7$5$l$^$7$?!#(B (by Yukihiro Matsumoto)

$B%9%F!<%?%9(B Open$B$+$i(BClosed$B$KJQ99(B
$B?JD=(B % 0$B$+$i(B100$B$KJQ99(B

Applied in changeset r21528.
----------------------------------------
http://redmine.ruby-lang.org/issues/show/981
This topic is locked and can not be replied to.