Class: String

Inherits:
Object show all
Defined in:
lib/javaclass/string_ux.rb,
lib/javaclass/java_name.rb

Overview

Add some unpack helper methods for HI-LO byte order (network byte order) contained in this String.

Author

Peter Kofler

Constant Summary collapse

TYPES =
[JavaClass::JavaClassFileName, JavaClass::JavaVMName, JavaClass::JavaPackageName, JavaClass::JavaQualifiedName]
RUBY19 =
''.respond_to? :codepoints

Instance Method Summary collapse

Instance Method Details

#double(index = 0) ⇒ Object

Return the index’th and the next 7 elements as double precision float.



46
47
48
# File 'lib/javaclass/string_ux.rb', line 46

def double(index=0)
  self[index..index+7].unpack('G')[0]
end

#single(index = 0) ⇒ Object

Return the index’th and the next 3 elements as single precision float.

See

steve.hollasch.net/cgindex/coding/ieeefloat.html

See

blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/196633



41
42
43
# File 'lib/javaclass/string_ux.rb', line 41

def single(index=0)
  self[index..index+3].unpack('g')[0]
end

#to_javanameObject

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
11
12
13
14
# File 'lib/javaclass/string_ux.rb', line 8

def u1(index=0)
  if RUBY19
    self[index..index].unpack('C')[0]
  else
    self[index]
  end
end

#u2(index = 0) ⇒ Object

Return the index’th and the next element as unsigned word.



17
18
19
20
# File 'lib/javaclass/string_ux.rb', line 17

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.



24
25
26
# File 'lib/javaclass/string_ux.rb', line 24

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.



29
30
31
# File 'lib/javaclass/string_ux.rb', line 29

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.



34
35
36
# File 'lib/javaclass/string_ux.rb', line 34

def u8(index=0)
  u4(index) * 256**4 + u4(index+4)
end