34
34
import java .util .Arrays ;
35
35
import java .util .List ;
36
36
37
- import org .jcodings .Encoding ;
38
37
import org .jruby .anno .FrameField ;
39
38
import org .jruby .anno .JRubyClass ;
40
39
import org .jruby .anno .JRubyMethod ;
@@ -744,6 +743,15 @@ public IRubyObject readchar() {
744
743
return c ;
745
744
}
746
745
746
+ @ JRubyMethod (name = "readchar" , compat = CompatVersion .RUBY1_9 )
747
+ public IRubyObject readchar19 (ThreadContext context ) {
748
+ IRubyObject c = getc19 (context );
749
+
750
+ if (c .isNil ()) throw getRuntime ().newEOFError ();
751
+
752
+ return c ;
753
+ }
754
+
747
755
@ JRubyMethod (name = "readline" , optional = 1 , writes = FrameField .LASTLINE )
748
756
public IRubyObject readline (ThreadContext context , IRubyObject [] args ) {
749
757
IRubyObject line = gets (context , args );
@@ -883,16 +891,7 @@ public IRubyObject ungetc(IRubyObject arg) {
883
891
884
892
int c = RubyNumeric .num2int (arg );
885
893
if (data .pos == 0 ) return getRuntime ().getNil ();
886
- data .internal .modify ();
887
- data .pos --;
888
-
889
- ByteList bytes = data .internal .getByteList ();
890
-
891
- if (bytes .length () <= data .pos ) {
892
- bytes .length ((int )data .pos + 1 );
893
- }
894
-
895
- bytes .set ((int ) data .pos , c );
894
+ ungetcCommon (c );
896
895
return getRuntime ().getNil ();
897
896
}
898
897
@@ -901,17 +900,33 @@ public IRubyObject ungetc19(ThreadContext context, IRubyObject arg) {
901
900
checkReadable ();
902
901
903
902
if (!arg .isNil ()) {
903
+ int c ;
904
904
if (arg instanceof RubyFixnum ) {
905
- int codepoint = RubyNumeric .fix2int (arg );
906
- Encoding encoding = data .internal .getEncoding ();
905
+ c = RubyNumeric .fix2int (arg );
907
906
} else {
908
- RubyString s = arg .convertToString ();
907
+ RubyString str = arg .convertToString ();
908
+ c = str .getEncoding ().mbcToCode (str .getBytes (), 0 , 1 );
909
909
}
910
+
911
+ ungetcCommon (c );
910
912
}
911
913
912
914
return getRuntime ().getNil ();
913
915
}
914
916
917
+ private void ungetcCommon (int c ) {
918
+ data .internal .modify ();
919
+ data .pos --;
920
+
921
+ ByteList bytes = data .internal .getByteList ();
922
+
923
+ if (bytes .length () <= data .pos ) {
924
+ bytes .length ((int )data .pos + 1 );
925
+ }
926
+
927
+ bytes .set ((int ) data .pos , c );
928
+ }
929
+
915
930
@ JRubyMethod (name = {"write" , "syswrite" }, required = 1 )
916
931
public IRubyObject write (ThreadContext context , IRubyObject arg ) {
917
932
return context .getRuntime ().newFixnum (writeInternal (context , arg ));
0 commit comments