Class: String

Inherits:
Object show all
Defined in:
lib/rdoba/fe.rb,
lib/rdoba/io.rb,
lib/rdoba/re.rb,
lib/rdoba/mixin.rb,
lib/rdoba/roman.rb,
lib/rdoba/common.rb,
lib/rdoba/numeric.rb,
lib/rdoba/strings.rb

Direct Known Subclasses

BCDString

Constant Summary collapse

ByteByByte =

TODO obsolete

0
FirstChar =
1

Instance Method Summary collapse

Instance Method Details

#-(str) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# 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) ⇒ Object



109
110
111
112
113
114
115
116
117
# 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

#__match__Object



108
# File 'lib/rdoba/common.rb', line 108

alias :__match__ :=~

#_rdoba_to_iObject



8
# File 'lib/rdoba/numeric.rb', line 8

alias :_rdoba_to_i :to_i

#consolizeObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# 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

#feObject Also known as: fenc



4
5
6
7
8
9
10
11
12
13
14
15
# 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

#hexdumpObject



123
124
125
126
127
128
129
130
131
132
# 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) ⇒ Object



119
120
121
# File 'lib/rdoba/common.rb', line 119

def rmatch(value)
  self == value || self =~ /^\/([^\/]+)/ && value =~ /#{$1}/
end

#romObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rdoba/roman.rb', line 27

def rom
  h = Numeric::Roman.reverse
  keys = h.keys.sort do |x,y| x.size < y.size ? 1 : x.size > y.size ? -1 : x <=> y end
  str = self.upcase
  res = 0
  while str and not str.empty?
  raise "Invalid roman number" if (keys.each do |key|
    if str =~ /^#{key}(.*)/
      str = $1
      res += h[key]
      break nil
    end
    end)
  end
  res
end

#scanf(format, &block) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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

#scanf_re(format) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

#to_reObject



18
19
20
# File 'lib/rdoba/re.rb', line 18

def to_re
  /#{to_res}/ui
end

#to_resObject



5
6
7
8
9
10
11
12
13
14
15
16
# 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