8 public enum MTextFormat
10 MTEXT_FORMAT_US_ASCII,
12 MTEXT_FORMAT_UTF_16BE,
13 MTEXT_FORMAT_UTF_16LE,
14 MTEXT_FORMAT_UTF_32BE,
15 MTEXT_FORMAT_UTF_32LE,
22 public enum MTextFormat format;
25 private class MTextPlist : MPlist
27 public class MInterval
31 public int start, end;
32 public MInterval prev, next;
37 public MTextPlist (MText mt)
39 head = tail = new MInterval ();
41 head.end = mt.sb.Length;
45 private StringBuilder sb;
47 private int cache_pos;
48 private int cache_idx;
49 private MTextPlist plist;
51 private static UTF8Encoding utf8 = new UTF8Encoding ();
52 private static UTF32Encoding utf32 = new UTF32Encoding (true, false);
54 private static int count_chars (String str)
56 int len = str.Length, n = 0;
58 for (int i = 0; i < len; i++)
59 n += surrogate_high_p (str[i]) ? 2 : 1;
63 private static int count_chars (StringBuilder str)
65 int len = str.Length, n = 0;
67 for (int i = 0; i < len; i++)
68 n += surrogate_high_p (str[i]) ? 2 : 1;
74 sb = new StringBuilder ();
77 public MText (byte[] str)
79 sb = new StringBuilder (utf8.GetString (str));
80 nchars = count_chars (sb);
83 public MText (String str)
85 sb = new StringBuilder (str);
86 nchars = count_chars (str);
89 public MText (StringBuilder str)
92 nchars = count_chars (str);
95 public static MText operator+ (MText mt1, MText mt2)
97 return new MText (mt1.sb + mt2.sb);
100 private static bool surrogate_high_p (char c)
102 return (c >= 0xD800 && c < 0xDC00);
105 private static bool surrogate_low_p (char c)
107 return (c >= 0xDC00 && c < 0xE000);
110 private int inc_idx (int i)
112 return (i + (surrogate_high_p (sb[i]) ? 2 : 1));
115 private int dec_idx (int i)
117 return (i - (surrogate_low_p (sb[i - 1]) ? 2 : 1));
120 private int pos_to_idx (int pos)
127 if (cache_pos == cache_idx)
129 if (pos < cache_pos - pos)
136 p = cache_pos; i = cache_idx;
142 if (nchars - cache_pos == sb.Length - cache_idx)
143 return (cache_idx + pos - cache_pos);
144 if (pos - cache_pos < nchars - pos)
146 p = cache_pos; i = cache_idx;
151 p = nchars; i = sb.Length;
156 for (; p < pos; i = inc_idx (i), p++);
158 for (; p > pos; i = dec_idx (i), p--);
164 public int this[int i]
170 sb[i] = (char) value;
173 char high = (char) (0xD800 + ((i - 0x10000) >> 10));
174 char low = (char) (0xDC00 + ((i - 0x10000) & 0x3FF));
176 if (! surrogate_high_p (sb[i]))
185 return (surrogate_high_p (sb[i])
186 ? i = ((sb[i] - 0xD800) << 10) + (sb[i + 1] - 0xDC00) + 0x10000
192 public class MTextProperty