# jcode.rb - ruby code to handle japanese (EUC/SJIS) string
if $VERBOSE && $KCODE == "NONE"
warn "Warning: $KCODE is NONE."
$vsave, $VERBOSE = $VERBOSE, false
warn "feel free for some warnings:\n" if $VERBOSE
str.gsub(/(\\[\[\]\-\\])|\\(.)|([\[\]\\])/) do
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['s'][i.chr] = 0x40 - i
SUCC['s']["\x7e"] = 0x80 - 0x7e
SUCC['s']["\xfd"] = 0x100 - 0xfd
SUCC['s']["\xfe"] = 0x100 - 0xfe
SUCC['s']["\xff"] = 0x100 - 0xff
SUCC['e'][i.chr] = 0xa1 - i
SUCC['u'][i.chr] = 0x80 - i
SUCC['u']["\xbf"] = 0x100 - 0xbf
alias original_succ! succ!
if $KCODE != 'NONE' && self =~ reg
succ_table = SUCC[$KCODE[0,1].downcase]
self[-1] += succ_table[self[-1]]
self[-2] += 1 if self[-1] == 0
str.scan(/(?:\\(.)|([^\\]))-(?:\\(.)|([^\\]))|(?:\\(.)|(.))/m) do
elsif from.length != to.length
from[0].upto(to[0]) { |c| a.push c.chr }
from.upto(to) { |c| a.push c }
def expand_ch_hash from, to
afrom.each_with_index do |x,i| h[x] = ato[i] || ato[-1] end
return self.delete!(from) if to == ""
pattern = TrPatternCache[from] ||= /[#{_regex_quote(from)}]/
self.gsub!(pattern, last)
h = HashCache[from + "1-0" + to] ||= expand_ch_hash(from, to)
self.gsub!(pattern) do |c| h[c] end
(str = self.dup).tr!(from, to) or str
self.gsub!(DeletePatternCache[del] ||= /[#{_regex_quote(del)}]+/, '')
(str = self.dup).delete!(del) or str
SqueezePatternCache[del] ||= /([#{_regex_quote(del)}])\1+/
self.gsub!(pattern, '\1')
(str = self.dup).squeeze!(del) or str
return self.delete!(from) if to.length == 0
pattern = SqueezePatternCache[from] ||= /([#{_regex_quote(from)}])\1*/
self.gsub!(pattern, last)
h = HashCache[from + "1-0" + to] ||= expand_ch_hash(from, to)
self.gsub!(pattern) do h[$1] end
(str = self.dup).tr_s!(from,to) or str
self.gsub!(/(?:.|\r?\n)\z/, '')
(str = self.dup).chop! or str
self.gsub(/[^\Wa-zA-Z_\d]/, ' ').length
self.delete("^#{str}").jlength