1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

merges r29446 and r29448 from trunk into ruby_1_9_2.

--
* numeric.c (rb_enc_uint_chr): split from int_chr.

* numeric.c (int_chr): use rb_enc_uint_chr.

* include/ruby/encoding.h (rb_enc_uint_chr): added.
--
* io.c (rb_io_ungetc): use unsigned int for GB18030.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_2@30038 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
yugui 2010-12-02 08:06:46 +00:00
parent 55eecb54e7
commit 230b8b524d
6 changed files with 60 additions and 17 deletions

View file

@ -1,3 +1,15 @@
Tue Oct 12 15:36:09 2010 NARUSE, Yui <naruse@ruby-lang.org>
* io.c (rb_io_ungetc): use unsigned int for GB18030.
Tue Oct 12 15:10:31 2010 NARUSE, Yui <naruse@ruby-lang.org>
* numeric.c (rb_enc_uint_chr): split from int_chr.
* numeric.c (int_chr): use rb_enc_uint_chr.
* include/ruby/encoding.h (rb_enc_uint_chr): added.
Tue Oct 12 14:04:41 2010 NARUSE, Yui <naruse@ruby-lang.org> Tue Oct 12 14:04:41 2010 NARUSE, Yui <naruse@ruby-lang.org>
* numeric.c (int_chr): a codepoint of Ruby M17N must be 32bit * numeric.c (int_chr): a codepoint of Ruby M17N must be 32bit

View file

@ -98,6 +98,7 @@ long rb_enc_strlen(const char*, const char*, rb_encoding*);
char* rb_enc_nth(const char*, const char*, long, rb_encoding*); char* rb_enc_nth(const char*, const char*, long, rb_encoding*);
VALUE rb_obj_encoding(VALUE); VALUE rb_obj_encoding(VALUE);
VALUE rb_enc_str_buf_cat(VALUE str, const char *ptr, long len, rb_encoding *enc); VALUE rb_enc_str_buf_cat(VALUE str, const char *ptr, long len, rb_encoding *enc);
VALUE rb_enc_uint_chr(unsigned int code, rb_encoding *enc);
VALUE rb_external_str_new_with_enc(const char *ptr, long len, rb_encoding *); VALUE rb_external_str_new_with_enc(const char *ptr, long len, rb_encoding *);
VALUE rb_str_export_to_enc(VALUE, rb_encoding *); VALUE rb_str_export_to_enc(VALUE, rb_encoding *);

11
io.c
View file

@ -3193,12 +3193,13 @@ rb_io_ungetc(VALUE io, VALUE c)
rb_io_check_char_readable(fptr); rb_io_check_char_readable(fptr);
if (NIL_P(c)) return Qnil; if (NIL_P(c)) return Qnil;
if (FIXNUM_P(c)) { if (FIXNUM_P(c)) {
int cc = FIX2INT(c); c = rb_enc_uint_chr(FIX2UINT(c), io_read_encoding(fptr));
rb_encoding *enc = io_read_encoding(fptr);
char buf[16];
c = rb_str_new(buf, rb_enc_mbcput(cc, buf, enc));
} }
#if SIZEOF_LONG > SIZEOF_INT
else if (TYPE(c) == T_BIGNUM) {
c = rb_enc_uint_chr(NUM2UINT(c), io_read_encoding(fptr));
}
#endif
else { else {
SafeStringValue(c); SafeStringValue(c);
} }

View file

@ -2038,6 +2038,19 @@ int_pred(VALUE num)
return rb_funcall(num, '-', 1, INT2FIX(1)); return rb_funcall(num, '-', 1, INT2FIX(1));
} }
VALUE
rb_enc_uint_chr(unsigned int code, rb_encoding *enc)
{
int n;
VALUE str;
if ((n = rb_enc_codelen(code, enc)) <= 0) {
rb_raise(rb_eRangeError, "%d out of char range", code);
}
str = rb_enc_str_new(0, n, enc);
rb_enc_mbcput(code, RSTRING_PTR(str), enc);
return str;
}
/* /*
* call-seq: * call-seq:
* int.chr([encoding]) -> string * int.chr([encoding]) -> string
@ -2054,16 +2067,14 @@ static VALUE
int_chr(int argc, VALUE *argv, VALUE num) int_chr(int argc, VALUE *argv, VALUE num)
{ {
char c; char c;
int n; unsigned int i = NUM2UINT(num);
uint32_t i = NUM2UINT(num);
rb_encoding *enc; rb_encoding *enc;
VALUE str;
switch (argc) { switch (argc) {
case 0: case 0:
if (i < 0) { if (i < 0) {
out_of_range: out_of_range:
rb_raise(rb_eRangeError, "%"PRIdVALUE " out of char range", i); rb_raise(rb_eRangeError, "%d out of char range", i);
} }
if (0xff < i) { if (0xff < i) {
enc = rb_default_internal_encoding(); enc = rb_default_internal_encoding();
@ -2086,13 +2097,7 @@ int_chr(int argc, VALUE *argv, VALUE num)
enc = rb_to_encoding(argv[0]); enc = rb_to_encoding(argv[0]);
if (!enc) enc = rb_ascii8bit_encoding(); if (!enc) enc = rb_ascii8bit_encoding();
decode: decode:
#if SIZEOF_INT < SIZEOF_VALUE return rb_enc_uint_chr(i, enc);
if (i > UINT_MAX) goto out_of_range;
#endif
if (i < 0 || (n = rb_enc_codelen(i, enc)) <= 0) goto out_of_range;
str = rb_enc_str_new(0, n, enc);
rb_enc_mbcput(i, RSTRING_PTR(str), enc);
return str;
} }
/* /*

View file

@ -418,6 +418,30 @@ EOT
} }
end end
def test_ungetc_int
with_tmpdir {
generate_file('tmp', "A")
s = open("tmp", "r:GB18030") {|f|
f.ungetc(0x8431A439)
f.read
}
assert_equal(Encoding::GB18030, s.encoding)
assert_str_equal(0x8431A439.chr("GB18030")+"A", s)
}
end
def test_ungetc_str
with_tmpdir {
generate_file('tmp', "A")
s = open("tmp", "r:GB18030") {|f|
f.ungetc(0x8431A439.chr("GB18030"))
f.read
}
assert_equal(Encoding::GB18030, s.encoding)
assert_str_equal(0x8431A439.chr("GB18030")+"A", s)
}
end
def test_ungetc_stateful_conversion def test_ungetc_stateful_conversion
with_tmpdir { with_tmpdir {
src = "before \e$B\x23\x30\x23\x31\e(B after".force_encoding("iso-2022-jp") src = "before \e$B\x23\x30\x23\x31\e(B after".force_encoding("iso-2022-jp")

View file

@ -1,5 +1,5 @@
#define RUBY_VERSION "1.9.2" #define RUBY_VERSION "1.9.2"
#define RUBY_PATCHLEVEL 68 #define RUBY_PATCHLEVEL 69
#define RUBY_VERSION_MAJOR 1 #define RUBY_VERSION_MAJOR 1
#define RUBY_VERSION_MINOR 9 #define RUBY_VERSION_MINOR 9
#define RUBY_VERSION_TEENY 1 #define RUBY_VERSION_TEENY 1