Class: IABConsentString::Bits
- Inherits:
-
Object
- Object
- IABConsentString::Bits
- Defined in:
- lib/iab_consent_string/bits.rb
Constant Summary collapse
- BYTE_POWS =
big endian
[ -128, 64, 32, 16, 8, 4, 2, 1 ]
Instance Method Summary collapse
-
#getBinaryString ⇒ String
A string representation of the byte array passed in the constructor.
-
#getBit(index) ⇒ Boolean
Get the value of the specified index bit, under a boolean format (1 = true, 0 = false).
-
#getDateTimeFromEpochDeciseconds(startInclusive, size) ⇒ void
returns a time derived from interpreting the given interval on the bit string as long representing the number of demiseconds from the unix epoch.
-
#getInt(startInclusive, size) ⇒ void
Interprets n number of bits as a big endiant int.
-
#getLong(startInclusive, size) ⇒ Long
Interprets n bits as a big endian long.
-
#getSixBitString(startInclusive, size) ⇒ String
Interprets the given interval in the bit string as a series of six bit characters, where 0=A and 26=Z.
-
#initialize(b) ⇒ Bits
constructor
A new instance of Bits.
-
#length ⇒ Integer
The number of bits in the bit string.
- #maxOfSize(size) ⇒ Object
-
#setBit(index) ⇒ Object
Set the value of the specified index bit to 1.
- #setDateTimeToEpochDeciseconds(startInclusive, size, dateTime) ⇒ Object
-
#setInt(startInclusive, size, to) ⇒ void
Writes an integer value into a bit array of given size.
-
#setLong(startInclusive, size, to) ⇒ void
Writes a long value into a bit array of given size.
- #setNumber(startInclusive, size, to) ⇒ Object
-
#setSixBitString(startInclusive, size, to) ⇒ Object
Interprets characters, as 0=A and 26=Z and writes to the given interval in the bit string as a series of six bits.
- #toByteArray ⇒ Object
-
#unsetBit(index) ⇒ Object
Set the value of the specified index bit to 1.
Constructor Details
#initialize(b) ⇒ Bits
Returns a new instance of Bits.
10 11 12 |
# File 'lib/iab_consent_string/bits.rb', line 10 def initialize(b) @bytes = b end |
Instance Method Details
#getBinaryString ⇒ String
Returns a string representation of the byte array passed in the constructor. for example, a bit array of [4] yields a String of “0100”.
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/iab_consent_string/bits.rb', line 172 def getBinaryString s = String.new() size = length() for i in (0...size) do if (getBit(i)) s << "1" else s << "0" end end s end |
#getBit(index) ⇒ Boolean
Get the value of the specified index bit, under a boolean format (1 = true, 0 = false)
17 18 19 20 21 22 |
# File 'lib/iab_consent_string/bits.rb', line 17 def getBit(index) byteIndex = index / 8 bitExact = index % 8 b = @bytes[byteIndex] return (b & BYTE_POWS[bitExact]) != 0 end |
#getDateTimeFromEpochDeciseconds(startInclusive, size) ⇒ void
This method returns an undefined value.
returns a time derived from interpreting the given interval on the bit string as long representing
the number of demiseconds from the unix epoch
122 123 124 125 |
# File 'lib/iab_consent_string/bits.rb', line 122 def getDateTimeFromEpochDeciseconds(startInclusive,size) epochDemi = getLong(startInclusive, size) epochDemi * 100 end |
#getInt(startInclusive, size) ⇒ void
This method returns an undefined value.
Interprets n number of bits as a big endiant int
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/iab_consent_string/bits.rb', line 45 def getInt(startInclusive, size) # Integer Size limited to 32 bits if (size > 32) raise IABConsentString::Error::VendorConsentParseError, "can't fit bit range in int " + size, caller end val = 0 sigMask = 1 sigIndex = size - 1 for i in (0...size) do if getBit(startInclusive + i) val += (sigMask << sigIndex) end sigIndex -= 1 end val end |
#getLong(startInclusive, size) ⇒ Long
Interprets n bits as a big endian long
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/iab_consent_string/bits.rb', line 83 def getLong(startInclusive, size) # Long Size limited to 64 bits if (size > 64) raise IABConsentString::Error::VendorConsentParseError, "can't fit bit range in long: " + size , caller end val = 0 sigMask = 1 sigIndex = size - 1 for i in (0...size) do if (getBit(startInclusive + i)) val += (sigMask << sigIndex) end sigIndex -= 1 end val end |
#getSixBitString(startInclusive, size) ⇒ String
Interprets the given interval in the bit string as a series of six bit characters, where 0=A and 26=Z
141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/iab_consent_string/bits.rb', line 141 def getSixBitString(startInclusive, size) if (size % 6 != 0) raise IABConsentString::Error::VendorConsentParseError , "string bit length must be multiple of six: " + size, caller end charNum = size / 6 val = String.new() for i in (0...charNum) do charCode = getInt(startInclusive + (i * 6), 6) + 65 val << charCode.chr end val.upcase end |
#length ⇒ Integer
Returns the number of bits in the bit string.
132 133 134 |
# File 'lib/iab_consent_string/bits.rb', line 132 def length @bytes.length * 8 end |
#maxOfSize(size) ⇒ Object
199 200 201 202 203 204 205 |
# File 'lib/iab_consent_string/bits.rb', line 199 def maxOfSize(size) max = 0 for i in (0...size) do max += 2**i end max end |
#setBit(index) ⇒ Object
Set the value of the specified index bit to 1
26 27 28 29 30 |
# File 'lib/iab_consent_string/bits.rb', line 26 def setBit(index) byteIndex = index / 8 shift = (byteIndex + 1) * 8 - index - 1 @bytes[byteIndex] |= 1 << shift end |
#setDateTimeToEpochDeciseconds(startInclusive, size, dateTime) ⇒ Object
127 128 129 |
# File 'lib/iab_consent_string/bits.rb', line 127 def setDateTimeToEpochDeciseconds(startInclusive, size, dateTime) setLong(startInclusive, size, dateTime / 100) end |
#setInt(startInclusive, size, to) ⇒ void
This method returns an undefined value.
Writes an integer value into a bit array of given size
69 70 71 72 73 74 75 76 |
# File 'lib/iab_consent_string/bits.rb', line 69 def setInt(startInclusive, size, to) # Integer Size limited to 32 bits if (size > 32 || to > maxOfSize(size) || to < 0) raise IABConsentString::Error::VendorConsentCreateError, "can't fit integer into bit range of size" + size , caller end setNumber(startInclusive, size, to); end |
#setLong(startInclusive, size, to) ⇒ void
This method returns an undefined value.
Writes a long value into a bit array of given size
107 108 109 110 111 112 113 114 |
# File 'lib/iab_consent_string/bits.rb', line 107 def setLong(startInclusive, size, to) # Long Size limited to 64 bits if (size > 64 || to > maxOfSize(size) || to < 0) raise IABConsentString::Error::VendorConsentCreateError, "can't fit long into bit range of size " + size , caller end setNumber(startInclusive, size, to) end |
#setNumber(startInclusive, size, to) ⇒ Object
189 190 191 192 193 194 195 196 197 |
# File 'lib/iab_consent_string/bits.rb', line 189 def setNumber(startInclusive,size,to) (size - 1).downto(0) do |i| index = startInclusive + i byteIndex = index / 8 shift = (byteIndex + 1) * 8 - index - 1 @bytes[byteIndex] |= (to % 2) << shift to /= 2 end end |
#setSixBitString(startInclusive, size, to) ⇒ Object
Interprets characters, as 0=A and 26=Z and writes to the given interval in the bit string as a series of six bits
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/iab_consent_string/bits.rb', line 159 def setSixBitString(startInclusive, size, to) if (size % 6 != 0 || size / 6 != to.length()) raise IABConsentString::Error::VendorConsentCreateError , "bit array size must be multiple of six and equal to 6 times the size of string", caller end values = to.chars for i in (0...values.length) do charCode = values[i].ord - 65 setInt(startInclusive + (i * 6), 6, charCode) end end |
#toByteArray ⇒ Object
185 186 187 |
# File 'lib/iab_consent_string/bits.rb', line 185 def toByteArray @bytes end |
#unsetBit(index) ⇒ Object
Set the value of the specified index bit to 1
34 35 36 37 38 |
# File 'lib/iab_consent_string/bits.rb', line 34 def unsetBit(index) byteIndex = index / 8 shift = (byteIndex + 1) * 8 - index - 1 @bytes[byteIndex] &= ~(1 << shift) end |