summaryrefslogtreecommitdiff
path: root/io.c
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-05-25 01:39:30 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-05-25 01:39:30 +0000
commitcb1a793510a8ae7acfb92d46cecd37d100903f9c (patch)
treebd0bd6cc21625daa291168f14ac31ed9bf952cdb /io.c
parent046883d2ca8c1b2074b261947ea708e6695f667e (diff)
merge revision(s) 35766:
* io.c (io_strip_bom): check EOF. [Bug #6487][ruby-core:45203] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_3@35782 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'io.c')
-rw-r--r--io.c68
1 files changed, 33 insertions, 35 deletions
diff --git a/io.c b/io.c
index 916281dd49..1aa58d5be7 100644
--- a/io.c
+++ b/io.c
@@ -4900,65 +4900,63 @@ static void io_encoding_set(rb_io_t *, VALUE, VALUE, VALUE);
static int
io_strip_bom(VALUE io)
{
- int b1, b2, b3, b4;
- switch (b1 = FIX2INT(rb_io_getbyte(io))) {
- case 0xEF:
- b2 = FIX2INT(rb_io_getbyte(io));
- if (b2 == 0xBB) {
- b3 = FIX2INT(rb_io_getbyte(io));
- if (b3 == 0xBF) {
+ VALUE b1, b2, b3, b4;
+
+ if (NIL_P(b1 = rb_io_getbyte(io))) return 0;
+ switch (b1) {
+ case INT2FIX(0xEF):
+ if (NIL_P(b2 = rb_io_getbyte(io))) break;
+ if (b2 == INT2FIX(0xBB) && !NIL_P(b3 = rb_io_getbyte(io))) {
+ if (b3 == INT2FIX(0xBF)) {
return rb_utf8_encindex();
}
- rb_io_ungetbyte(io, INT2FIX(b3));
+ rb_io_ungetbyte(io, b3);
}
- rb_io_ungetbyte(io, INT2FIX(b2));
+ rb_io_ungetbyte(io, b2);
break;
- case 0xFE:
- b2 = FIX2INT(rb_io_getbyte(io));
- if (b2 == 0xFF) {
+ case INT2FIX(0xFE):
+ if (NIL_P(b2 = rb_io_getbyte(io))) break;
+ if (b2 == INT2FIX(0xFF)) {
return rb_enc_find_index("UTF-16BE");
}
- rb_io_ungetbyte(io, INT2FIX(b2));
+ rb_io_ungetbyte(io, b2);
break;
- case 0xFF:
- b2 = FIX2INT(rb_io_getbyte(io));
- if (b2 == 0xFE) {
- b3 = FIX2INT(rb_io_getbyte(io));
- if (b3 == 0) {
- b4 = FIX2INT(rb_io_getbyte(io));
- if (b4 == 0) {
+ case INT2FIX(0xFF):
+ if (NIL_P(b2 = rb_io_getbyte(io))) break;
+ if (b2 == INT2FIX(0xFE)) {
+ b3 = rb_io_getbyte(io);
+ if (b3 == INT2FIX(0) && !NIL_P(b4 = rb_io_getbyte(io))) {
+ if (b4 == INT2FIX(0)) {
return rb_enc_find_index("UTF-32LE");
}
- rb_io_ungetbyte(io, INT2FIX(b4));
+ rb_io_ungetbyte(io, b4);
+ rb_io_ungetbyte(io, b3);
}
else {
- rb_io_ungetbyte(io, INT2FIX(b3));
+ rb_io_ungetbyte(io, b3);
return rb_enc_find_index("UTF-16LE");
}
- rb_io_ungetbyte(io, INT2FIX(b3));
}
- rb_io_ungetbyte(io, INT2FIX(b2));
+ rb_io_ungetbyte(io, b2);
break;
- case 0:
- b2 = FIX2INT(rb_io_getbyte(io));
- if (b2 == 0) {
- b3 = FIX2INT(rb_io_getbyte(io));
- if (b3 == 0xFE) {
- b4 = FIX2INT(rb_io_getbyte(io));
- if (b4 == 0xFF) {
+ case INT2FIX(0):
+ if (NIL_P(b2 = rb_io_getbyte(io))) break;
+ if (b2 == INT2FIX(0) && !NIL_P(b3 = rb_io_getbyte(io))) {
+ if (b3 == INT2FIX(0xFE) && !NIL_P(b4 = rb_io_getbyte(io))) {
+ if (b4 == INT2FIX(0xFF)) {
return rb_enc_find_index("UTF-32BE");
}
- rb_io_ungetbyte(io, INT2FIX(b4));
+ rb_io_ungetbyte(io, b4);
}
- rb_io_ungetbyte(io, INT2FIX(b3));
+ rb_io_ungetbyte(io, b3);
}
- rb_io_ungetbyte(io, INT2FIX(b2));
+ rb_io_ungetbyte(io, b2);
break;
}
- rb_io_ungetbyte(io, INT2FIX(b1));
+ rb_io_ungetbyte(io, b1);
return 0;
}