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 MProperty
28 /// A text inserted before a character that has this property
29 /// inherits this property. If the text already has properties
30 /// of the same key, they are deleted.
32 /// A text inserted after a character that has this property
33 /// inherits this property. If the text already has properties
34 /// of the same key, they are deleted.
36 /// This property is deleted from a span of text if the span is
37 /// modified (i.e. a character is changed, a text is inserted,
38 /// some part is deleted). This propery is also deleted if a
39 /// property of the same key is added, which means that this
40 /// property is not stackable.
42 /// Like Sensitive but also this property is deleted from a span
43 /// of text if a text just before the span is modified,
44 /// inserted, or deleted.
45 FrontSensitive = 0x0C,
46 /// Like Sensitive but also this property is deleted from a span
47 /// of text if a text just after the span is modified, inserted,
55 public MSymbol Key { get { return key; } }
56 public object Val { get { return val; } }
58 public MProperty (MSymbol key, object val)
60 if (key.flags == null)
61 key.flags = Flags.None;
66 public MProperty (string name, object val)
68 key = MSymbol.PropertyKey (name);
72 public static bool HasFlags (MSymbol key, Flags flags)
74 return ((key.flags & flags) == flags);
77 public override string ToString ()
79 return key.ToString () + ":" + val;
83 public class MText : IEnumerable, IEquatable<MText>, IComparable<MText>
86 public enum MTextFormat format;
88 private StringBuilder sb;
90 private int cache_pos;
91 private int cache_idx;
92 private MPlist intervals;
93 private MPlist default_property;
94 private bool read_only;
96 private static UTF8Encoding utf8 = new UTF8Encoding ();
98 private static int count_chars (String str)
100 int len = str.Length, n = 0;
102 for (int i = 0; i < len; i++, n++)
103 if (surrogate_high_p (str[i]))
108 private static int count_chars (StringBuilder str)
110 int len = str.Length, n = 0;
112 for (int i = 0; i < len; i++, n++)
113 if (surrogate_high_p (str[i]))
120 sb = new StringBuilder ();
121 intervals = new MPlist ();
124 public MText (byte[] str)
126 sb = new StringBuilder (utf8.GetString (str));
127 nchars = count_chars (sb);
128 intervals = new MPlist ();
131 public MText (String str)
133 sb = new StringBuilder (str);
134 nchars = count_chars (str);
135 intervals = new MPlist ();
138 public MText (StringBuilder str)
141 nchars = count_chars (str);
142 intervals = new MPlist ();
145 public static MText operator+ (object obj, MText mt)
149 MText mtnew = new MText ((string) obj);
150 return mtnew.Ins (mtnew.Length, mt);
152 throw new Exception ("Unknown object type: " + obj.GetType());
155 public static MText operator+ (MText mt, object obj)
158 return mt + new MText ((string) obj);
160 return mt.Dup ().Ins (mt.Length, (int) obj);
162 return mt.Dup ().Ins (mt.Length, (int) ((char) obj));
163 throw new Exception ("Unknown object type: " + obj.GetType());
166 public static MText operator+ (MText mt1, MText mt2)
168 return mt1.Dup ().Ins (mt1.Length, mt2);
172 public bool ReadOnly { get { return read_only; } }
173 public int Length { get { return nchars; } }
177 // for IEnumerable interface
178 public IEnumerator GetEnumerator() { return new MTextEnum (this); }
180 // for IEquatable interface
181 public bool Equals (MText other) { return this.sb.Equals (other.sb); }
183 // for IComparable interface
184 public int CompareTo (MText other)
186 return this.sb.ToString ().CompareTo (other.sb.ToString ());
189 public override string ToString () { return sb.ToString (); }
191 private static bool surrogate_high_p (char c)
193 return (c >= 0xD800 && c < 0xDC00);
196 private static bool surrogate_low_p (char c)
198 return (c >= 0xDC00 && c < 0xE000);
201 private static int inc_idx (StringBuilder sb, int i)
203 return (i + (surrogate_high_p (sb[i]) ? 2 : 1));
206 private static int dec_idx (StringBuilder sb, int i)
208 return (i - (surrogate_low_p (sb[i - 1]) ? 2 : 1));
211 private static int pos_to_idx (MText mt, int pos)
213 if (pos == mt.cache_pos)
219 if (pos < mt.cache_pos)
221 if (mt.cache_pos == mt.cache_idx)
223 if (pos < mt.cache_pos - pos)
230 p = mt.cache_pos; i = mt.cache_idx;
236 if (mt.nchars - mt.cache_pos == mt.sb.Length - mt.cache_idx)
237 return (mt.cache_idx + pos - mt.cache_pos);
238 if (pos - mt.cache_pos < mt.nchars - pos)
240 p = mt.cache_pos; i = mt.cache_idx;
245 p = mt.nchars; i = mt.sb.Length;
250 for (; p < pos; i = inc_idx (mt.sb, i), p++);
252 for (; p > pos; i = dec_idx (mt.sb, i), p--);
258 private void check_pos (int pos, bool tail_ok)
260 if (pos < 0 || (tail_ok ? pos > nchars : pos >= nchars))
261 throw new Exception ("Invalid MText position:" + pos);
264 private bool check_range (int from, int to, bool zero_ok)
266 if (from < 0 || (zero_ok ? from > to : from >= to)
268 throw new Exception ("Invalid MText range");
272 private void insert (int pos, MText mt2, int from, int to)
274 check_pos (pos, true);
278 Console.Write ("inserting {0} to {1} of ", from, to);
279 mt2.DumpPropNested ();
283 foreach (MPlist plist in intervals)
285 MInterval root = (MInterval) plist.Val;
286 MPlist p = mt2.intervals.Find (plist.Key);
287 MInterval i = p == null ? null : (MInterval) p.Val;
289 root.Insert (pos, i, from, to);
291 foreach (MPlist plist in mt2.intervals)
292 if (intervals.Find (plist.Key) == null)
297 root = ((MInterval) plist.Val).Copy (this, from, to);
300 root = new MInterval (plist.Key, this);
301 root.Insert (pos, (MInterval) plist.Val, from, to);
303 intervals.Push (plist.Key, root);
306 int pos_idx = pos_to_idx (this, pos);
307 int from_idx = pos_to_idx (mt2, from);
308 int to_idx = pos_to_idx (mt2, to);
310 sb.Insert (pos_idx, mt2.sb.ToString (from_idx, to_idx - from_idx));
314 private void insert (int pos, int c)
316 check_pos (pos, true);
318 int pos_idx = pos_to_idx (this, pos);
323 sb.Insert (pos_idx, ch);
327 char high = (char) (0xD800 + ((c - 0x10000) >> 10));
328 char low = (char) (0xDC00 + ((c - 0x10000) & 0x3FF));
329 sb.Insert (pos_idx, low);
330 sb.Insert (pos_idx, high);
333 foreach (MPlist plist in intervals)
334 ((MInterval) plist.Val).Insert (pos, null, 0, 1);
337 public int this[int i]
340 i = pos_to_idx (this, i);
343 if (surrogate_high_p (sb[i]))
345 sb[i] = (char) value;
349 char high = (char) (0xD800 + ((value - 0x10000) >> 10));
350 char low = (char) (0xDC00 + ((value - 0x10000) & 0x3FF));
352 if (! surrogate_high_p (sb[i]))
359 i = pos_to_idx (this, i);
360 return (surrogate_high_p (sb[i])
361 ? ((sb[i] - 0xD800) << 10) + (sb[i + 1] - 0xDC00) + 0x10000
368 MText mt = new MText (sb.ToString ());
370 foreach (MPlist p in intervals)
371 mt.intervals.Add (p.Key, ((MInterval) p.Val).Copy (mt, 0, Length));
375 public MText Dup (int from, int to)
377 if (check_range (from, to, true))
379 int from_idx = pos_to_idx (this, from);
380 int len = pos_to_idx (this, to) - from_idx;
381 MText mt = new MText (sb.ToString ().Substring (from_idx, len));
383 foreach (MPlist p in intervals)
384 mt.intervals.Add (p.Key, ((MInterval) p.Val).Copy (mt, from, to));
388 public MText Ins (int pos, int c)
394 public MText Ins (int pos, MText mt)
396 insert (pos, mt, 0, mt.nchars);
400 public MText Ins (int pos, MText mt, int from, int to)
402 insert (pos, mt, from, to);
406 public MText Cat (int c)
412 public MText Del (int from, int to)
414 if (check_range (from, to, true))
417 sb.Remove (from, pos_to_idx (this, to) - pos_to_idx (this, from));
421 foreach (MPlist plist in intervals)
423 MInterval root = (MInterval) plist.Val;
424 root.Delete (from, to);
425 if (from > 0 && from < nchars)
426 ((MInterval) plist.Val).MergeAfterChange (from, from);
435 public object GetProp (int pos, MSymbol key)
437 check_pos (pos, false);
439 MInterval i = (MInterval) intervals.Get (key);
443 MProperty prop = i.Get (pos);
444 return (prop != null ? prop.Val : null);
447 public object GetProp (int pos, MSymbol key, out MProperty prop)
449 check_pos (pos, false);
451 MInterval i = (MInterval) intervals.Get (key);
453 return (prop = null);
455 return (prop != null ? prop.Val : null);
458 public object GetProp (int pos, MSymbol key, out MProperty[] array)
460 check_pos (pos, false);
462 MInterval i = (MInterval) intervals.Get (key);
464 return (array = null);
465 MProperty prop = i.Get (pos, out array);
466 return (prop != null ? prop.Val : null);
469 public void PushProp (int from, int to, MSymbol key, object val)
471 if (! check_range (from, to, true))
472 PushProp (from, to, new MProperty (key, val));
475 public void PushProp (int from, int to, MProperty prop)
479 if (default_property == null)
480 default_property = new MPlist ();
481 default_property.Push (prop.key, prop.val);
485 if (check_range (from, to, true))
488 MPlist p = intervals.Find (prop.key);
493 root = new MInterval (prop.key, this);
494 intervals.Push (prop.key, root);
497 root = (MInterval) p.Val;
499 if (root.isSensitive)
500 root.PopSensitive (from, to);
501 root.Push (from, to, prop);
502 root.MergeAfterChange (from, to);
507 public void PopProp (int from, int to, MSymbol key)
511 if (default_property == null)
513 MPlist p = default_property.Find (key);
520 if (check_range (from, to, true))
523 MPlist p = intervals.Find (key);
527 MInterval root = (MInterval) p.Val;
528 if (root.isSensitive)
529 root.PopSensitive (from, to);
532 root = (MInterval) p.Val;
535 root.MergeAfterChange (from, to);
541 public void DumpProp ()
544 foreach (MPlist p in intervals)
545 ((MInterval) p.Val).Dump (true);
546 Console.WriteLine (")");
549 public void DumpPropNested ()
551 Console.WriteLine ("total length = {0}", Length);
552 foreach (MPlist p in intervals)
553 ((MInterval) p.Val).DumpNested (true);
556 private class MInterval
558 // position: 0 1 2 3 4 5 6 7
559 // | A | B | C | D | E F | G |
560 // interval |---|---|---|<->|-------|---|
561 // |---|<->|---| |<----->|---|
565 // [3 (1 2)] [3 (4 6)]
566 // [1 (0 1)] [2 (2 3)] [1 (6 7)]
568 private static int count = 0;
571 private int From, To;
573 private MPlist Stack;
574 private MInterval Left, Right, Parent;
577 public MInterval (MSymbol key, MText mt, int length)
580 throw new Exception ("Invalid interval length");
584 Stack = new MPlist ();
588 public MInterval (MSymbol key, MText mt)
592 Length = mt.sb.Length;
595 Stack = new MPlist ();
599 /// POS must be smaller than Length;
600 public MProperty Get (int pos)
602 MInterval i = find_head (pos);
604 return (i.Stack.IsEmpty ? null : (MProperty) i.Stack.Val);
607 /// POS must be smaller than Length;
608 public MProperty Get (int pos, out MProperty[] array)
610 MInterval i = find_head (pos);
619 array = new MProperty[i.Stack.Count];
623 for (idx = 0, p = i.Stack; ! p.IsEmpty; idx++, p = p.Next)
624 array[idx] = (MProperty) p.Val;
628 private MInterval (MSymbol key, MText mt, int length, MPlist stack)
635 Stack = stack == null ? new MPlist () : stack.Clone ();
639 private bool isRearSticky
641 get { return MProperty.HasFlags (Key, MProperty.Flags.RearSticky) ; }
644 private bool isFrontSticky
646 get { return MProperty.HasFlags (Key, MProperty.Flags.FrontSticky) ; }
649 public bool isSensitive
651 get { return MProperty.HasFlags (Key, MProperty.Flags.Sensitive) ; }
654 public bool isFrontSensitive
656 get { return MProperty.HasFlags (Key,
657 MProperty.Flags.FrontSensitive) ; }
660 public bool isRearSensitive
662 get { return MProperty.HasFlags (Key,
663 MProperty.Flags.RearSensitive) ; }
666 private void update_from_to ()
671 To = Length - RightLength;
673 else if (Parent.Left == this)
675 From = Parent.From - Length + LeftLength;
676 To = Parent.From - RightLength;
680 From = Parent.To + LeftLength;
681 To = Parent.To + Length - RightLength;
685 private int LeftLength
687 get { return (Left == null ? 0 : Left.Length); }
690 private int RightLength
692 get { return (Right == null ? 0 : Right.Length); }
695 private MInterval LeftMost
701 return Left.LeftMost;
705 private MInterval RightMost
711 return Right.RightMost;
715 private MInterval Prev {
721 for (i = Left; i.Right != null; i = i.Right)
727 MInterval child = this;
728 for (i = Parent; i != null && i.Left == child;
729 child = i, i = i.Parent);
735 private MInterval Next {
741 for (i = Right; i.Left != null; i = i.Left)
747 MInterval child = this;
748 for (i = Parent; i != null && i.Right == child;
749 child = i, i = i.Parent);
755 private MInterval find_head (int pos)
759 return Left.find_head (pos);
761 return Right.find_head (pos);
765 private MInterval find_tail (int pos)
769 return Left.find_tail (pos);
771 return Right.find_tail (pos);
775 private bool mergeable (MInterval i)
779 for (p1 = Stack, p2 = i.Stack; ! p1.IsEmpty && ! p2.IsEmpty;
780 p1 = p1.Next, p2 = p2.Next)
781 if (p1.Val != p2.Val)
783 return (p1.IsEmpty && p2.IsEmpty);
786 // p-. or .-p p-. or .-p
787 // .-this-. .-right-.
788 // left .-right-. -> .-this-. c2
790 private MInterval promote_right ()
792 MInterval c1 = Right.Left;
796 mtext.intervals.Put (Key, Right);
797 else if (Parent.Left == this)
800 Parent.Right = Right;
803 Right.Parent = Parent;
805 Right.Length += LeftLength + (To - From);
810 Length = LeftLength + (To - From) + RightLength;
812 // Update C1 if necessary.
816 Parent.update_from_to ();
820 // p-. or .-p p-. or .-p
822 // .-left-. .-right-. -> c1 .-this-.
824 private MInterval promote_left ()
826 MInterval c2 = Left.Right;
830 mtext.intervals.Put (Key, Left);
831 else if (Parent.Left == this)
837 Left.Parent = Parent;
839 Left.Length += (To - From) + RightLength;
844 Length = LeftLength + (To - From) + RightLength;
846 // Update C2 if necessary.
850 Parent.update_from_to ();
854 public MInterval Balance ()
862 // .-left-. .-right-.
864 int diff = i.LeftLength - i.RightLength;
869 new_diff = (i.Length - i.LeftLength
870 + i.Left.RightLength - i.Left.LeftLength);
871 if (Math.Abs (new_diff) >= diff)
873 M17n.DebugPrint ("balancing #{0} by promoting left...", i.ID);
874 i = i.promote_left ();
875 M17n.DebugPrint ("done\n");
880 new_diff = (i.Length - i.RightLength
881 + i.Right.LeftLength - i.Right.RightLength);
882 if (Math.Abs (new_diff) >= diff)
884 M17n.DebugPrint ("balancing #{0} by promoting right\n", i.ID);
885 i = i.promote_right ();
894 public MInterval Copy (MText mt, int start, int end)
896 MInterval copy, left_copy = null, right_copy = null;
903 return Left.Copy (mt, start, end);
904 left_copy = Left.Copy (mt, start, From);
909 return Right.Copy (mt, start, end);
910 right_copy = Right.Copy (mt, To, end);
913 copy = new MInterval (Key, null, end - start, Stack);
915 if (isSensitive && (From < start || end < To))
917 if (left_copy != null)
919 copy.Left = left_copy;
920 left_copy.Parent = copy;
922 if (right_copy != null)
924 copy.Right = right_copy;
925 right_copy.Parent = copy;
933 private MInterval divide_right (int pos)
935 MInterval interval = new MInterval (Key, mtext, To - pos, Stack);
937 M17n.DebugPrint ("divide-right({0}) at ", pos); DumpOne (false, true);
941 interval.Right = Right;
942 Right.Parent = interval;
943 interval.Length += Right.Length;
945 interval.Parent = this;
953 private MInterval divide_left (int pos)
955 MInterval interval = new MInterval (Key, mtext, pos - From, Stack);
957 M17n.DebugPrint ("divide-left({0}) at ", pos); DumpOne (false, true);
961 interval.Left = Left;
962 Left.Parent = interval;
963 interval.Length += Left.Length;
965 interval.Parent = this;
970 private void set_mtext (MText mt)
976 Right.set_mtext (mt);
979 private void enlarge (int len)
983 for (MInterval prev = this, i = this.Parent; i != null;
984 prev = i, i = i.Parent)
995 private int graft_forward (MInterval interval, int start, int end)
999 if (! Stack.IsEmpty && isRearSticky)
1001 else if (interval == null)
1002 len = Stack.IsEmpty ? end - start : 0;
1005 MInterval i = interval.find_head (start);
1008 while (i != null && mergeable (i))
1010 M17n.DebugPrint (" grafting "); i.DumpOne (false, false);
1011 len += i.To - i.From;
1013 len -= start - i.From;
1023 M17n.DebugPrint (" grafted {0} in ", len); DumpOne (false, true);
1029 private int graft_backward (MInterval interval, int start, int end)
1033 if (! Stack.IsEmpty && isFrontSticky)
1035 else if (interval == null)
1036 len = Stack.IsEmpty ? end - start : 0;
1039 MInterval i = interval.find_tail (end);
1042 while (i != null && mergeable (i))
1044 M17n.DebugPrint (" grafting "); i.DumpOne (false, false);
1045 len += i.To - i.From;
1048 if (i.From <= start)
1050 len -= start - i.From;
1057 M17n.DebugPrint (" grafted {0} in ", len); DumpOne (false, true);
1063 public void Insert (int pos, MInterval interval, int start, int end)
1067 M17n.DebugPrint ("insert({0} to {1}) at {2} in ", start, end, pos);
1068 DumpOne (false, false);
1071 Left.Insert (pos, interval, start, end);
1072 else if (pos == From)
1074 MInterval prev = Left != null ? Prev : null;
1076 if (isFrontSensitive)
1078 if (prev != null && isRearSensitive)
1079 prev.Stack.Clear ();
1080 if (prev != null && isRearSticky && ! prev.Stack.IsEmpty)
1082 prev.enlarge (end - start);
1085 if (isFrontSticky && ! Stack.IsEmpty)
1087 enlarge (end - start);
1092 start += prev.graft_forward (interval, start, end);
1096 if ((end -= graft_backward (interval, start, end)) == start)
1099 if (interval != null)
1100 interval = interval.Copy (mtext, start, end);
1102 interval = new MInterval (Key, mtext, end - start, null);
1107 // .-this-. ==> .-this-.
1119 interval.Parent = i;
1120 for (; i != null; i = i.Parent)
1121 i.Length += interval.Length;
1127 else if (! Stack.IsEmpty && (isFrontSticky || isRearSticky))
1129 enlarge (end - start);
1132 int len = graft_forward (interval, start, end);
1136 if ((end -= graft_backward (interval, start, end)) == start)
1139 if (interval != null)
1140 interval = interval.Copy (mtext, start, end);
1142 interval = new MInterval (Key, mtext, end - start, null);
1145 Right.Left = interval;
1146 interval.Parent = Right;
1147 for (MInterval i = Right; i != null; i = i.Parent)
1148 i.Length += interval.Length;
1152 MInterval next = Right != null ? Next : null;
1154 if (isRearSensitive)
1156 if (next != null && isFrontSensitive)
1157 next.Stack.Clear ();
1158 if (isRearSticky && ! Stack.IsEmpty)
1160 enlarge (end - start);
1165 if (isFrontSticky && ! next.Stack.IsEmpty)
1167 next.enlarge (end - start);
1170 end -= next.graft_backward (interval, start, end);
1175 if ((start += graft_forward (interval, start, end)) == end)
1178 if (interval != null)
1179 interval = interval.Copy (mtext, start, end);
1181 interval = new MInterval (Key, mtext, end - start, null);
1186 // .-this-. ==> .-this-.
1199 interval.Parent = i;
1200 for (; i != null; i = i.Parent)
1201 i.Length += interval.Length;
1204 Right.Insert (pos, interval, start, end);
1205 M17n.DebugPrint (" done\n");
1208 private void vacate_node (MInterval interval)
1210 vacate_node (interval, null);
1213 private void vacate_node (MInterval interval, MInterval stop)
1215 if (interval != null)
1216 M17n.DebugPrint ("vacate #{0} to #{1}\n", ID, interval.ID);
1218 M17n.DebugPrint ("vacate #{0} to null\n", ID);
1219 if (interval != null)
1220 interval.Parent = Parent;
1224 mtext.intervals.Put (Key, interval);
1228 if (this == Parent.Right)
1229 Parent.Right = interval;
1231 Parent.Left = interval;
1234 if (interval != null)
1235 diff -= interval.Length;
1236 for (MInterval i = Parent; i != stop; i = i.Parent)
1241 public void Delete (int start, int end)
1244 M17n.DebugPrint ("delete({0} {1}) from ", start, end); DumpOne (false, true);
1249 Left.Delete (start, end);
1252 Left.Delete (start, From);
1254 end -= From - start;
1261 Right.Delete (start, end);
1264 Right.Delete (To, end);
1267 if (start == From && end == To)
1279 for (i = Right; i.Left != null; i = i.Left)
1280 i.Length += Left.Length;
1281 i.Length += Left.Length;
1285 vacate_node (Right);
1290 int len = end - start;
1292 for (MInterval i = this; i != null; i = i.Parent)
1297 public void Push (int start, int end, MProperty prop)
1300 M17n.DebugPrint ("push({0} {1}) at ", start, end); DumpOne (false, true);
1305 Left.Push (start, end, prop);
1308 Left.Push (start, From, prop);
1315 Right.Push (start, end, prop);
1318 Right.Push (To, end, prop);
1323 divide_left (start);
1326 Stack.Push (prop.key, prop);
1329 /// Combine intervals between HEAD and TAIL (both inclusive) to
1330 /// the common parent of HEAD and TAIL while assuming that the
1331 /// intervals are mergeable.
1332 private static void combine (MInterval head, MInterval tail)
1334 M17n.DebugPrint ("combining "); head.DumpOne (true, false);
1335 M17n.DebugPrint (" through "); tail.DumpOne (true, false);
1337 int from = head.From;
1339 // The nearest common parent of HEAD and TAIL.
1341 for (root = head; root.To + root.RightLength < to;
1342 root = root.Parent);
1344 M17n.DebugPrint (" common root is "); root.DumpOne (false, true);
1346 if (from < root.From)
1348 MInterval prev = root.Prev;
1352 M17n.DebugPrint ("merging "); prev.DumpOne (false, true);
1353 prev.vacate_node (prev.Left, root);
1356 if (prev.Left != null)
1357 prev = prev.Left.RightMost;
1361 root.update_from_to ();
1365 MInterval next = root.Next;
1369 M17n.DebugPrint ("merging "); next.DumpOne (false, true);
1370 next.vacate_node (next.Right, root);
1373 if (next.Right != null)
1374 next = next.Right.LeftMost;
1378 root.update_from_to ();
1382 public void MergeAfterChange (int start, int end)
1386 MInterval head = find_head (start), i = head;
1387 MInterval tail = find_tail (end).Next;
1389 if (start == head.From && start > 0)
1392 if (! head.mergeable (i))
1397 MInterval next = i.Next;
1399 if (next == null || ! i.mergeable (next))
1409 public void Pop (int start, int end)
1412 M17n.DebugPrint ("pop({0} {1}) at ", start, end); DumpOne (false, true);
1417 Left.Pop (start, end);
1420 Left.Pop (start, From);
1427 Right.Pop (start, end);
1430 Right.Pop (To, end);
1434 if (! Stack.IsEmpty)
1437 divide_left (start);
1444 public void PopSensitive (int start, int end)
1447 MInterval head = find_head (start);
1448 MInterval tail = find_tail (end);
1449 while (! head.Stack.IsEmpty && head.From > 0)
1451 MInterval prev = head.Prev;
1453 if (prev.Stack.IsEmpty || head.Stack.Val != prev.Stack.Val)
1457 while (! tail.Stack.IsEmpty && tail.To < mtext.Length)
1459 MInterval next = tail.Next;
1461 if (next.Stack.IsEmpty || tail.Stack.Val != next.Stack.Val)
1465 Pop (head.From, tail.To);
1468 private void DumpOne (bool with_prop, bool newline)
1470 DumpOne (with_prop, newline, false);
1473 private void DumpOne (bool with_prop, bool newline, bool force)
1475 if (force || M17n.debug)
1477 Console.Write ("#{0}({1} {2} {3}", ID, Length, From, To);
1478 if (with_prop && ! Stack.IsEmpty)
1480 string prepend = " [";
1481 foreach (MPlist p in Stack)
1483 Console.Write (prepend + ((MProperty) p.Val).Val);
1486 Console.Write ("]");
1488 Console.Write (")");
1490 Console.WriteLine ();
1492 throw new Exception ("Invalid interval length");
1496 public void Dump () { Dump (false); }
1498 public void Dump (bool force)
1500 if (force || M17n.debug)
1507 Console.Write (" ");
1508 DumpOne (true, false, force);
1515 get { return (Parent == null ? 0 : Parent.Depth + 1); }
1518 public void DumpNested (bool force)
1520 DumpNested (Key.ToString () + ":", force);
1523 public void DumpNested (string indent, bool force)
1525 if (force || M17n.debug)
1527 int indent_type = (Parent == null ? 1
1528 : Parent.Left == this ? 0 : 2);
1533 if (indent_type <= 1)
1534 Left.DumpNested (indent + " ", force);
1536 Left.DumpNested (indent + "| ", force);
1538 Console.Write (indent);
1539 if (indent_type == 0)
1540 Console.Write (".-");
1541 else if (indent_type == 2)
1542 Console.Write ("`-");
1543 DumpOne (true, true, true);
1546 if (indent_type >= 1)
1547 Right.DumpNested (indent + " ", force);
1549 Right.DumpNested (indent + "| ", force);
1555 private class MTextEnum : IEnumerator
1558 private int pos = -1;
1560 public MTextEnum (MText mt)
1565 public bool MoveNext ()
1568 return (pos < mt.nchars);
1571 public void Reset ()
1576 public object Current
1579 //if (pos < 0 || pos >= mt.nchars)
1580 //throw new InvalidOperationException ();