class String

Constants

TABLE

Public Instance Methods

decode64() click to toggle source
# File lib/core_ext/string.rb, line 73
def decode64
  binary_text = delete('=').split('').map { |str| TABLE.invert.fetch(str) }.join
  binary_text
    .each_char
    .each_slice(8)
    .select { |str| str.count == 8 }
    .map { |str| str.join.to_i(2).chr }
    .join
end
encode64() click to toggle source
# File lib/core_ext/string.rb, line 69
def encode64
  encoded_text
end

Private Instance Methods

binary_text() click to toggle source
# File lib/core_ext/string.rb, line 96
def binary_text
  # NOTE: String#unpackは、エンディアンに依存しそう。。。
  binary_text = unpack('B*')[0]

  loop do
    break if (binary_text.size % 6).zero?
    binary_text << '0'
  end

  binary_text
end
encoded_text() click to toggle source
# File lib/core_ext/string.rb, line 85
def encoded_text
  encoded_text = binary_text.each_char.each_slice(6).map { |str| TABLE.fetch(str.join) }.join

  loop do
    break if (encoded_text.size % 4).zero?
    encoded_text << '='
  end

  encoded_text
end