class String

coding:utf-8

Constants

ByteByByte

TODO obsolete

FirstChar
RomanRe

Public Instance Methods

-(str) click to toggle source
# File lib/rdoba/common.rb, line 95
def -(str)
  #TODO make smart search for match in the 'str', when only last subpart matched to 'self'
  len = self.size
  bc = ec = nil
  (0...len).each do |idx|
    break bc = idx if self[idx] == str[0]
  end
  ((bc + 1)...len).each do |idx|
    break ec = idx if self[idx] != str[idx - bc]
  end if bc
  (not bc) ? self.clone : (not ec) ? self[0, bc] : self[0, bc] + self[ec, len - ec]
end
=~(value) click to toggle source
# File lib/rdoba/common.rb, line 109
def =~(value)
  if value.class == String
    self == value
  elsif value.class == Regexp
    value =~ self
  else
    __match__(value)
  end
end
Also aliased as: __match__
__match__(value)
Alias for: =~
__scanf__(format, &block)
Alias for: scanf
_rdoba_to_i(base = 10, *opts)
Alias for: to_i
consolize() click to toggle source
# File lib/rdoba/io.rb, line 137
def consolize
  ss = StringScanner.new(self)
  res = ''
  ostr = ''
  pos = 0

  while ss.scan_until(/\r/)
    ostr[0...ss.pre_match.size - pos] = ss.pre_match[pos..-1]
    pos = ss.pos

    if ss.post_match[0] == "\n"[0]
      res = ostr
      pos += 1
      ostr = ''
    end

  end

  ostr[0...ss.rest.size] = ss.rest
  res + ostr
end
fe() click to toggle source
# File lib/rdoba/fe.rb, line 4
def fe
  if RUBY_VERSION < '1.9'
    self
  else
    return self unless Encoding.default_internal
    if self.frozen?
      self.dup.force_encoding(Encoding.default_internal || 'UTF-8').freeze
    else
      self.force_encoding(Encoding.default_internal || 'UTF-8')
    end
  end
end
Also aliased as: fenc
fenc()
Alias for: fe
hexdump() click to toggle source
# File lib/rdoba/common.rb, line 123
def hexdump
  res= ''
  i = 0
  self.each_byte do |byte|
    res << sprintf("%.2X ", byte)
    i += 1
    res << "\n" if i % 16 == 0
  end
  res
end
rmatch(value) click to toggle source
# File lib/rdoba/common.rb, line 119
def rmatch(value)
  self == value || self =~ /^\/([^\/]+)/ && value =~ /#{$1}/
end
rom() click to toggle source
# File lib/rdoba/roman.rb, line 37
def rom
   numbers = self.upcase.scan(RomanRe).flatten.map { |x| Numeric::RomanToInteger[x] }
   numbers.sort do |x, y|
      if x < y
         raise "Invalid roman number" ;end
      0 ;end
   numbers.sum ;end
scanf(format, &block) click to toggle source
# File lib/rdoba/io.rb, line 95
def scanf(format, &block)
  (re, argfs) = scanf_re(format)

  ss = StringScanner.new(self)
  res = []
  rline = []
  while ss.scan_until(re)
    argfs.each_index do |i|
      argf = argfs[i]
      value = ss[i + 1]
      rline << case argf[3]
      when 'x'
        value.to_i(16)
      when /[diu]/
        value.to_i
      when /[efg]/
        value.to_f
      when 'c'
        argf[2] ? value.to_i(:be) : value.to_i
      when 'b'
        value.to_i(2)
      when 'o'
        value.to_i(8)
      when 's'
        value
      when 'r'
        value.rom
      end
    end

    if block_given?
      pass = []
      (1..block.arity).each do |i| pass << "rline[#{i}]" end
      eval "yield(#{pass.join(', ')})"
    end

    res << rline
  end

  res
end
Also aliased as: __scanf__
scanf_re(format) click to toggle source
# File lib/rdoba/io.rb, line 50
def scanf_re(format)
  fss = StringScanner.new(format) # TODO remove scanner in favor of match
  nformat = ''

  pos = 0
  argfs = []
  while fss.scan_until(/%([0-9 #+\-*]*)(?:\.([0-9]+)(\+)?)?([bcdefgiosuxr])/)
    argfs << [ fss[1], fss[2], fss[3], fss[4] ]
    # TODO add performing the special case in fss[1]
    nformat += fss.pre_match[pos..-1].to_res + case fss[4]
    when 'x'
      '(?:0[xX])?([a-fA-F0-9]+)'
    when 'i'
      '([+\-]?[0-9]+)'
    when 'u'
      '([0-9]+)'
    when 'e'
      '([+\-]?[0-9]+[eE][+\-]?[0-9]+)'
    when 'f'
      '([+\-]?[0-9]+\.[0-9]*)'
    when 'g'
      '([+\-]?[0-9]+(?:[eE][+\-]?[0-9]+|\.[0-9]*))'
    when 'c'
      fss[2] ? "(.{1,#{fss[2]}})" : "(.)"
    when 'b'
      '([01]+)b?'
    when 'o'
      '0([0-9]+)'
    when 'd'
      '([+\-]?(?:0X)?[A-F0-9.+]+)'
    when 's'
      '(.+)'
    when 'r'
      '([IVXLCDMivxlcdm]+)'
    end

    pos = fss.pos
  end

  nformat += fss.rest

  [ /#{nformat}/, argfs ]
end
to_i(base = 10, *opts) click to toggle source
# File lib/rdoba/numeric.rb, line 9
def to_i(base = 10, *opts)
  v = parse_opts(opts)
  if v[:be]
    (str, sign, num) = (self.match /\s*(-?)([0-9a-fx]+)/u).to_a
    if str
      n = num.reverse._rdoba_to_i(base)
      sign.empty? && n || -n
    else
      0
    end
  else
    _rdoba_to_i(base)
  end
end
Also aliased as: _rdoba_to_i
to_re() click to toggle source
# File lib/rdoba/re.rb, line 18
def to_re
  /#{to_res}/ui
end
to_res() click to toggle source
# File lib/rdoba/re.rb, line 5
def to_res
  ostr = self.dup
  res = ''
  while true
    m = ostr.match(/(?:([+\[\]\\().*?{}^$\/|])|«([^«]*)»)/u)
    break unless m
    res += m.pre_match + (m[2] || m[1] && ('\\' + m[1]))
    ostr = m.post_match
  end

  res + ostr
end