*** empty log message ***
[m17n/m17n-lib-cs.git] / MText.cs
1 using System;
2 using System.Text;
3 using M17N.Core;
4
5 namespace M17N.Core
6 {
7 #if false
8   public enum MTextFormat
9   {
10     MTEXT_FORMAT_US_ASCII,
11     MTEXT_FORMAT_UTF_8,
12     MTEXT_FORMAT_UTF_16BE,
13     MTEXT_FORMAT_UTF_16LE,
14     MTEXT_FORMAT_UTF_32BE,
15     MTEXT_FORMAT_UTF_32LE,
16   }
17 #endif
18
19   public class MText
20   {
21 #if false
22     public enum MTextFormat format;
23 #endif
24
25     private class MTextPlist : MPlist
26     {
27       public class MInterval
28       {
29         MPlist stack;
30         int nprops;
31         public int start, end;
32         public MInterval prev, next;
33       }
34
35       MInterval head, tail;
36
37       public MTextPlist (MText mt)
38       {
39         head = tail = new MInterval ();
40         head.start = 0;
41         head.end = mt.sb.Length;
42       }
43     }
44
45     private StringBuilder sb;
46     private int cache_pos;
47     private int cache_idx;
48     private MTextPlist plist;
49
50     private static UTF8Encoding utf8 = new UTF8Encoding ();
51     private static UTF32Encoding utf32 = new UTF8Encoding ();
52
53     public MText ()
54     {
55       cache_pos = cache_idx = 0;
56       plist = null;
57       sb = new StringBuilder ();
58     }
59
60     public MText (byte[] str)
61     {
62       sb = new StringBuilder (utf8.GetString (str));
63     }
64
65     public MText (String str)
66     {
67       sb = new StringBuilder (str);
68     }
69
70     private static int inc_idx (int i)
71     {
72       return ((sb[i] >= 0xD800 && sb[i] < 0xDC00)
73               ? i + 2 : i + 1);
74     }
75
76     private static int dec_idx (int i)
77     {
78       return ((sb[i - 1] >= 0xDC00 && sb[i - 1] < 0xE000)
79               ? i - 2 : i - 1);
80     }
81
82     private static int pos_to_idx (int pos)
83     {
84       int p, i;
85       bool forward;
86
87       if (pos < cache_pos)
88         {
89           if (cache_pos == cache_idx)
90             return cache_idx;
91           if (pos < cache_pos - pos)
92             {
93               p = i = 0;
94               forward = true;
95             }
96           else
97             {
98               p = cache_pos; i = cache_idx;
99               forward = false;
100             }
101         }
102       else
103         {
104           if (nchars - cache_pos == sb.Length - cache_idx)
105             return (cache_idx + pos - cache_pos);
106           if (pos - cache_pos < nchars - pos)
107             {
108               p = char_pos; i = char_idx;
109               forward = true;
110             }
111           else
112             {
113               p = nchars; i = sb.Length;
114               forward = false;
115             }
116         }
117       if (forward)
118         for (; p < pos; i = inc_idx (i), p++);
119       else
120         for (; p > pos; i = dec_idx (i), p--);
121       cache_pos = p;
122       cache_idx = i;
123       return i;
124     }
125
126     public int this[int i]
127     {
128       set {
129         if (i != cache_pos)
130           i = pos_to_idx (i);
131         if (value < 0x10000)
132           this.sb[i] = (char) value;
133         else
134           {
135             
136           }
137       }
138       get { return this.sb[i]; }
139     }
140
141   public class MTextProperty
142   {
143   }
144 }