module HMap::Utils

A collection of utility functions used throughout cocoapods-hmap.

Public Class Methods

effective_platform_name(symbolic_name) click to toggle source

Converts the symbolic name of a platform to a string name suitable to be presented to the user.

@param [Symbol] symbolic_name

the symbolic name of a platform.

@return [String] The string that describes the name of the given symbol.

# File lib/cocoapods-hmap/utils.rb, line 16
def self.effective_platform_name(symbolic_name)
  case symbolic_name
  when :ios then %w[iphoneos iphonesimulator]
  when :osx then %w[macosx]
  when :watchos then %w[watchos watchsimulator]
  when :tvos then %w[appletvos appletvsimulator]
  else []
  end
end
effective_platforms_names(platforms) click to toggle source
# File lib/cocoapods-hmap/utils.rb, line 26
def self.effective_platforms_names(platforms)
  platforms.flat_map { |name| effective_platform_name(name) }.compact.uniq
end
hash_set_value(hash, *args) click to toggle source
# File lib/cocoapods-hmap/utils.rb, line 49
def self.hash_set_value(hash, *args)
  args.each do |arg|
    hash.merge(arg)
  end
  hash
end
index_of_range(num, range) click to toggle source
# File lib/cocoapods-hmap/utils.rb, line 30
def self.index_of_range(num, range)
  num &= range - 1
  num
end
magic?(magic) click to toggle source
# File lib/cocoapods-hmap/utils.rb, line 82
def self.magic?(magic)
  magic.eql?(HEADER_CONST[:HMAP_SWAPPED_MAGIC]) || magic.eql?(HEADER_CONST[:HMAP_HEADER_MAGIC_NUMBER])
end
next_power_of_two(num) click to toggle source
# File lib/cocoapods-hmap/utils.rb, line 39
def self.next_power_of_two(num)
  num |= (num >> 1)
  num |= (num >> 2)
  num |= (num >> 4)
  num |= (num >> 8)
  num |= (num >> 16)
  num |= (num >> 32)
  num + 1
end
power_of_two?(num) click to toggle source
# File lib/cocoapods-hmap/utils.rb, line 35
def self.power_of_two?(num)
  num != 0 && (num & (num - 1)).zero?
end
safe_encode(string, target_encoding) click to toggle source
# File lib/cocoapods-hmap/utils.rb, line 86
def self.safe_encode(string, target_encoding)
  string.encode(target_encoding)
rescue Encoding::InvalidByteSequenceError
  string.force_encoding(target_encoding)
rescue Encoding::UndefinedConversionError
  string.encode(target_encoding, fallback: lambda { |c|
    c.force_encoding(target_encoding)
  })
end
specialize_format(format, swapped) click to toggle source
# File lib/cocoapods-hmap/utils.rb, line 56
def self.specialize_format(format, swapped)
  modifier = swapped ? '<' : '>'
  format.tr('=', modifier)
end
string_downcase_hash(str) click to toggle source
# File lib/cocoapods-hmap/utils.rb, line 61
def self.string_downcase_hash(str)
  str.downcase.bytes.inject(0) do |sum, value|
    sum += value * 13
    sum
  end
end
swapped_magic?(magic, version) click to toggle source
# File lib/cocoapods-hmap/utils.rb, line 78
def self.swapped_magic?(magic, version)
  magic.eql?(HEADER_CONST[:HMAP_SWAPPED_MAGIC]) && version.eql?(HEADER_CONST[:HMAP_SWAPPED_VERSION])
end
update_changed_file(path, contents) click to toggle source
# File lib/cocoapods-hmap/utils.rb, line 68
def self.update_changed_file(path, contents)
  if path.exist?
    content_stream = StringIO.new(contents)
    identical = File.open(path, 'rb') { |f| FileUtils.compare_stream(f, content_stream) }
    return if identical
  end
  path.dirname.mkpath
  File.open(path, 'w') { |f| f.write(contents) }
end