[#1058] ruby 0.99.3-961119 available — matz@... (Yukihiro Matsumoto)

まつもと ゆきひろです.

19 messages 1996/11/19
[#1059] segm fault (Re ruby 0.99.3-961119 available) — sinara@... 1996/11/19

原です。

[ruby-list:1143] Re: Japanese and tr

From: WATANABE Hirofumi <watanabe@...>
Date: 1996-11-29 01:41:51 UTC
List: ruby-list #1143
渡辺@ソニーです.

jcode.pl からぱくりました. 叩いてください. たぶん EUC と 
SJIS なら動くと思います.

-- 
わたなべひろふみ


def _expand1(str)
    str.gsub!(/\\(.)/, '\1')
    s, e = str.unpack('CxC')
    if s <= e
	str = ''
	for i in s .. e
	    str += [i].pack('C');
	end
    end
    str
end

def _expand2(str)
    s1, s2, e1, e2 = str.unpack('CCxCC');
    if s1 == e1 && s2 <= e2
	str = ''
	for i in s2 .. e2
	    str += [s1, i].pack('CC');
	end
    end
    str
end

def jtr(str, from, to)
    table = {}
    oldkcode = $KCODE
    $KCODE = 'n'
    ascii = "[\000-\177]"
    kanji = "[\200-\377][\000-\377]-[\200-\377][\000-\377]"
    pattern1 = /#{ascii + '-' + ascii}/
    pattern2 = /#{kanji}/

    from.gsub!(pattern2) {_expand2($&)}
    to.gsub!(pattern2) {_expand2($&)}
    from.gsub!(pattern1) {_expand1($&)}
    to.gsub!(pattern1) {_expand1($&)}

    $KCODE = oldkcode

    ato = to.split(//)
    afrom = from.split(//)

    ato.fill(ato[ato.size-1], ato.size .. afrom.size) if ato.size < afrom.size
    for i in 0 .. ato.size - 1
	table[afrom[i]] = ato[i]
    end

    str.split(//).collect{ |c|
	if table[c]
	    table[c]
	else
	    c
	end
    }.join("")
end

print jtr('[RUBY-LIST:1105]', 'A-ZA-Z0-9', 'a-zA-Z0-9'), "\n";

In This Thread