src/bit_struct/byte_data.rb in BitStructEx-0.0.74 vs src/bit_struct/byte_data.rb in BitStructEx-0.0.85
- old
+ new
@@ -1,13 +1,13 @@
+require 'bit_struct/endianess.rb'
+
module BitStruct
class ByteData
- LITTLE_ENDIAN = LITTLE = INTEL = 0
- BIG_ENDIAN = BIG = MOTOROLA = 1
- NETWORK_BYTE_ORDER = BIG_ENDIAN
+ include Endianess
def initialize( data, endianess = nil )
raise "Bad input" unless data.respond_to? "[]".to_sym
raise "Unknown endianess" unless valid endianess
@data = data
@@ -38,10 +38,14 @@
def []=( idx, val )
@data[ endianized( idx ) ] = val
end
+ def reverse
+ ReversedByteData.new self, BIG_ENDIAN
+ end
+
private
def valid( endianess )
endianess == nil || endianess == LITTLE_ENDIAN || endianess == BIG_ENDIAN
end
@@ -49,8 +53,18 @@
def endianized( idx )
return idx if @endianess == BIG_ENDIAN
return @data.length - idx - 1 if @endianess == LITTLE_ENDIAN
end
+ end
+
+ class ReversedByteData < ByteData
+
+ alias super_endianized endianized
+
+ def endianized( idx )
+ res = super_endianized( idx )
+ @data.length - res - 1
+ end
end
end