Skip to content

Commit

Permalink
Array#pack with format "fFeEdDgG" will raise a TypeError when does no…
Browse files Browse the repository at this point in the history
…t convert into float.

Test Script:
{{{
require 'test/unit/assertions.rb'
include Test::Unit::Assertions

assert_raise(TypeError){ ["a"].pack("f") }
assert_raise(TypeError){ ["a"].pack("F") }
assert_raise(TypeError){ ["a"].pack("e") }
assert_raise(TypeError){ ["a"].pack("E") }
assert_raise(TypeError){ ["a"].pack("d") }
assert_raise(TypeError){ ["a"].pack("D") }
assert_raise(TypeError){ ["a"].pack("g") }
assert_raise(TypeError){ ["a"].pack("G") }

class String
  def utf8
    self.force_encoding('UTF-8')
  end
end
assert_equal("\x00\x00\x80?".utf8, [1.0].pack("f").utf8)
assert_equal("\x00\x00\x80?".utf8, [1.0].pack("e").utf8)
assert_equal("\x00\x00\x00\x00\x00\x00\xF0?".utf8, [1.0].pack("d").utf8)
assert_equal("?\x80\x00\x00".utf8, [1.0].pack("g").utf8)

puts :ok
}}}

git-svn-id: http://svn.macosforge.org/repository/ruby/MacRuby/trunk@5169 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information
Watson1978 committed Jan 15, 2011
1 parent 9595725 commit 8d12f25
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions include/ruby/intern.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ VALUE rb_check_convert_type(VALUE,int,const char*,const char*);
VALUE rb_check_to_integer(VALUE, const char *);
VALUE rb_to_int(VALUE);
VALUE rb_Integer(VALUE);
VALUE rb_to_float(VALUE);
VALUE rb_Float(VALUE);
VALUE rb_String(VALUE);
VALUE rb_Array(VALUE);
Expand Down
14 changes: 14 additions & 0 deletions object.c
Original file line number Diff line number Diff line change
Expand Up @@ -2754,6 +2754,20 @@ rb_f_float(VALUE obj, SEL sel, VALUE arg)
return rb_Float(arg);
}

VALUE
rb_to_float(VALUE val)
{
if (TYPE(val) == T_FLOAT) return val;
if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
rb_raise(rb_eTypeError, "can't convert %s into Float",
NIL_P(val) ? "nil" :
val == Qtrue ? "true" :
val == Qfalse ? "false" :
rb_obj_classname(val));
}
return rb_convert_type(val, T_FLOAT, "Float", "to_f");
}

VALUE
rb_check_to_float(VALUE val)
{
Expand Down
12 changes: 6 additions & 6 deletions pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
float f;

from = NEXTFROM;
f = RFLOAT_VALUE(rb_Float(from));
f = RFLOAT_VALUE(rb_to_float(from));
rb_bstr_concat(data, (const UInt8 *)&f, sizeof(float));
}
break;
Expand All @@ -803,7 +803,7 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
FLOAT_CONVWITH(ftmp);

from = NEXTFROM;
f = RFLOAT_VALUE(rb_Float(from));
f = RFLOAT_VALUE(rb_to_float(from));
f = HTOVF(f,ftmp);
rb_bstr_concat(data, (const UInt8 *)&f, sizeof(float));
}
Expand All @@ -815,7 +815,7 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
DOUBLE_CONVWITH(dtmp);

from = NEXTFROM;
d = RFLOAT_VALUE(rb_Float(from));
d = RFLOAT_VALUE(rb_to_float(from));
d = HTOVD(d,dtmp);
rb_bstr_concat(data, (const UInt8 *)&d, sizeof(double));
}
Expand All @@ -827,7 +827,7 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
double d;

from = NEXTFROM;
d = RFLOAT_VALUE(rb_Float(from));
d = RFLOAT_VALUE(rb_to_float(from));
rb_bstr_concat(data, (const UInt8 *)&d, sizeof(double));
}
break;
Expand All @@ -838,7 +838,7 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
FLOAT_CONVWITH(ftmp);

from = NEXTFROM;
f = RFLOAT_VALUE(rb_Float(from));
f = RFLOAT_VALUE(rb_to_float(from));
f = HTONF(f,ftmp);
rb_bstr_concat(data, (const UInt8 *)&f, sizeof(float));
}
Expand All @@ -850,7 +850,7 @@ pack_pack(VALUE ary, SEL sel, VALUE fmt)
DOUBLE_CONVWITH(dtmp);

from = NEXTFROM;
d = RFLOAT_VALUE(rb_Float(from));
d = RFLOAT_VALUE(rb_to_float(from));
d = HTOND(d,dtmp);
rb_bstr_concat(data, (const UInt8 *)&d, sizeof(double));
}
Expand Down

0 comments on commit 8d12f25

Please sign in to comment.