lib/bindata/bits.rb in bindata-2.4.13 vs lib/bindata/bits.rb in bindata-2.4.14

- old
+ new

@@ -97,33 +97,33 @@ end end def create_dynamic_clamp_code(signed) if signed == :signed - max = "max = (1 << (nbits - 1)) - 1" - min = "min = -(max + 1)" + max = "(1 << (nbits - 1)) - 1" + min = "-((#{max}) + 1)" else - max = "max = (1 << nbits) - 1" - min = "min = 0" + max = "(1 << nbits) - 1" + min = "0" end - "#{max}; #{min}; val = (val < min) ? min : (val > max) ? max : val" + "val = val.clamp(#{min}, #{max})" end def create_fixed_clamp_code(nbits, signed) if nbits == 1 && signed == :signed raise "signed bitfield must have more than one bit" end if signed == :signed - max = "max = (1 << (#{nbits} - 1)) - 1" - min = "min = -(max + 1)" + max = "(1 << (#{nbits} - 1)) - 1" + min = "-((#{max}) + 1)" else - min = "min = 0" - max = "max = (1 << #{nbits}) - 1" + min = "0" + max = "(1 << #{nbits}) - 1" end - clamp = "(#{max}; #{min}; val = (val < min) ? min : (val > max) ? max : val)" + clamp = "(val = val.clamp(#{min}, #{max}))" if nbits == 1 # allow single bits to be used as booleans clamp = "(val == true) ? 1 : (not val) ? 0 : #{clamp}" end