update.
[chise/ruby.git] / chise / org-string.rb
1 class String
2   def to_utf8()
3     return to_a.map {|ch|
4       ch.char.to_utf8
5     }.join("")
6   end
7
8   def map_char!(block = Proc.new)
9     return unless block_given?
10     self.replace(self.map_char {|ch| block.call(ch)})
11   end
12
13   def map_character(block = Proc.new)
14     return unless block_given?
15     return self.to_a.map {|ch| (block.call(ch.char)).to_s }.join("")
16   end
17
18   def map_character!(block = Proc.new)
19     return unless block_given?
20     self.replace(self.map_char {|ch| block.call(ch.char)})
21   end
22
23   def map_utf8() map_char {|ch| ch.char.map_utf8 } end
24   alias map_ucs map_utf8
25
26   def map_ucs_er() map_char {|ch| ch.char.map_ucs_er } end
27   def to_er() map_char {|ch| ch.char.to_er } end
28
29   #put\8aÖ\8cW\81A[]\8aÖ\8cW\82Í\97p\88Ó\82µ\82È\82¢\82±\82Æ\82É\82µ\82½\81B
30
31   def inspect_all() map_char {|ch| ch.char.inspect_all } end
32   def inspect_x()   map_char {|ch| ch.char.inspect_x   } end
33
34 # def to_euc()   map_char {|ch| ch.char.to_euc   } end
35   def map_euc()  map_char {|ch| ch.char.map_euc  } end
36 # def to_sjis()  map_char {|ch| ch.char.to_sjis  } end
37   def map_sjis() map_char {|ch| ch.char.map_sjis } end
38
39   def glyph_decompose() map_char {|ch| ch.char.glyph_decompose } end
40 #  def decompose!() self.replace(self.decompose); self; end
41
42   def nu_decompose_all(level=nil)
43     level = 0 if level.nil?
44     if 10 < level
45       p ["too many recursive", self] 
46       exit
47     end
48     de = self.decompose
49     return de.decompose_all(level+1) if de != self #\82È\82É\82©\95Ï\89»\82ª\82 \82Á\82½\82©\82ç\8dÄ\8bA
50     de #\82à\82¤\82±\82ê\88È\8fã\95Ï\89»\82Í\96³\82³\82»\82¤\82¾\82¼\82Æ\81B
51   end
52
53   def decompose_all!() self.replace(self.decompose_all); self; end
54
55   def find() #"\93ú\89_"\81¨"\93Ü"\82Æ\82©\82¢\82¤\8a´\82\82Ì\91\80\8dì
56     ar = []
57     length = char_length()
58     each_char {|ch|
59       char = ch.char
60       ar << char.ids_contained #\82»\82Ì\95\8e\9a\82ð\8aÜ\82ñ\82Å\82¢\82é\8a¿\8e\9a\82Ì\83\8a\83X\83g
61     }
62     h = Hash.new(0)
63     ar.each {|list|
64       next if list.nil?
65       list.each_char {|ch|
66         h[ch] += 1
67       }
68     }
69     str = ""
70     h.each {|k, v|
71       #      p [k, v]
72       if length == v #\91S\95\94\82É\8aç\82ð\8fo\82µ\82Ä\82¢\82½\82ç
73         str += k
74       end
75     }
76     #    p str
77     str
78   end
79
80 end