Class: Multibases::Base16
- Inherits:
-
Object
- Object
- Multibases::Base16
- Defined in:
- lib/multibases/base16.rb
Defined Under Namespace
Classes: Table
Constant Summary collapse
- Default =
Base16.new('0123456789abcdef')
Class Method Summary collapse
- .decode(packed) ⇒ Object
-
.encode(plain) ⇒ Object
RFC 4648 implementation.
Instance Method Summary collapse
- #decodable?(encoded) ⇒ Boolean
- #decode(encoded) ⇒ Object
- #default? ⇒ Boolean
- #encode(plain) ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
-
#initialize(alphabet, strict: false, encoding: nil) ⇒ Base16
constructor
A new instance of Base16.
- #inspect ⇒ Object
- #table_ords(force_strict: nil) ⇒ Object
Constructor Details
Class Method Details
.decode(packed) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/multibases/base16.rb', line 26 def self.decode(packed) packed = packed.pack('C*') if packed.is_a?(Array) # packed.scan(/../).map { |x| x.hex.chr }.join DecodedByteArray.new(Array(String(packed)).pack('H*').bytes) end |
.encode(plain) ⇒ Object
RFC 4648 implementation
16 17 18 19 20 21 22 23 24 |
# File 'lib/multibases/base16.rb', line 16 def self.encode(plain) plain = plain.pack('C*') if plain.is_a?(Array) # plain.each_byte.map do |byte| byte.to_s(16) end.join EncodedByteArray.new( plain.unpack1('H*').bytes, encoding: Encoding::US_ASCII ) end |
Instance Method Details
#decodable?(encoded) ⇒ Boolean
101 102 103 |
# File 'lib/multibases/base16.rb', line 101 def decodable?(encoded) (encoded.uniq - @table.tr_ords).length.zero? end |
#decode(encoded) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/multibases/base16.rb', line 69 def decode(encoded) return DecodedByteArray::EMPTY if encoded.empty? unless encoded.is_a?(Array) encoded = encoded.force_encoding(@table.encoding).bytes end unless decodable?(encoded) raise ArgumentError, "'#{encoded}' contains unknown characters'" end unless default? encoded = ByteArray.new(encoded).transcode( table_ords, Default.table_ords(force_strict: @table.strict?), encoding: Encoding::US_ASCII ) end Multibases::Base16.decode(encoded) end |
#default? ⇒ Boolean
91 92 93 |
# File 'lib/multibases/base16.rb', line 91 def default? eql?(Default) end |
#encode(plain) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/multibases/base16.rb', line 58 def encode(plain) encoded = Multibases::Base16.encode(plain) return encoded if default? encoded.transcode( Default.table_ords(force_strict: @table.strict?), table_ords, encoding: @table.encoding ) end |
#eql?(other) ⇒ Boolean Also known as: ==
95 96 97 |
# File 'lib/multibases/base16.rb', line 95 def eql?(other) other.is_a?(Base16) && other.instance_variable_get(:@table) == @table end |
#inspect ⇒ Object
8 9 10 11 12 13 |
# File 'lib/multibases/base16.rb', line 8 def inspect '[Multibases::Base16 ' \ "alphabet=\"#{@table.alphabet}\"" \ "#{@table.strict? ? ' strict' : ''}" \ ']' end |
#table_ords(force_strict: nil) ⇒ Object
105 106 107 |
# File 'lib/multibases/base16.rb', line 105 def table_ords(force_strict: nil) @table.tr_ords(force_strict: force_strict) end |