Module: HMap::Utils

Defined in:
lib/hmap/helper/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.

Parameters:

  • symbolic_name (Symbol)

    the symbolic name of a platform.

Returns:

  • (String)

    The string that describes the name of the given symbol.



14
15
16
17
18
19
20
21
22
# File 'lib/hmap/helper/utils.rb', line 14

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



24
25
26
# File 'lib/hmap/helper/utils.rb', line 24

def self.effective_platforms_names(platforms)
  platforms.flat_map { |name| effective_platform_name(name) }.compact.uniq
end


73
74
75
76
77
78
79
80
81
82
83
# File 'lib/hmap/helper/utils.rb', line 73

def self.file_symlink_to(path, filepath)
  return unless path.exist?

  filepath.dirname.mkpath unless filepath.exist?

  if Gem.win_platform?
    FileUtils.ln(path, filepath, force: true)
  else
    FileUtils.ln_sf(path, filepath)
  end
end

.hash_set_value(hash, *args) ⇒ Object



47
48
49
50
51
52
# File 'lib/hmap/helper/utils.rb', line 47

def self.hash_set_value(hash, *args)
  args.each do |arg|
    hash.merge(arg)
  end
  hash
end

.index_of_range(num, range) ⇒ Object



28
29
30
31
# File 'lib/hmap/helper/utils.rb', line 28

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

.magic?(magic) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/hmap/helper/utils.rb', line 89

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



37
38
39
40
41
42
43
44
45
# File 'lib/hmap/helper/utils.rb', line 37

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

Returns:

  • (Boolean)


33
34
35
# File 'lib/hmap/helper/utils.rb', line 33

def self.power_of_two?(num)
  num != 0 && (num & (num - 1)).zero?
end

.safe_encode(string, target_encoding) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/hmap/helper/utils.rb', line 93

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



54
55
56
57
# File 'lib/hmap/helper/utils.rb', line 54

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

.string_downcase_hash(str) ⇒ Object



59
60
61
# File 'lib/hmap/helper/utils.rb', line 59

def self.string_downcase_hash(str)
  str.downcase.bytes.inject(:+) * 13
end

.swapped_magic?(magic, version) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/hmap/helper/utils.rb', line 85

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



63
64
65
66
67
68
69
70
71
# File 'lib/hmap/helper/utils.rb', line 63

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