#!/usr/bin/ruby -Ks
# copyright (c) 2001 ZnZ (Kazuhiro NISHIYAMA), all rights reserved.

require 'nkf'

module ISO_2022_JP
  MAP = {
    '♯' => '#',
    '、' => ',',
    '。' => '.',
  }
  '　！”＃＄％＆’（）＊＋，−．／０１２３４５６７８９：；＜＝＞？＠ＡＢＣＤＥＦＧＨＩＪＫＬＭＮＯＰＱＲＳＴＵＶＷＸＹＺ［￥］＾＿｀ａｂｃｄｅｆｇｈｉｊｋｌｍｎｏｐｑｒｓｔｕｖｗｘｙｚ｛｜｝￣'.split(//).each_with_index do |ch,i|
    c = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'[i].chr
    MAP[c] = ch
    MAP[ch] = c
  end

  # iso-2022-jpでのバイト数を小さくする。
  def min(kcode_string)
    kcode_string.collect do |line|
      eol = '' # 行末を保存
      h = lasth = {''=>[0,:usascii]}
      line.sub(/\r?\n?\z/){eol=$&;''}.split(//).each do |ch|
        h = {}
        lasth.each do |laststr,(lastlen,lasttype)|
          if MAP.key?(ch) && (lasttype != :usascii || /^[\x00-\x7F]$/ =~ MAP[ch])
            [ch, MAP[ch]]
          else
            [ch]
          end.each do |c|
            if /^[\x00-\x7F]$/ === c
              chtype = :usascii
            else
              chtype = :other
            end
            len = c.size
            if lasttype != chtype && lasttype == :usascii
              len += 6
            end
            h[laststr+c] = [lastlen+len, chtype]
          end
        end
        lasth = h
      end
      h.each do |str,(len,_)| #
        jis = NKF.nkf('-jS -m0', str)
        unless jis.size == len
          p '[BUG]',len,[str.size,str],[jis.size,jis]
        end
      end #
#h.each{|k,v|
#  puts "he:#{v[0]}\t#{k}"
#}
      h.keys.min{|a,b|h[a][0]<=>h[b][0]}+eol
    end.join('')
  end
  module_function :min
end

if __FILE__ == $0
  DATA.each do |line|
    next if /^#/ === line
    next if /^$/ === line
    p ['元文字列:',line.size,line]
    jlin = NKF.nkf('-jS -m0', line)
    p [' 元→JIS:',jlin.size,jlin]
    minl = ISO_2022_JP.min(line)
    p ['後文字列:',minl.size, minl]
    jmnl = NKF.nkf('-jS -m0', minl)
    p [' 後→JIS:',jmnl.size,jmnl]
  end
end
__END__
あabcdefい
あａｂｃｄｅｆい
abcdefあい
あいａｂｃｄｅｆ

