update.
[chise/ruby.git] / chise / string.rb
1 # Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
2
3 require "chise/character"
4 require "chise/parser"
5 require "chise/ids"
6
7 class String
8   include CHISE::StringIDS
9
10   # copied from htree/encoder.rb
11   UTF8_RE = /\A(?:
12         [\x00-\x7f]
13        |[\xc0-\xdf][\x80-\xbf]
14        |[\xe0-\xef][\x80-\xbf][\x80-\xbf]
15        |[\xf0-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]
16        |[\xf8-\xfb][\x80-\xbf][\x80-\xbf][\x80-\xbf][\x80-\xbf]
17        |[\xfc-\xfd][\x80-\xbf][\x80-\xbf][\x80-\xbf][\x80-\xbf][\x80-\xbf])\Z/nx
18
19   def is_a_utf8? # Is this string one character in UTF-8?
20     (UTF8_RE =~ self) != nil
21   end
22
23   def char
24     raise unless is_a_utf8?
25     CHISE::Character.get("?"+self)
26   end
27
28   def method_missing(mid, *args, &block)
29     #char.method_missing(mid, *args)
30     char.send(mid, *args, &block)
31   end
32
33   def to_a
34     self.split(//u)
35   end
36
37   def char_length
38     to_a.length
39   end
40
41   def each_char
42     to_a.each {|c|
43       yield(c)
44     }
45   end
46
47   def map_char
48     to_a.map {|c|
49       yield(c).to_s
50     }.join
51   end
52
53   def each_character
54     to_a.each {|ch|
55       yield ch.char
56     }
57   end
58
59   def de_er()
60     pa = CHISE::EntityReferenceParser.new
61     pa.de_er(self)
62   end
63
64   def to_ids
65     CHISE::IDS.new(self)
66   end
67
68 end