Class: String
- Defined in:
- lib/javaclass/string_20.rb,
lib/javaclass/java_name.rb,
lib/javaclass/string_ux.rb,
lib/javaclass/string_hexdump.rb
Overview
Add some hexdump
helper method to dump the data contained in this String.
- Author
-
Peter Kofler
Direct Known Subclasses
JavaClass::JavaClassFileName, JavaClass::JavaPackageName, JavaClass::JavaQualifiedName, JavaClass::JavaVMName
Constant Summary collapse
- TYPES =
[JavaClass::JavaClassFileName, JavaClass::JavaVMName, JavaClass::JavaPackageName, JavaClass::JavaQualifiedName]
- RUBY19 =
''.respond_to? :codepoints
Instance Method Summary collapse
-
#byte_at(index = 0) ⇒ Object
Return the index’th element as byte.
-
#double(index = 0) ⇒ Object
Return the index’th and the next 7 elements as double precision float.
-
#hexdump(columns = 16) ⇒ Object
Return the hex dump of this String with columns columns of hexadecimal numbers per line.
- #number_bytes ⇒ Object
- #same_bytes_as?(other) ⇒ Boolean
-
#single(index = 0) ⇒ Object
Return the index’th and the next 3 elements as single precision float.
- #strip_non_printable ⇒ Object
-
#to_javaname ⇒ Object
Convert a Java classname or Java class filename to special String with methods to work with Java class or package names.
-
#u1(index = 0) ⇒ Object
Return the index’th element as byte.
-
#u2(index = 0) ⇒ Object
Return the index’th and the next element as unsigned word.
-
#u2rep(count = 1, index = 0) ⇒ Object
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.
-
#u4(index = 0) ⇒ Object
Return the index’th and the next 3 elements as unsigned dword.
-
#u8(index = 0) ⇒ Object
Return the index’th and the next 7 elements as unsigned qword.
Instance Method Details
#byte_at(index = 0) ⇒ Object
Return the index’th element as byte.
8 9 10 11 12 13 14 |
# 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 |
#double(index = 0) ⇒ Object
Return the index’th and the next 7 elements as double precision float.
42 43 44 |
# File 'lib/javaclass/string_ux.rb', line 42 def double(index=0) self[index..index+7].unpack('G')[0] end |
#hexdump(columns = 16) ⇒ Object
Return the hex dump of this String with columns columns of hexadecimal numbers per line.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# 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 |
#number_bytes ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/javaclass/string_20.rb', line 24 def number_bytes if RUBY19 self.bytesize else self.length end end |
#same_bytes_as?(other) ⇒ Boolean
16 17 18 19 20 21 22 |
# 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 |
#single(index = 0) ⇒ Object
Return the index’th and the next 3 elements as single precision float.
37 38 39 |
# File 'lib/javaclass/string_ux.rb', line 37 def single(index=0) self[index..index+3].unpack('g')[0] end |
#strip_non_printable ⇒ Object
32 33 34 35 36 37 38 |
# 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 |
#to_javaname ⇒ Object
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.
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# 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 |
#u1(index = 0) ⇒ Object
Return the index’th element as byte.
8 9 10 |
# File 'lib/javaclass/string_ux.rb', line 8 def u1(index=0) self.byte_at(index) end |
#u2(index = 0) ⇒ Object
Return the index’th and the next element as unsigned word.
13 14 15 16 |
# 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 |
#u2rep(count = 1, index = 0) ⇒ Object
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.
20 21 22 |
# File 'lib/javaclass/string_ux.rb', line 20 def u2rep(count=1, index=0) self[index...index+count*2].unpack('n'*count) end |
#u4(index = 0) ⇒ Object
Return the index’th and the next 3 elements as unsigned dword.
25 26 27 |
# File 'lib/javaclass/string_ux.rb', line 25 def u4(index=0) self[index..index+3].unpack('N')[0] end |
#u8(index = 0) ⇒ Object
Return the index’th and the next 7 elements as unsigned qword.
30 31 32 |
# File 'lib/javaclass/string_ux.rb', line 30 def u8(index=0) u4(index) * 256**4 + u4(index+4) end |