Class: String

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

Constant Summary collapse

PATTERN_SJIS =
'[\x81-\x9f\xe0-\xef][\x40-\x7e\x80-\xfc]'
PATTERN_EUC =
'[\xa1-\xfe][\xa1-\xfe]'
PATTERN_UTF8 =
'[\xc0-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf][\x80-\xbf]'
RE_SJIS =
Regexp.new(PATTERN_SJIS, 0, 'n')
RE_EUC =
Regexp.new(PATTERN_EUC, 0, 'n')
RE_UTF8 =
Regexp.new(PATTERN_UTF8, 0, 'n')
SUCC =
{}
HashCache =
{}
TrPatternCache =
{}
DeletePatternCache =
{}
SqueezePatternCache =
{}

Instance Method Summary collapse

Instance Method Details

#chopObject


196
197
198
# File 'lib/jcode.rb', line 196

def chop
  (str = self.dup).chop! or str
end

#chop!Object


192
193
194
# File 'lib/jcode.rb', line 192

def chop!
  self.gsub!(/(?:.|\r?\n)\z/, '')
end

#delete(del) ⇒ Object


156
157
158
# File 'lib/jcode.rb', line 156

def delete(del)
  (str = self.dup).delete!(del) or str
end

#delete!(del) ⇒ Object


151
152
153
154
# File 'lib/jcode.rb', line 151

def delete!(del)
  return nil if del == ""
  self.gsub!(DeletePatternCache[del] ||= /[#{_regex_quote(del)}]+/, '')
end

#each_charObject


209
210
211
212
213
214
215
216
217
# File 'lib/jcode.rb', line 209

def each_char
  if block_given?
    scan(/./m) do |x|
      yield x
    end
  else
    scan(/./m)
  end
end

#end_regexpObject


59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/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) ⇒ Object


205
206
207
# File 'lib/jcode.rb', line 205

def jcount(str)
  self.delete("^#{str}").jlength
end

#jlengthObject Also known as: jsize


200
201
202
# File 'lib/jcode.rb', line 200

def jlength
  self.gsub(/[^\Wa-zA-Z_\d]/, ' ').length
end

#mbchar?Boolean

Returns:

  • (Boolean)

46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/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) ⇒ Object


171
172
173
# File 'lib/jcode.rb', line 171

def squeeze(del=nil)
  (str = self.dup).squeeze!(del) or str
end

#squeeze!(del = nil) ⇒ Object


160
161
162
163
164
165
166
167
168
169
# File 'lib/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

#succObject Also known as: original_succ


92
93
94
95
# File 'lib/jcode.rb', line 92

def succ
  str = self.dup
  str.succ! or str
end

#succ!Object Also known as: original_succ!


78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/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

#tr(from, to) ⇒ Object


147
148
149
# File 'lib/jcode.rb', line 147

def tr(from, to)
  (str = self.dup).tr!(from, to) or str
end

#tr!(from, to) ⇒ Object


133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/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) ⇒ Object


188
189
190
# File 'lib/jcode.rb', line 188

def tr_s(from, to)
  (str = self.dup).tr_s!(from,to) or str
end

#tr_s!(from, to) ⇒ Object


175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/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