OSDN Git Service

Add folder functions.
[mave/mave.git] / mave_base.rb
index 8c2c04d..f3148ae 100644 (file)
@@ -54,14 +54,19 @@ class String
        #               http://tools.ietf.org/html/rfc2047
        #
        @@decoders = {}
+       @@current_decode_charset = 'UTF-8'
 
        def self.bind_decoder(code)
                @@decoders[code.upcase] = Proc.new
        end
 
+       def self.set_decode_charset(charset)
+               @@current_decode_charset = charset
+       end
+
        def decode_mh                                                                                           # decode message header
                gsub(/=\?([^?]+)\?(B|Q)\?([^?]+)\?=/i) {
-                       $3.decode_ec($2).decode_cs('UTF-8', $1)                         #### now fixed utf-8
+                       $3.decode_ec($2).decode_cs(@@current_decode_charset, $1)
                }.gsub(/[\x00-\x1F]/, '^x')
        end
 
@@ -110,7 +115,8 @@ class String
        #               http://ja.wikipedia.org/wiki/UTF-8
        #
        def enspc
-               self.gsub(/[\xC0-\xE2][\x80-\xBF]+/) {|c|                               #### いーかげん
+#              return(self)                                                                                    # UTF-8 以外ならコメントを生かす
+               self.gsub(/[\xC0-\xE2][\x80-\xBF]+/) {|c|                               #### for UTF-8 いーかげん
                        c + '_'
                }
        end
@@ -212,8 +218,8 @@ String.bind_decoder('B') {|str|                                                                     # Base64 decoder
 String.bind_decoder('Q') {|str|                                                                        # Quoted Printable decoder
        str.unpack('M')[0]
 }
-String.bind_decoder('US-ASCII') {|str, out_code|                               # us-ascii(auto) decoder
-       str.kconv(String.charset(out_code))
+String.bind_decoder('US-ASCII') {|str, out_code|                               # us-ascii decoder
+       str
 }
 String.bind_decoder('ISO-2022-JP') {|str, out_code|                            # iso-2022-jp decoder
        str.kconv(String.charset(out_code), Kconv::JIS)
@@ -238,10 +244,16 @@ String.bind_decoder('UTF-7') {|str, out_code|                                     # utf-7 decoder
 
 #### String.center                                                                                             # センタリングする
 
+# 11bit: 0xC0-0xDF 0x80-0xBF
+# 16bit: 0xE0-0xEF 0x80-0xBF 0x80-0xBF
+# 21bit: 0xF0-0xF7 0x80-0xBF 0x80-0xBF 0x80-0xBF
+# 26bit: 0xF8-0xFB 0x80-0xBF 0x80-0xBF 0x80-0xBF 0x80-0xBF
+# 31bit: 0xFC-0xFD 0x80-0xBF 0x80-0xBF 0x80-0xBF 0x80-0xBF 0x80-0xBF
+
 String.bind_snipper('UTF-8') {|str, n|                                                 # 指定の長さに切り詰める
        ws = str[0, n * 2].gsub(/[\xC0-\xFD][\x80-\xBF]+/, "\xFF\xFF")[0, n]
        wc = ws.count("\xFF")
-       str.slice(0, n + wc / 2 - wc % 2).enspc + ' ' * (n - ws.length + wc % 2)
+       str.slice(0, n + wc / 2 - wc % 2) + ' ' * (n - ws.length + wc % 2)
 }
 
 String.bind_each_snipper('UTF-8') {|str, n, max, proc|                 # 指定の長さに切り詰め、順に行を渡す
@@ -249,23 +261,27 @@ String.bind_each_snipper('UTF-8') {|str, n, max, proc|                    # 指定の長さに切
                break if((max -= 1) < 0)
                ws = str[p, n * 2].gsub(/[\xC0-\xFD][\x80-\xBF]+/, "\xFF\xFF")[0, n]
                wc = ws.count("\xFF")
-               proc.call(str.slice(p, nn = n + wc / 2 - wc % 2).enspc + ' ' * (n - ws.length + wc % 2))
+               proc.call(str.slice(p, nn = n + wc / 2 - wc % 2) + ' ' * (n - ws.length + wc % 2))
                p += nn
        end
 }
 
+#     漢字: 0xA1-0xFE 0xA1-0xFE
+# 半角カナ: 0x8E 0xA1-0xDF 
+# 補助漢字: 0x8F 0xA1-0xFE 0xA1-0xFE
+
 String.bind_snipper('EUC-JP') {|str, n|                                                        #### 指定の長さに切り詰める
-       ws = str[0, n * 2].gsub(/[\x80-\xFF][\x80-\xFF]+/, "\xFF\xFF")[0, n]
+       ws = str[0, n * 2].gsub(/[\xA1-\xFE][\xA1-\xFE]/, "\xFF\xFF")[0, n]
        wc = ws.count("\xFF")
-       str.slice(0, n + wc / 2 - wc % 2) + ' ' * (n - ws.length + wc % 2)
+       str.slice(0, n - wc % 2) + ' ' * (n - ws.length + wc % 2)
 }
 
 String.bind_each_snipper('EUC-JP') {|str, n, max, proc|                        #### 指定の長さに切り詰め、順に行を渡す
        p = 0; while(p <= str.length)                                                           # '<': 改行文字のみの行は省略
                break if((max -= 1) < 0)
-               ws = str[p, n * 2].gsub(/[\x80-\xFF][\x80-\xFF]+/, "\xFF\xFF")[0, n]
+               ws = str[p, n * 2].gsub(/[\xA1-\xFE][\xA1-\xFE]/, "\xFF\xFF")[0, n]
                wc = ws.count("\xFF")
-               proc.call(str.slice(p, nn = n + wc / 2 - wc % 2) + ' ' * (n - ws.length + wc % 2))
+               proc.call(str.slice(p, nn = n - wc % 2) + ' ' * (n - ws.length + wc % 2))
                p += nn
        end
 }