3 using System.Collections;
4 using System.Collections.Generic;
11 public enum MTextFormat
13 MTEXT_FORMAT_US_ASCII,
15 MTEXT_FORMAT_UTF_16BE,
16 MTEXT_FORMAT_UTF_16LE,
17 MTEXT_FORMAT_UTF_32BE,
18 MTEXT_FORMAT_UTF_32LE,
22 public class MTextProperty
28 internal enum Flag : byte
37 public MSymbol Key { get { return key; } }
38 public object Val { get { return val; } }
39 public bool FrontSticky
41 get { return (flags & Flag.FrontSticky) != Flag.None; }
43 public bool RearSticky
45 get { return (flags & Flag.RearSticky) != Flag.None; }
49 get { return (flags & Flag.Sensitive) != Flag.None; }
52 public MTextProperty (MSymbol key, object val)
56 flags |= Flag.RearSticky;
59 public MTextProperty (MSymbol key, object val,
60 bool front_sticky, bool rear_sticky, bool sensitive)
65 flags |= Flag.FrontSticky;
67 flags |= Flag.RearSticky;
69 flags |= Flag.Sensitive;
72 public override string ToString ()
74 return key.ToString () + ":" + val;
78 public class MText : IEnumerable, IEquatable<MText>, IComparable<MText>
81 public enum MTextFormat format;
83 private StringBuilder sb;
85 private int cache_pos;
86 private int cache_idx;
87 private MPlist intervals;
88 private MPlist default_property;
89 private bool read_only;
91 private static UTF8Encoding utf8 = new UTF8Encoding ();
93 private static int count_chars (String str)
95 int len = str.Length, n = 0;
97 for (int i = 0; i < len; i++, n++)
98 if (surrogate_high_p (str[i]))
103 private static int count_chars (StringBuilder str)
105 int len = str.Length, n = 0;
107 for (int i = 0; i < len; i++, n++)
108 if (surrogate_high_p (str[i]))
115 sb = new StringBuilder ();
116 intervals = new MPlist ();
119 public MText (byte[] str)
121 sb = new StringBuilder (utf8.GetString (str));
122 nchars = count_chars (sb);
123 intervals = new MPlist ();
126 public MText (String str)
128 sb = new StringBuilder (str);
129 nchars = count_chars (str);
130 intervals = new MPlist ();
133 public MText (StringBuilder str)
136 nchars = count_chars (str);
137 intervals = new MPlist ();
140 public static MText operator+ (MText mt1, MText mt2)
142 MText mt = new MText ();
144 mt.sb.Append (mt1.sb);
145 mt.sb.Append (mt2.sb);
146 mt.nchars = mt1.nchars + mt2.nchars;
151 public bool ReadOnly { get { return read_only; } }
152 public int Length { get { return nchars; } }
156 // for IEnumerable interface
157 public IEnumerator GetEnumerator() { return new MTextEnum (this); }
159 // for IEquatable interface
160 public bool Equals (MText other) { return this.sb.Equals (other.sb); }
162 // for IComparable interface
163 public int CompareTo (MText other)
165 return this.sb.ToString ().CompareTo (other.sb.ToString ());
168 public override String ToString () { return "\"" + sb.ToString () + "\""; }
170 private static bool surrogate_high_p (char c)
172 return (c >= 0xD800 && c < 0xDC00);
175 private static bool surrogate_low_p (char c)
177 return (c >= 0xDC00 && c < 0xE000);
180 private static int inc_idx (StringBuilder sb, int i)
182 return (i + (surrogate_high_p (sb[i]) ? 2 : 1));
185 private static int dec_idx (StringBuilder sb, int i)
187 return (i - (surrogate_low_p (sb[i - 1]) ? 2 : 1));
190 private static int pos_to_idx (MText mt, int pos)
192 if (pos == mt.cache_pos)
198 if (pos < mt.cache_pos)
200 if (mt.cache_pos == mt.cache_idx)
202 if (pos < mt.cache_pos - pos)
209 p = mt.cache_pos; i = mt.cache_idx;
215 if (mt.nchars - mt.cache_pos == mt.sb.Length - mt.cache_idx)
216 return (mt.cache_idx + pos - mt.cache_pos);
217 if (pos - mt.cache_pos < mt.nchars - pos)
219 p = mt.cache_pos; i = mt.cache_idx;
224 p = mt.nchars; i = mt.sb.Length;
229 for (; p < pos; i = inc_idx (mt.sb, i), p++);
231 for (; p > pos; i = dec_idx (mt.sb, i), p--);
237 private void check_pos (int pos, bool tail_ok)
239 if (pos < 0 || (tail_ok ? pos > nchars : pos >= nchars))
240 throw new Exception ("Invalid MText position:" + pos);
243 private bool check_range (int from, int to, bool zero_ok)
245 if (from < 0 || (zero_ok ? from > to : from >= to)
247 throw new Exception ("Invalid MText range");
251 private void insert (int pos, MText mt2, int from, int to)
253 check_pos (pos, true);
255 int pos_idx = pos_to_idx (this, pos);
256 int from_idx = pos_to_idx (mt2, from);
257 int to_idx = pos_to_idx (mt2, to);
259 sb.Insert (pos_idx, mt2.sb.ToString (from_idx, to_idx - from_idx));
262 foreach (MPlist plist in mt2.intervals)
263 if (intervals.Find (plist.Key) == null)
264 intervals.Push (plist.Key, new MInterval (plist.Key, this));
265 foreach (MPlist plist in intervals)
267 MPlist p = mt2.intervals.Find (plist.Key);
271 interval = new MInterval (plist.Key, this, to - from);
273 interval = ((MInterval) p.Val).Copy (from, to);
274 ((MInterval) plist.Val).Insert (pos, interval);
278 private void insert (int pos, int c)
280 check_pos (pos, true);
282 int pos_idx = pos_to_idx (this, pos);
287 sb.Insert (pos_idx, ch);
291 char high = (char) (0xD800 + ((c - 0x10000) >> 10));
292 char low = (char) (0xDC00 + ((c - 0x10000) & 0x3FF));
293 sb.Insert (pos_idx, low);
294 sb.Insert (pos_idx, high);
297 foreach (MPlist plist in intervals)
298 ((MInterval) plist.Val).Insert (pos,
299 new MInterval (plist.Key, this, 1));
302 public int this[int i]
305 i = pos_to_idx (this, i);
308 if (surrogate_high_p (sb[i]))
310 sb[i] = (char) value;
314 char high = (char) (0xD800 + ((value - 0x10000) >> 10));
315 char low = (char) (0xDC00 + ((value - 0x10000) & 0x3FF));
317 if (! surrogate_high_p (sb[i]))
324 i = pos_to_idx (this, i);
325 return (surrogate_high_p (sb[i])
326 ? ((sb[i] - 0xD800) << 10) + (sb[i + 1] - 0xDC00) + 0x10000
333 return (new MText (sb.ToString ()));
336 public MText Ins (int pos, int c)
342 public MText Ins (int pos, MText mt)
344 insert (pos, mt, 0, mt.nchars);
348 public MText Ins (int pos, MText mt, int from, int to)
350 insert (pos, mt, from, to);
354 public MText Cat (int c)
360 public MText Del (int from, int to)
362 if (check_range (from, to, true))
365 sb.Remove (from, pos_to_idx (this, to) - pos_to_idx (this, from));
369 foreach (MPlist plist in intervals)
370 ((MInterval) plist.Val).Delete (from, to);
372 intervals = new MPlist ();
376 public object GetProp (int pos, MSymbol key)
378 check_pos (pos, false);
380 MInterval i = (MInterval) intervals.Find (key).Val;
385 MTextProperty prop = i.Get (pos);
386 return (prop != null ? prop.Val : null);
389 public object GetProp (int pos, MSymbol key, out MTextProperty prop)
391 check_pos (pos, false);
393 MInterval i = (MInterval) intervals.Find (key).Val;
396 return (prop = null);
398 return (prop != null ? prop.Val : null);
401 public object GetProp (int pos, MSymbol key, out MTextProperty[] array)
403 check_pos (pos, false);
405 MInterval i = (MInterval) intervals.Find (key).Val;
408 return (array = null);
409 MTextProperty prop = i.Get (pos, out array);
410 return (prop != null ? prop.Val : null);
413 public void PushProp (int from, int to, MSymbol key, object val)
415 if (! check_range (from, to, true))
416 PushProp (from, to, new MTextProperty (key, val));
419 public void PushProp (int from, int to, MTextProperty prop)
423 if (default_property == null)
424 default_property = new MPlist ();
425 default_property.Push (prop.key, prop.val);
429 if (check_range (from, to, true))
432 MPlist p = intervals.Find (prop.key);
437 root = new MInterval (prop.key, this);
438 intervals.Push (prop.key, root);
441 root = (MInterval) p.Val;
443 root.Push (from, to, prop);
447 public void PopProp (int from, int to, MSymbol key)
451 if (default_property == null)
453 MPlist p = default_property.Find (key);
460 if (check_range (from, to, true))
463 MPlist p = intervals.Find (key);
466 ((MInterval) p.Val).Pop (from, to);
470 public void DumpProp ()
473 foreach (MPlist p in intervals)
474 ((MInterval) p.Val).Dump (true);
475 Console.WriteLine (")");
478 private class MInterval
480 // position: 0 1 2 3 4 5 6 7
481 // | A | B | C | D | E F | G |
482 // interval |---|---|---|<->|-------|---|
483 // |---|<->|---| |<----->|---|
487 // [3 (1 2)] [3 (4 6)]
488 // [1 (0 1)] [2 (2 3)] [1 (6 7)]
490 private static int count = 0;
493 private int From, To;
495 private MPlist Stack;
496 private MInterval Left, Right, Parent;
499 public MInterval (MSymbol key, MText mt, int length)
502 throw new Exception ("Invalid interval length");
506 Stack = new MPlist ();
510 public MInterval (MSymbol key, MText mt)
514 Length = mt.sb.Length;
517 Stack = new MPlist ();
521 public MTextProperty Get (int pos)
523 MInterval i = find (pos);
525 return (i.Stack.IsEmpty ? null : (MTextProperty) i.Stack.Val);
528 public MTextProperty Get (int pos, out MTextProperty[] array)
530 MInterval i = find (pos);
537 array = new MTextProperty[i.Stack.Count];
541 for (idx = 0, p = i.Stack; ! p.IsEmpty; idx++, p = p.Next)
542 array[idx] = (MTextProperty) p.Val;
543 return array[idx - 1];
546 private MInterval (MSymbol key, MText mt, int length, MPlist stack)
553 Stack = stack.Clone ();
557 private void update_from_to ()
562 To = Length - RightLength;
564 else if (Parent.Left == this)
566 From = Parent.From - Length + LeftLength;
567 To = Parent.From - RightLength;
571 From = Parent.To + LeftLength;
572 To = Parent.To + Length - RightLength;
576 private int LeftLength
578 get { return (Left == null ? 0 : Left.Length); }
581 private int RightLength
583 get { return (Right == null ? 0 : Right.Length); }
586 private MInterval LeftMost
588 get { return (Left == null ? this : Left.LeftMost); }
591 private MInterval RightMost
593 get { return (Right == null ? this : Right.RightMost); }
596 private MInterval Prev {
601 for (i = Left; i.Right != null; i = i.Right);
604 MInterval child = this;
605 for (i = Parent; i != null && i.Left == child;
606 child = i, i = i.Parent);
612 private MInterval Next {
617 for (i = Right; i.Left != null; i = i.Left);
620 MInterval child = this;
621 for (i = Parent; i != null && i.Right == child;
622 child = i, i = i.Parent);
628 private MInterval find (int pos)
632 return Left.find (pos);
634 return Right.find (pos);
638 private bool mergeable (MInterval i)
642 for (p1 = Stack, p2 = i.Stack; ! p1.IsEmpty && ! p2.IsEmpty;
643 p1 = p1.Next, p2 = p2.Next)
644 if (p1.Val != p2.Val)
646 return (p1.IsEmpty && p2.IsEmpty);
649 // p-. or .-p p-. or .-p
650 // .-this-. .-right-.
651 // left .-right-. -> .-this-. c2
653 private MInterval promote_right ()
655 int right_length = Right.Length;
659 mtext.intervals.Put (Key, Right);
660 else if (Parent.Left == this)
663 Parent.Right = Right;
664 Right.Parent = Parent;
670 Parent.Length += Length;
671 Length -= right_length;
675 Parent.Length -= c1.Length;
681 // p-. or .-p p-. or .-p
683 // .-left-. .-right-. -> c1 .-this-.
685 private MInterval promote_left ()
687 int left_length = Left.Length;
691 mtext.intervals.Put (Key, Left);
692 else if (Parent.Left == this)
696 Left.Parent = Parent;
702 Parent.Length += Length;
703 Length -= left_length;
707 Parent.Length -= c1.Length;
713 private MInterval balance ()
720 // .-left-. .-right-.
722 int diff = i.LeftLength - i.RightLength;
727 new_diff = (i.Length - i.LeftLength
728 + i.Left.RightLength - i.Left.LeftLength);
729 if (Math.Abs (new_diff) >= diff)
731 i = i.promote_left ();
736 new_diff = (i.Length - i.RightLength
737 + i.Right.LeftLength - i.Right.RightLength);
738 if (Math.Abs (new_diff) >= diff)
740 i = i.promote_right ();
747 public MInterval Copy (int start, int end)
749 MInterval copy, left_copy = null, right_copy = null;
755 return Left.Copy (start, end);
756 left_copy = Left.Copy (start, From);
761 return Right.Copy (start, end);
762 right_copy = Right.Copy (To, end);
765 copy = new MInterval (Key, null, end - start, Stack);
766 remove_properties (MTextProperty.Flag.Sensitive);
767 copy.Left = left_copy;
768 copy.Right = right_copy;
776 private MInterval divide_right (int pos)
778 MInterval interval = new MInterval (Key, mtext, To - pos, Stack);
780 M17N.DebugPrint ("divide-right({0}) at ", pos); DumpOne (false, true);
784 interval.Right = Right;
785 Right.Parent = interval;
786 interval.Length += Right.Length;
788 interval.Parent = this;
796 private MInterval divide_left (int pos)
798 MInterval interval = new MInterval (Key, mtext, pos - From, Stack);
800 M17N.DebugPrint ("divide-left({0}) at ", pos); DumpOne (false, true);
804 interval.Left = Left;
805 Left.Parent = interval;
806 interval.Length += Left.Length;
808 interval.Parent = this;
813 private void remove_properties (MTextProperty.Flag flags)
815 for (MPlist p = Stack; ! p.IsEmpty;)
817 MTextProperty prop = (MTextProperty) p.Val;
819 if ((prop.flags & flags) == flags)
826 private void inherit_front_properties (MPlist plist)
828 for (MInterval i = LeftMost; i != null; i = i.Next)
832 for (MPlist p = plist; ! p.IsEmpty; p = p.Next)
834 MTextProperty prop = (MTextProperty) p.Val;
836 if ((prop.flags & MTextProperty.Flag.RearSticky)
837 == MTextProperty.Flag.RearSticky)
838 i.Stack.Add (prop.key, prop);
843 private void inherit_rear_properties (MPlist plist)
845 for (MInterval i = RightMost; i != null; i = i.Prev)
849 for (MPlist p = plist; ! p.IsEmpty; p = p.Next)
851 MTextProperty prop = (MTextProperty) p.Val;
853 if ((prop.flags & MTextProperty.Flag.FrontSticky)
854 == MTextProperty.Flag.FrontSticky)
855 i.Stack.Add (prop.key, prop);
860 private MInterval delete_node_forward ()
864 int len = Length - RightLength;
866 for (MInterval i = Parent; i != null; i = i.Parent)
868 if (Parent.Left == this)
871 Parent.Right = Right;
876 Right.Parent = Parent;
877 return Right.LeftMost;
882 private MInterval delete_node_backward ()
886 int len = Length - RightLength;
888 for (MInterval i = Parent; i != null; i = i.Parent)
890 if (Parent.Left == this)
898 Left.Parent = Parent;
899 return Left.RightMost;
904 private void set_mtext (MText mt)
910 Right.set_mtext (mt);
913 private MInterval graft (MInterval interval, bool forward, out int len)
920 i = interval.LeftMost;
925 len += i.Length - i.RightLength;
926 i = i.delete_node_forward ();
931 i = interval.RightMost;
936 len += i.Length - i.LeftLength;
937 i = i.delete_node_backward ();
943 for (MInterval prev = this, ii = this.Parent; ii != null;
944 prev = ii, ii = ii.Parent)
954 while (i.Parent != null) i = i.Parent;
958 public void Insert (int pos, MInterval interval)
961 M17N.DebugPrint ("insert({0}) at {1} in ", interval.Length, pos);
962 DumpOne (false, true);
964 interval.set_mtext (mtext);
967 Prev.Insert (pos, interval);
968 else if (pos == From)
970 MInterval prev = Prev;
976 prev.Insert (pos, interval);
979 prev.remove_properties
980 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.RearSticky);
981 interval.inherit_front_properties (prev.Stack);
984 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.FrontSticky);
985 interval.inherit_rear_properties (Stack);
988 interval = graft (interval, false, out len);
989 if (interval != null && prev != null)
990 interval = prev.graft (interval, true, out len);
991 if (interval != null)
997 // .-this-. ==> .-this-.
1009 interval.Parent = i;
1010 for (; i != null; i = i.Parent)
1011 i.Length += interval.Length;
1016 remove_properties (MTextProperty.Flag.Sensitive);
1019 interval = graft (interval, true, out len);
1021 if (interval != null)
1022 interval = graft (interval, false, out len);
1023 if (interval != null)
1026 Right.Left = interval;
1027 interval.Parent = Right;
1028 for (MInterval i = Right; i != null; i = i.Parent)
1029 i.Length += interval.Length;
1034 MInterval next = Next;
1040 next.Insert (pos, interval);
1043 next.remove_properties
1044 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.FrontSticky);
1045 interval.inherit_rear_properties (next.Stack);
1048 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.RearSticky);
1049 interval.inherit_front_properties (Stack);
1052 interval = graft (interval, true, out len);
1053 if (interval != null && next != null)
1054 interval = next.graft (interval, false, out len);
1055 if (interval != null)
1061 // .-this-. ==> .-this-.
1074 interval.Parent = i;
1075 for (; i != null; i = i.Parent)
1076 i.Length += interval.Length;
1080 Next.Insert (pos, interval);
1083 private void vacate_node (MInterval interval)
1085 M17N.DebugPrint ("vacate #{0} to #{1}", ID, interval.ID);
1086 if (interval != null)
1087 interval.Parent = Parent;
1091 mtext.intervals.Put (Key, interval);
1095 if (this == Parent.Right)
1096 Parent.Right = interval;
1098 Parent.Left = interval;
1101 if (interval != null)
1102 diff -= interval.Length;
1103 for (MInterval i = Parent; i != null; i = i.Parent)
1108 public void Delete (int start, int end)
1111 M17N.DebugPrint ("delete({0} {1}) from ", start, end); DumpOne (false, true);
1116 Left.Delete (start, end);
1119 Left.Delete (start, From);
1121 end -= From - start;
1128 Right.Delete (start, end);
1131 Right.Delete (To, end);
1134 if (start == From && end == To)
1146 for (i = Right; i.Left != null; i = i.Left)
1147 i.Length += Left.Length;
1148 i.Length += Left.Length;
1152 vacate_node (Right);
1157 int len = end - start;
1159 for (MInterval i = this; i != null; i = i.Parent)
1164 public void Push (int start, int end, MTextProperty prop)
1167 M17N.DebugPrint ("push({0} {1}) at ", start, end); DumpOne (false, true);
1172 Left.Push (start, end, prop);
1175 Left.Push (start, From, prop);
1182 Right.Push (start, end, prop);
1185 Right.Push (To, end, prop);
1190 divide_left (start);
1193 Stack.Push (prop.key, prop);
1196 private bool try_merge_prev ()
1198 MInterval prev = Prev;
1200 if (! mergeable (prev))
1203 int len = prev.Length - prev.LeftLength;
1205 // PREV is Left, Left.Right, ..., or Left....Right.
1208 prev.Parent.Right = prev.Left;
1209 while (prev.Parent != Left)
1216 if (Left.Length == Left.LeftLength)
1221 private bool try_merge_next ()
1223 MInterval next = Next;
1225 if (! mergeable (next))
1228 int len = next.Length - next.LeftLength;
1230 // NEXT is Right, Right.Left, ..., or Right....Left.
1233 next.Parent.Left = next.Right;
1234 while (next.Parent != Right)
1240 Right.Length -= len;
1241 if (Right.Length == Right.LeftLength)
1248 public void Pop (int start, int end)
1251 M17N.DebugPrint ("pop({0} {1}) at ", start, end); DumpOne (false, true);
1256 Left.Pop (start, end);
1259 Left.Pop (start, From);
1266 Right.Pop (start, end);
1269 Right.Pop (To, end);
1273 if (! Stack.IsEmpty)
1275 bool check_prev = start == From && start > 0;
1276 bool check_next = end == To && end < mtext.Length;
1279 divide_left (start);
1283 if (check_prev && Left != null)
1284 check_prev = try_merge_prev () && (Left != null);
1285 if (check_next && Right != null)
1286 check_next = try_merge_next () && (Right != null);
1289 if (Prev.try_merge_next () && check_next)
1290 Prev.try_merge_next ();
1292 else if (check_next)
1294 Next.try_merge_prev ();
1299 private void DumpOne (bool with_prop, bool newline)
1301 DumpOne (with_prop, newline, false);
1304 private void DumpOne (bool with_prop, bool newline, bool force)
1306 if (force || M17N.debug)
1308 Console.Write ("#{0}({1} {2} {3}", ID, Length, From, To);
1310 foreach (MPlist p in Stack)
1311 Console.Write (" " + p.Val);
1312 Console.Write (")");
1314 Console.WriteLine ();
1318 public void Dump () { Dump (false); }
1321 public void Dump (bool force)
1323 if (force || M17N.debug)
1330 Console.Write (" ");
1331 DumpOne (true, false, force);
1338 private class MTextEnum : IEnumerator
1341 private int pos = -1;
1343 public MTextEnum (MText mt)
1348 public bool MoveNext ()
1351 return (pos < mt.nchars);
1354 public void Reset ()
1359 public object Current
1362 //if (pos < 0 || pos >= mt.nchars)
1363 //throw new InvalidOperationException ();