class String
Constants
- DeletePatternCache
- HashCache
- PATTERN_EUC
- PATTERN_SJIS
- PATTERN_UTF8
- RE_EUC
- RE_SJIS
- RE_UTF8
- SUCC
- SqueezePatternCache
- TrPatternCache
Public Instance Methods
chop()
click to toggle source
# File lib/rubysl/jcode/jcode.rb, line 196 def chop (str = self.dup).chop! or str end
chop!()
click to toggle source
# File lib/rubysl/jcode/jcode.rb, line 192 def chop! self.gsub!(/(?:.|\r?\n)\z/, '') end
delete(del)
click to toggle source
# File lib/rubysl/jcode/jcode.rb, line 156 def delete(del) (str = self.dup).delete!(del) or str end
delete!(del)
click to toggle source
# File lib/rubysl/jcode/jcode.rb, line 151 def delete!(del) return nil if del == "" self.gsub!(DeletePatternCache[del] ||= /[#{_regex_quote(del)}]+/, '') end
each_char() { |x| ... }
click to toggle source
# File lib/rubysl/jcode/jcode.rb, line 209 def each_char if block_given? scan(/./m) do |x| yield x end else scan(/./m) end end
end_regexp()
click to toggle source
# File lib/rubysl/jcode/jcode.rb, line 59 def end_regexp case $KCODE[0] when ?s, ?S /#{PATTERN_SJIS}$/on when ?e, ?E /#{PATTERN_EUC}$/on when ?u, ?U /#{PATTERN_UTF8}$/on else /.$/on end end
jcount(str)
click to toggle source
# File lib/rubysl/jcode/jcode.rb, line 205 def jcount(str) self.delete("^#{str}").jlength end
jlength()
click to toggle source
# File lib/rubysl/jcode/jcode.rb, line 200 def jlength self.gsub(/[^\Wa-zA-Z_\d]/, ' ').length end
Also aliased as: jsize
mbchar?()
click to toggle source
# File lib/rubysl/jcode/jcode.rb, line 46 def mbchar? case $KCODE[0] when ?s, ?S self =~ RE_SJIS when ?e, ?E self =~ RE_EUC when ?u, ?U self =~ RE_UTF8 else nil end end
squeeze(del=nil)
click to toggle source
# File lib/rubysl/jcode/jcode.rb, line 171 def squeeze(del=nil) (str = self.dup).squeeze!(del) or str end
squeeze!(del=nil)
click to toggle source
# File lib/rubysl/jcode/jcode.rb, line 160 def squeeze!(del=nil) return nil if del == "" pattern = if del SqueezePatternCache[del] ||= /([#{_regex_quote(del)}])\1+/ else /(.|\n)\1+/ end self.gsub!(pattern, '\1') end
succ()
click to toggle source
# File lib/rubysl/jcode/jcode.rb, line 92 def succ str = self.dup str.succ! or str end
Also aliased as: original_succ
succ!()
click to toggle source
# File lib/rubysl/jcode/jcode.rb, line 78 def succ! reg = end_regexp if $KCODE != 'NONE' && self =~ reg succ_table = SUCC[$KCODE[0,1].downcase] begin self[-1] += succ_table[self[-1]] self[-2] += 1 if self[-1] == 0 end while self !~ reg self else original_succ! end end
Also aliased as: original_succ!
tr(from, to)
click to toggle source
# File lib/rubysl/jcode/jcode.rb, line 147 def tr(from, to) (str = self.dup).tr!(from, to) or str end
tr!(from, to)
click to toggle source
# File lib/rubysl/jcode/jcode.rb, line 133 def tr!(from, to) return nil if from == "" return self.delete!(from) if to == "" pattern = TrPatternCache[from] ||= /[#{_regex_quote(from)}]/ if from[0] == ?^ last = /.$/.match(to)[0] self.gsub!(pattern, last) else h = HashCache[from + "1-0" + to] ||= expand_ch_hash(from, to) self.gsub!(pattern) do |c| h[c] end end end
tr_s(from, to)
click to toggle source
# File lib/rubysl/jcode/jcode.rb, line 188 def tr_s(from, to) (str = self.dup).tr_s!(from,to) or str end
tr_s!(from, to)
click to toggle source
# File lib/rubysl/jcode/jcode.rb, line 175 def tr_s!(from, to) return self.delete!(from) if to.length == 0 pattern = SqueezePatternCache[from] ||= /([#{_regex_quote(from)}])\1*/ if from[0] == ?^ last = /.$/.match(to)[0] self.gsub!(pattern, last) else h = HashCache[from + "1-0" + to] ||= expand_ch_hash(from, to) self.gsub!(pattern) do h[$1] end end end
Private Instance Methods
_expand_ch(str)
click to toggle source
# File lib/rubysl/jcode/jcode.rb, line 99 def _expand_ch str a = [] str.scan(/(?:\\(.)|([^\\]))-(?:\\(.)|([^\\]))|(?:\\(.)|(.))/m) do from = $1 || $2 to = $3 || $4 one = $5 || $6 if one a.push one elsif from.length != to.length next elsif from.length == 1 from[0].upto(to[0]) { |c| a.push c.chr } else from.upto(to) { |c| a.push c } end end a end
_regex_quote(str)
click to toggle source
# File lib/rubysl/jcode/jcode.rb, line 11 def _regex_quote(str) str.gsub(/(\\[\[\]\-\\])|\\(.)|([\[\]\\])/) do $1 || $2 || '\\' + $3 end end
expand_ch_hash(from, to)
click to toggle source
# File lib/rubysl/jcode/jcode.rb, line 118 def expand_ch_hash from, to h = {} afrom = _expand_ch(from) ato = _expand_ch(to) afrom.each_with_index do |x,i| h[x] = ato[i] || ato[-1] end h end