Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/gonzui/util.rb

Instance Method Summary collapse

Instance Method Details

#clearObject



119
120
121
# File 'lib/gonzui/util.rb', line 119

def clear
  replace("")
end

#each_line_range(byteno, ncontexts) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/gonzui/util.rb', line 146

def each_line_range(byteno, ncontexts)
  raise unless block_given?
  lines = []
  center = self.line_range(byteno)
  lines.push([0, center])

  head = center.first
  ncontexts.times {|i|
    pos = head - 2
    pos += 1 if self[pos] == ?\n # empty line
    break if pos <= 0
    range = self.line_range(pos)
    head = range.first
    lines.unshift([0 - i - 1, range])
  }

  tail = center.last
  ncontexts.times {|i|
    pos = tail + 1
    break if pos >= self.length
    range = self.line_range(pos)
    tail = range.last
    lines.push([i + 1, range])
  }

  lines.each {|lineno_offset, range|
    yield(lineno_offset, range)
  }
end

#line_range(byteno) ⇒ Object



136
137
138
139
140
141
142
143
144
# File 'lib/gonzui/util.rb', line 136

def line_range(byteno)
  head = if self[byteno] == ?\n
           byteno
         else
           (self.rindex(?\n, byteno) or -1) + 1
         end
  tail = (self.index(?\n, byteno) or  self.length)
  return (head...tail)
end

#prechopObject



123
124
125
126
127
128
129
# File 'lib/gonzui/util.rb', line 123

def prechop
  if m = /^./m.match(self)
    return m.post_match
  else
    return ""
  end
end

#substring(range) ⇒ Object



131
132
133
134
# File 'lib/gonzui/util.rb', line 131

def substring(range)
  raise unless range.exclude_end?
  self[range.first, range.last - range.first]
end

#untabifyObject



176
177
178
179
180
181
182
183
184
185
# File 'lib/gonzui/util.rb', line 176

def untabify
  new = ""
  self.each_line {|line|
    true while line.gsub!(/\t+/) { 
      ' ' * ($&.length * 8 - $`.length % 8)  #`)
    }
    new << line
  }
  return new
end