# \--encoding affects script encoding

**URL:** <https://www.ruby-forum.com/t/encoding-affects-script-encoding/130670>\
**Category:** Development\
**Created:** [March 1, 2008, 4:31pm UTC](https://www.ruby-forum.com/t/encoding-affects-script-encoding/130670 "2008-03-01T16:31:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sheepman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@sheepman](https://www.ruby-forum.com/u/sheepman)\
**Post date:** [March 1, 2008, 4:31pm UTC](https://www.ruby-forum.com/t/encoding-affects-script-encoding/130670/1 "2008-03-01T16:31:49Z")

</div>

e$B$3$s$P$s$Oe(B sheepman e$B$G$9!#e(B

e$B0J2\<$N$h$&$Ke(B–encoding e$B$Ke(B utf-16be  
e$B$r;XDj$9$k$H%(%i!\<$bI=\<($5$l$:$Ke(B  
e$B=\*N;$7$^$9!#e(B

$ cat e.rb  
p **ENCODING**

$ ruby-1.9 -v --encoding=utf-16be e.rb  
ruby 1.9.0 (2008-03-01 revision 15239) [i686-linux]  
(e$B2?$bI=\<($5$l$:$K=\*N;e(B)

shebang e$B$r8!::$9$kA0$K0lC6e(B IO e$B%(%s%3!\<%G%#%s%0$re(B  
e$B%;%C%H$7$F$\*$/$Y$-e(B  
e$B$J$s$@$H;W$$$^$9!#e(B

# $ svn diff ruby.c Index: ruby.c

— ruby.c (e$B%j%S%8%g%se(B 15664)  
+++ ruby.c (e$B:n6H%3%T!\<e(B)  
@@ -1155,6 +1155,9 @@ load\_file(VALUE parser, const char \*fnam  
int no\_src\_enc = !opt-\>src.enc.name;  
int no\_ext\_enc = !opt-\>ext.enc.name;

- 

```
  enc = rb_usascii_encoding();

```

- 

```
  rb_funcall(f, rb_intern("set_encoding"), 1, 

```

rb\_enc\_from\_encoding(enc));  
+  
if (opt-\>xflag) {  
forbid\_setid("-x");  
opt-\>xflag = Qfalse;

---

<div class="post-metadata">

**Author:** ![sheepman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@sheepman](https://www.ruby-forum.com/u/sheepman)\
**Post date:** [March 2, 2008, 4:22pm UTC](https://www.ruby-forum.com/t/encoding-affects-script-encoding/130670/2 "2008-03-02T16:22:17Z")

</div>

e$B$J$+$@$G$9!#e(B

At Sun, 2 Mar 2008 00:31:01 +0900,  
sheepman wrote in [ruby-dev:33955]:

> shebang e$B$r8!::$9$kA0$K0lC6e(B IO e$B%(%s%3!\<%G%#%s%0$re(B e$B%;%C%H$7$F$\*$/$Y$-e(B  
> e$B$J$s$@$H;W$$$^$9!#e(B

e$B%3%\_%C%H$7$^$7$?$,!"e(BIO#ungetce$B$G$N%(%s%3!\<%G%#%s%0$N07$$$,H4$1$Fe(B  
e$B$$$k$3$H$K5$$E$-$^$7$?!#e(B

# Index: io.c

— io.c (revision 15671)  
+++ io.c (working copy)  
@@ -2430,4 +2430,5 @@ VALUE  
rb\_io\_ungetc(VALUE io, VALUE c)  
{

- rb\_encoding \*enc;  
rb\_io\_t \*fptr;

@@ -2437,11 +2438,15 @@ rb\_io\_ungetc(VALUE io, VALUE c)  
if (FIXNUM\_P©) {  
int cc = FIX2INT©;

- rb\_encoding \*enc = io\_read\_encoding(fptr);  
char buf[16];

- c = rb\_str\_new(buf, rb\_enc\_mbcput(cc, buf, enc));

- c = rb\_str\_new(buf, rb\_enc\_mbcput(cc, buf, io\_read\_encoding(fptr)));
- enc = fptr-\>enc2;  
}  
else {  
SafeStringValue©;
- enc = io\_input\_encoding(fptr);
- }
- if (enc && enc != rb\_enc\_get©) {
- c = rb\_funcall(c, id\_encode, 1, rb\_enc\_from\_encoding(enc));  
}  
io\_ungetc(c, fptr);

---

<div class="post-metadata">

**Author:** ![sheepman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@sheepman](https://www.ruby-forum.com/u/sheepman)\
**Post date:** [March 2, 2008, 4:55pm UTC](https://www.ruby-forum.com/t/encoding-affects-script-encoding/130670/3 "2008-03-02T16:55:52Z")

</div>

In article [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
Nobuyoshi N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> e$B%3%\_%C%H$7$^$7$?$,!"e(BIO#ungetce$B$G$N%(%s%3!\<%G%#%s%0$N07$$$,H4$1$Fe(B  
> e$B$$$k$3$H$K5$$E$-$^$7$?!#e(B

> ```
> else {
> 
> ```
> 
> SafeStringValue(c);
> 
> - enc = io\_input\_encoding(fptr);
> - }
> - if (enc && enc != rb\_enc\_get(c)) {
> - c = rb\_funcall(c, id\_encode, 1, rb\_enc\_from\_encoding(enc));  
> }  
> io\_ungetc(c, fptr);

encdetect e$B$r\<BAu$9$k$K$O!“e(BIO e$B$+$i%P%$%HNs$GFI$\_=P$7$?$b$N$re(B  
e$B%P%$%HNs$H$7$Fe(B ungetc e$B$9$k$3$H$,I,MW$J$s$G$9$,!”$=$l$O$G$-$ke(B  
e$B$G$7$g$&$+!#e(B

e$BJL%a%=%C%I$+$J$!e(B?

---

<div class="post-metadata">

**Author:** ![sheepman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@sheepman](https://www.ruby-forum.com/u/sheepman)\
**Post date:** [March 3, 2008, 7:57am UTC](https://www.ruby-forum.com/t/encoding-affects-script-encoding/130670/4 "2008-03-03T07:57:05Z")

</div>

e$B$J$+$@$G$9!#e(B

At Mon, 3 Mar 2008 00:54:33 +0900,  
Tanaka A. wrote in [ruby-dev:33960]:

> > ```
> > io_ungetc(c, fptr);
> > 
> > ```
> 
> encdetect e$B$r\<BAu$9$k$K$O!“e(BIO e$B$+$i%P%$%HNs$GFI$\_=P$7$?$b$N$re(B  
> e$B%P%$%HNs$H$7$Fe(B ungetc e$B$9$k$3$H$,I,MW$J$s$G$9$,!”$=$l$O$G$-$ke(B  
> e$B$G$7$g$&$+!#e(B

e$B$=$l$O8=\>u$G$b$G$-$J$$$G$9$M!#e(B?xe$B$be(BStringe$B$K$J$C$?$3$H$G$b$"$k$7!“e(B  
0…255e$B$N@0?t$NJB$S$O%P%$%HNs$H$7$FLa$9!”$H$$$&$N$G$b$$$$$+$b!#e(B

> e$BJL%a%=%C%I$+$J$!e(B?

getce$B$He(Bgetbytee$B$bJ,$1$?$3$H$r9M$($l$P!"$3$C$A$G$9$+$M$’!#e(B  
ungetbytee$B$H$$$&$N$O$$$^$$$A$J5$$,$7$^$9$,!#e(B

# Index: io.c

— io.c (revision 15671)  
+++ io.c (working copy)  
@@ -312,9 +312,7 @@ io\_unread(rb\_io\_t \*fptr)  
}

-static void  
-io\_ungetc(VALUE str, rb\_io\_t \*fptr)  
+static char \*  
+io\_unget\_space(long len, rb\_io\_t \*fptr)  
{

- int len = RSTRING\_LEN(str);
- if (fptr-\>rbuf == NULL) {  
fptr-\>rbuf\_off = 0;  
@@ -337,5 +335,11 @@ io\_ungetc(VALUE str, rb\_io\_t \*fptr)  
fptr-\>rbuf\_off-=len;  
fptr-\>rbuf\_len+=len;
- MEMMOVE(fptr-\>rbuf+fptr-\>rbuf\_off, RSTRING\_PTR(str), char, len);

- return fptr-\>rbuf+fptr-\>rbuf\_off;  
+}
- 

+static void  
+io\_ungetc(const char \*ptr, long len, rb\_io\_t \*fptr)  
+{

- MEMMOVE(io\_unget\_space(len, fptr), ptr, char, len);  
}

@@ -680,10 +684,10 @@ io\_fwrite(VALUE str, rb\_io\_t _fptr)  
/_ Can’t use encode! because puts writes a frozen newline \*/  
if (fptr-\>enc2) {

- 

```
 str = rb_funcall(str, id_encode, 2,

```

- 

```
 str = rb_funcall(str, id_encode, 2,
      rb_enc_from_encoding(fptr->enc2),
      rb_enc_from_encoding(fptr->enc));

```

}  
else {

- 

```
 str = rb_funcall(str, id_encode, 1,

```

- 

```
 str = rb_funcall(str, id_encode, 1,
      rb_enc_from_encoding(fptr->enc));

```

}  
@@ -1348,5 +1352,5 @@ io\_enc\_str(VALUE str, rb\_io\_t _fptr)  
/_ two encodings, so transcode from enc2 to enc _/  
/_ the methods in transcode.c are static, so call indirectly \*/

- str = rb\_funcall(str, id\_encode, 2,

- str = rb\_funcall(str, id\_encode, 2,  
rb\_enc\_from\_encoding(fptr-\>enc),  
rb\_enc\_from\_encoding(fptr-\>enc2));  
@@ -1896,5 +1900,5 @@ prepare\_getline\_args(int argc, VALUE \*ar  
if (fptr-\>enc2) {  
VALUE rs2;

- 

```
 rs2 = rb_funcall(rs, id_encode, 2,

```

- 

```
 rs2 = rb_funcall(rs, id_encode, 2,
     rb_enc_from_encoding(fptr->enc2),
     rb_enc_from_encoding(fptr->enc));

```

@@ -2414,10 +2418,11 @@ rb\_io\_readbyte(VALUE io)

- call-seq:
- 

```
ios.ungetc(string) => nil

```

- 
  - 

```
ios.ungetc(byte) => nil

```

- 
  - 

```
ios.ungetc(byte_ary) => nil

```

  - 

- 
  - Pushes back one character (passed as a parameter) onto  
_ios_,

- 
  - such that a subsequent buffered read will return it. Only one  
character

- 
  - may be pushed back before a subsequent read operation (that is,

- 
  - you will be able to read only the last of several characters that  
have been pushed

- 
  - back). Has no effect with unbuffered reads (such as  
`IO#sysread`).

- 
  - Pushes back character string or byte sequence (passed as a

- 
  - parameter) onto _ios_, such that a subsequent buffered read

- 
  - will return it. Has no effect with unbuffered reads (such as

- 
  - `IO#sysread`).
  - 
  - 

```
f = File.new("testfile") #=> #<File:testfile>

```

@@ -2425,25 +2430,89 @@ rb\_io\_readbyte(VALUE io)

- 

```
f.ungetc(c) #=> nil

```

- 

```
f.getc #=> "8"

```

- 
  - 

```
f.ungetc(0x39, 10) #=> nil

```

- 
  - 

```
f.gets #=> "9\n"

```

\*/

VALUE  
-rb\_io\_ungetc(VALUE io, VALUE c)  
+rb\_io\_unget(int argc, VALUE \*argv, VALUE io)  
{  
rb\_io\_t \*fptr;

- VALUE c;

- if (argc \< 1) return Qnil;

- if (argc \> 1) { /\* expects byte array \*/

- /\* use temporary buffer to get rid of race condition \*/

- volatile VALUE str = rb\_str\_tmp\_new(argc);

- char \*ptr = RSTRING\_PTR(str);

- int i;

- 
- for (i = 0; i \< argc; ++i) {

- 

```
 c = argv[i];

```

- 

```
 ptr[i] = (char)NUM2INT(c);

```

- }

- io\_ungetc(ptr, argc, fptr);

- rb\_str\_resize(str, 0);

- return Qnil;

- }

- c = argv[0];  
if (NIL\_P©) return Qnil;  
if (FIXNUM\_P©) {

- int cc = FIX2INT©;
- rb\_encoding \*enc = io\_read\_encoding(fptr);
- char buf[16];

- char cc = (char)FIX2INT©;

- c = rb\_str\_new(buf, rb\_enc\_mbcput(cc, buf, enc));

- io\_ungetc(&cc, 1, fptr);  
}  
else {
- rb\_encoding \*enc = io\_input\_encoding(fptr);  
SafeStringValue©;
- if (rb\_enc\_get\_index© && enc != rb\_enc\_get©) {
- 

```
 c = rb_funcall(c, id_encode, 1, rb_enc_from_encoding(enc));

```

- }
- io\_ungetc(RSTRING\_PTR©, RSTRING\_LEN©, fptr);
- RB\_GC\_GUARD©;
- }
- return Qnil;  
+}
- 

+VALUE  
+rb\_io\_ungetc(VALUE io, VALUE c)  
+{

- VALUE ary = rb\_check\_array\_type©;
- if (!NIL\_P(ary)) {
- rb\_io\_unget(RARRAY\_LEN(ary), RARRAY\_PTR(ary), io);
- RB\_GC\_GUARD(ary);  
}

- io\_ungetc(c, fptr);

- else {
- rb\_io\_unget(1, &c, io);
- }
- return Qnil;  
+}
- 

+/\*

- 
  - call-seq:

- 
  - 

```
ios.ungetbyte(fixnum) => nil

```

- 
  - 

- 
  - Pushes back one character (passed as a parameter) onto

- 
  - _ios_, such that a subsequent buffered read will return it.

- 
  - See also `IO#ungetc`.

- 
  - 

- 
  - 

```
f = File.new("testfile")

```

- 
  - 

```
c = f.getbyte #=> 84

```

- 
  - 

```
f.ungetbyte(c) #=> nil

```

- 
  - 

```
f.getbyte #=> 84

```

- \*/
- 

+VALUE  
+rb\_io\_ungetbyte(VALUE io, VALUE c)  
+{

- rb\_io\_t \*fptr;
- int cc;
- 
- GetOpenFile(io, fptr);
- rb\_io\_check\_readable(fptr);
- if (NIL\_P©) return Qnil;
- cc = NUM2INT©;
- \*io\_unget\_space(1, fptr) = (char)cc;  
return Qnil;  
}  
@@ -6789,5 +6858,6 @@ Init\_IO(void)  
rb\_define\_method(rb\_cIO, “readchar”, rb\_io\_readchar, 0);  
rb\_define\_method(rb\_cIO, “readbyte”, rb\_io\_readbyte, 0);

- rb\_define\_method(rb\_cIO, “ungetc”,rb\_io\_ungetc, 1);

- rb\_define\_method(rb\_cIO, “ungetc”, rb\_io\_unget, -1);
- rb\_define\_method(rb\_cIO, “ungetbyte”, rb\_io\_ungetbyte, 1);  
rb\_define\_method(rb\_cIO, “\<\<”, rb\_io\_addstr, 1);  
rb\_define\_method(rb\_cIO, “flush”, rb\_io\_flush, 0);

---

<div class="post-metadata">

**Author:** ![sheepman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@sheepman](https://www.ruby-forum.com/u/sheepman)\
**Post date:** [March 3, 2008, 11:20am UTC](https://www.ruby-forum.com/t/encoding-affects-script-encoding/130670/5 "2008-03-03T11:20:25Z")

</div>

e$B$J$+$@$G$9!#e(B

At Mon, 3 Mar 2008 17:41:25 +0900,  
Tanaka A. wrote in [ruby-dev:33976]:

> > e$B$=$l$O8=\>u$G$b$G$-$J$$$G$9$M!#e(B?xe$B$be(BStringe$B$K$J$C$?$3$H$G$b$"$k$7!“e(B  
> > 0…255e$B$N@0?t$NJB$S$O%P%$%HNs$H$7$FLa$9!”$H$$$&$N$G$b$$$$$+$b!#e(B
> 
> e$B8=\>u$G$O!"e(BString e$B%\*%V%8%’%/%H$OL5JQ49$G%P%C%U%!$KF~$k$H;W$&e(B  
> e$B$s$G$9$,!"0c$$$^$9e(B?

e$B$=$&$G$9$,!"$=$l$,LdBj$G$O$J$$$+$H;W$$$^$9!#e(B  
e$B30It$HFbIt$N%(%s%3!\<%G%#%s%0$,0[$J$k\>l9g$O!“e(Bgetce$B$5$l$?$b$N$O$9$Ge(B  
e$B$KJQ49$5$l$F$$$k$o$1$G!”$=$l$r$=$N$^$^La$9$H\<!2s$NFI$_9~$_$G:FEYe(B  
e$BJQ49$9$k$3$H$K$J$j$^$9!#$3$l$O$"$^$j4|BT$5$l$kF0:n$H$O;W$($^$;$s!#e(B

---

<div class="post-metadata">

**Author:** ![sheepman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@sheepman](https://www.ruby-forum.com/u/sheepman)\
**Post date:** [March 3, 2008, 9:42am UTC](https://www.ruby-forum.com/t/encoding-affects-script-encoding/130670/6 "2008-03-03T09:42:29Z")

</div>

In article [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
Nobuyoshi N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> e$B$=$l$O8=\>u$G$b$G$-$J$$$G$9$M!#e(B?xe$B$be(BStringe$B$K$J$C$?$3$H$G$b$"$k$7!“e(B  
> 0…255e$B$N@0?t$NJB$S$O%P%$%HNs$H$7$FLa$9!”$H$$$&$N$G$b$$$$$+$b!#e(B

e$B8=\>u$G$O!"e(BString e$B%\*%V%8%'%/%H$OL5JQ49$G%P%C%U%!$KF~$k$H;W$&e(B  
e$B$s$G$9$,!"0c$$$^$9e(B?

> getce$B$He(Bgetbytee$B$bJ,$1$?$3$H$r9M$($l$P!"$3$C$A$G$9$+$M$'!#e(B  
> ungetbytee$B$H$$$&$N$O$$$^$$$A$J5$$,$7$^$9$,!#e(B

e$B$$$^$^$G$N$d$j$+$?$+$i1ieh$9$l$Pe(B ungetbyte e$B$G$9$h$M$'!#e(B
