Module: HMap::Utils

Defined in:
lib/cocoapods-hmap/utils.rb

Overview

A collection of utility functions used throughout cocoapods-hmap.

Class Method Summary collapse

Class Method Details

.effective_platform_name(symbolic_name) ⇒ String

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



16
17
18
19
20
21
22
23
24
# 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) ⇒ Object



26
27
28
# 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) ⇒ Object



49
50
51
52
53
54
# 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) ⇒ Object



30
31
32
33
# File 'lib/cocoapods-hmap/utils.rb', line 30

def self.index_of_range(num, range)
  num &= range - 1
  num
end

.magic?(magic) ⇒ Boolean



82
83
84
# 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) ⇒ Object



39
40
41
42
43
44
45
46
47
# 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) ⇒ Boolean



35
36
37
# 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) ⇒ Object



86
87
88
89
90
91
92
93
94
# 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) ⇒ Object



56
57
58
59
# File 'lib/cocoapods-hmap/utils.rb', line 56

def self.specialize_format(format, swapped)
  modifier = swapped ? '<' : '>'
  format.tr('=', modifier)
end

.string_downcase_hash(str) ⇒ Object



61
62
63
64
65
66
# 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) ⇒ Boolean



78
79
80
# 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) ⇒ Object



68
69
70
71
72
73
74
75
76
# 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