Module: BitStream::Utils
- Defined in:
- lib/bitstream/utils.rb,
lib/types/string-utils.rb
Class Method Summary collapse
- .bit_lshift(s, bit) ⇒ Object
- .bit_rshift(s, bit) ⇒ Object
- .camel2snake(camel) ⇒ Object
- .class2symbol(type) ⇒ Object
Class Method Details
.bit_lshift(s, bit) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/types/string-utils.rb', line 10 def self.bit_lshift(s, bit) if bit != 0 last_byte = nil (s.size - 1).times do |i| dbyte = s[i..(i + 1)].unpack('n')[0] c = dbyte >> (8 - bit) s[i] = (c & 0xff).chr end end end |
.bit_rshift(s, bit) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/types/string-utils.rb', line 21 def self.bit_rshift(s, bit) if bit != 0 s << "\0" first_byte = nil (s.size - 1).downto 1 do |i| dbyte = s[(i - 1)..i].unpack('n')[0] c = dbyte >> bit s[i] = (c & 0xff).chr first_byte = c >> 8 end s[0] = first_byte.chr end end |
.camel2snake(camel) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/bitstream/utils.rb', line 14 def self.camel2snake(camel) snake = camel.dup snake[0] = snake[0].downcase snake.gsub(/[A-Z]/) do |s| "_" + s.downcase end end |
.class2symbol(type) ⇒ Object
9 10 11 12 |
# File 'lib/bitstream/utils.rb', line 9 def self.class2symbol(type) name = type.name.split("::").last name = self.camel2snake(name).intern end |