class String
Compatibility methods to work with Ruby 1.8, 1.9 and 2.0 Strings.
- Author
-
Peter Kofler
Add some hexdump
helper method to dump the data contained in this String
.
- Author
-
Peter Kofler
Add some unpack
helper methods for HI-LO byte order (network byte order) contained in this String
.
- Author
-
Peter Kofler
Constants
- RUBY19
- TYPES
Public Instance Methods
Return the index'th element as byte.
# File lib/javaclass/string_20.rb, line 8 def byte_at(index=0) if RUBY19 self[index..index].unpack('C')[0] else self[index] end end
Return the index'th and the next 7 elements as double precision float.
# File lib/javaclass/string_ux.rb, line 42 def double(index=0) self[index..index+7].unpack('G')[0] end
Return the hex dump of this String
with columns columns of hexadecimal numbers per line.
# File lib/javaclass/string_hexdump.rb, line 8 def hexdump(columns=16) return StringLineHexdumper.empty(columns).format if size == 0 input = [0, []] lines = 1..number_hexdump_lines(columns) output = lines.inject(input) { |result, line_index| offset, previous_lines = *result part = hexdump_line(line_index, columns) line = StringLineHexdumper.new(offset, columns, part).format [ offset + columns, previous_lines + [line] ] } lines = output[1] lines.join end
# File lib/javaclass/string_20.rb, line 24 def number_bytes if RUBY19 self.bytesize else self.length end end
# File lib/javaclass/string_20.rb, line 16 def same_bytes_as?(other) if RUBY19 self.unpack('C*') == other.unpack('C*') else self == other end end
Return the index'th and the next 3 elements as single precision float.
- See
- See
# File lib/javaclass/string_ux.rb, line 37 def single(index=0) self[index..index+3].unpack('g')[0] end
# File lib/javaclass/string_20.rb, line 32 def strip_non_printable if RUBY19 self.unpack('C*').map { |c| if c < 32 or c > 127 then 46 else c end }.pack('C*') else self.gsub(Regexp.new("[^ -\x7f]", nil, 'n'), '.') end end
Convert a Java classname or Java class filename to special String
with methods to work with Java class or package names. If it's a pathname then it must be relative to the classpath. Source extension and additional JVM method declarations are dropped.
# File lib/javaclass/java_name.rb, line 348 def to_javaname match = TYPES.find { |type| type.valid?(self) } if match return match.new(self) end plain_name = self.sub(/#{JavaClass::JavaLanguage::SOURCE_REGEX}|".*$|\.<.*$/, '').gsub(/\\/, '/') match = TYPES.find { |type| type.valid?(plain_name) } if match match.new(plain_name) else raise ArgumentError, "unknown Java name #{self}" end end
Return the index'th element as byte.
# File lib/javaclass/string_ux.rb, line 8 def u1(index=0) self.byte_at(index) end
Return the index'th and the next element as unsigned word.
# File lib/javaclass/string_ux.rb, line 13 def u2(index=0) self[index..index+1].unpack('n')[0] # self[index]*256 + self[index+1] end
Return the index'th and the next element as unsigned word, repeat it for count words in total and return an array of these words.
# File lib/javaclass/string_ux.rb, line 20 def u2rep(count=1, index=0) self[index...index+count*2].unpack('n'*count) end
Return the index'th and the next 3 elements as unsigned dword.
# File lib/javaclass/string_ux.rb, line 25 def u4(index=0) self[index..index+3].unpack('N')[0] end
Return the index'th and the next 7 elements as unsigned qword.
# File lib/javaclass/string_ux.rb, line 30 def u8(index=0) u4(index) * 256**4 + u4(index+4) end
Private Instance Methods
# File lib/javaclass/string_hexdump.rb, line 31 def hexdump_line(index, columns=16) from = (index-1) * columns to = index * columns - 1 self[from..to] end
# File lib/javaclass/string_hexdump.rb, line 27 def number_hexdump_lines(columns=16) (self.number_bytes + columns - 1) / columns end