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. See the documentation of
31 /// Sensitive for exception.
33 /// A text inserted after a character that has this property
34 /// inherits this property. If the text already has properties
35 /// of the same key, they are deleted. See the documentation of
36 /// Sensitive for exception.
38 /// This property is deleted from a span of text if the span is
39 /// modified (i.e. a character is changed, a text is inserted,
40 /// some part is deleted). This propery is also deleted if a
41 /// property of the same key is added, which means that this
42 /// property is not stackable. If this property is also
43 /// FrontSticky (or RearSticky), text insertion just before (or
44 /// after) the span also deletes this property from the span of
52 public MSymbol Key { get { return key; } }
53 public object Val { get { return val; } }
55 public MProperty (MSymbol key, object val)
57 if (key.flags == null)
58 key.flags = MProperty.Flags.None;
63 public MProperty (string name, object val)
65 key = MSymbol.PropertyKey (name);
69 public override string ToString ()
71 return key.ToString () + ":" + val;
75 public class MText : IEnumerable, IEquatable<MText>, IComparable<MText>
78 public enum MTextFormat format;
80 private StringBuilder sb;
82 private int cache_pos;
83 private int cache_idx;
84 private MPlist intervals;
85 private MPlist default_property;
86 private bool read_only;
88 private static UTF8Encoding utf8 = new UTF8Encoding ();
90 private static int count_chars (String str)
92 int len = str.Length, n = 0;
94 for (int i = 0; i < len; i++, n++)
95 if (surrogate_high_p (str[i]))
100 private static int count_chars (StringBuilder str)
102 int len = str.Length, n = 0;
104 for (int i = 0; i < len; i++, n++)
105 if (surrogate_high_p (str[i]))
112 sb = new StringBuilder ();
113 intervals = new MPlist ();
116 public MText (byte[] str)
118 sb = new StringBuilder (utf8.GetString (str));
119 nchars = count_chars (sb);
120 intervals = new MPlist ();
123 public MText (String str)
125 sb = new StringBuilder (str);
126 nchars = count_chars (str);
127 intervals = new MPlist ();
130 public MText (StringBuilder str)
133 nchars = count_chars (str);
134 intervals = new MPlist ();
137 public static MText operator+ (object obj, MText mt)
140 return new MText ((string) obj) + mt;
141 throw new Exception ("Unknown object type: " + obj.GetType());
144 public static MText operator+ (MText mt1, MText mt2)
146 MText mt = new MText ();
148 mt.sb.Append (mt1.sb);
149 mt.sb.Append (mt2.sb);
150 mt.nchars = mt1.nchars + mt2.nchars;
154 public static MText operator+ (string str, MText mt)
156 MText mtnew = new MText (str);
158 mtnew.sb.Append (mt.sb);
159 mtnew.nchars += mt.nchars;
164 public bool ReadOnly { get { return read_only; } }
165 public int Length { get { return nchars; } }
169 // for IEnumerable interface
170 public IEnumerator GetEnumerator() { return new MTextEnum (this); }
172 // for IEquatable interface
173 public bool Equals (MText other) { return this.sb.Equals (other.sb); }
175 // for IComparable interface
176 public int CompareTo (MText other)
178 return this.sb.ToString ().CompareTo (other.sb.ToString ());
181 public override String ToString () { return "\"" + sb.ToString () + "\""; }
183 private static bool surrogate_high_p (char c)
185 return (c >= 0xD800 && c < 0xDC00);
188 private static bool surrogate_low_p (char c)
190 return (c >= 0xDC00 && c < 0xE000);
193 private static int inc_idx (StringBuilder sb, int i)
195 return (i + (surrogate_high_p (sb[i]) ? 2 : 1));
198 private static int dec_idx (StringBuilder sb, int i)
200 return (i - (surrogate_low_p (sb[i - 1]) ? 2 : 1));
203 private static int pos_to_idx (MText mt, int pos)
205 if (pos == mt.cache_pos)
211 if (pos < mt.cache_pos)
213 if (mt.cache_pos == mt.cache_idx)
215 if (pos < mt.cache_pos - pos)
222 p = mt.cache_pos; i = mt.cache_idx;
228 if (mt.nchars - mt.cache_pos == mt.sb.Length - mt.cache_idx)
229 return (mt.cache_idx + pos - mt.cache_pos);
230 if (pos - mt.cache_pos < mt.nchars - pos)
232 p = mt.cache_pos; i = mt.cache_idx;
237 p = mt.nchars; i = mt.sb.Length;
242 for (; p < pos; i = inc_idx (mt.sb, i), p++);
244 for (; p > pos; i = dec_idx (mt.sb, i), p--);
250 private void check_pos (int pos, bool tail_ok)
252 if (pos < 0 || (tail_ok ? pos > nchars : pos >= nchars))
253 throw new Exception ("Invalid MText position:" + pos);
256 private bool check_range (int from, int to, bool zero_ok)
258 if (from < 0 || (zero_ok ? from > to : from >= to)
260 throw new Exception ("Invalid MText range");
264 private void insert (int pos, MText mt2, int from, int to)
266 check_pos (pos, true);
270 Console.Write ("inserting {0} to {1} of ", from, to);
271 mt2.DumpPropNested ();
275 foreach (MPlist plist in intervals)
277 MInterval root = (MInterval) plist.Val;
278 MPlist p = mt2.intervals.Find (plist.Key);
279 MInterval i = p == null ? null : (MInterval) p.Val;
281 root.Insert (pos, i, from, to);
283 foreach (MPlist plist in mt2.intervals)
284 if (intervals.Find (plist.Key) == null)
289 root = ((MInterval) plist.Val).Copy (this, from, to);
292 root = new MInterval (plist.Key, this);
293 root.Insert (pos, (MInterval) plist.Val, from, to);
295 intervals.Push (plist.Key, root);
298 int pos_idx = pos_to_idx (this, pos);
299 int from_idx = pos_to_idx (mt2, from);
300 int to_idx = pos_to_idx (mt2, to);
302 sb.Insert (pos_idx, mt2.sb.ToString (from_idx, to_idx - from_idx));
306 private void insert (int pos, int c)
308 check_pos (pos, true);
310 int pos_idx = pos_to_idx (this, pos);
315 sb.Insert (pos_idx, ch);
319 char high = (char) (0xD800 + ((c - 0x10000) >> 10));
320 char low = (char) (0xDC00 + ((c - 0x10000) & 0x3FF));
321 sb.Insert (pos_idx, low);
322 sb.Insert (pos_idx, high);
325 foreach (MPlist plist in intervals)
326 ((MInterval) plist.Val).Insert (pos, null, 0, 1);
329 public int this[int i]
332 i = pos_to_idx (this, i);
335 if (surrogate_high_p (sb[i]))
337 sb[i] = (char) value;
341 char high = (char) (0xD800 + ((value - 0x10000) >> 10));
342 char low = (char) (0xDC00 + ((value - 0x10000) & 0x3FF));
344 if (! surrogate_high_p (sb[i]))
351 i = pos_to_idx (this, i);
352 return (surrogate_high_p (sb[i])
353 ? ((sb[i] - 0xD800) << 10) + (sb[i + 1] - 0xDC00) + 0x10000
360 MText mt = new MText (sb.ToString ());
362 foreach (MPlist p in intervals)
363 mt.intervals.Add (p.Key, ((MInterval) p.Val).Copy (mt, 0, Length));
367 public MText Dup (int from, int to)
369 if (check_range (from, to, true))
371 int from_idx = pos_to_idx (this, from);
372 int len = pos_to_idx (this, to) - from_idx;
373 MText mt = new MText (sb.ToString ().Substring (from_idx, len));
375 foreach (MPlist p in intervals)
376 mt.intervals.Add (p.Key, ((MInterval) p.Val).Copy (mt, from, to));
380 public MText Ins (int pos, int c)
386 public MText Ins (int pos, MText mt)
388 insert (pos, mt, 0, mt.nchars);
392 public MText Ins (int pos, MText mt, int from, int to)
394 insert (pos, mt, from, to);
398 public MText Cat (int c)
404 public MText Del (int from, int to)
406 if (check_range (from, to, true))
409 sb.Remove (from, pos_to_idx (this, to) - pos_to_idx (this, from));
413 foreach (MPlist plist in intervals)
415 MInterval root = (MInterval) plist.Val;
416 root.Delete (from, to);
417 if (from > 0 && from < nchars)
418 root.MergeAfterChange (from, from);
427 public object GetProp (int pos, MSymbol key)
429 check_pos (pos, false);
431 MInterval i = (MInterval) intervals.Get (key);
435 MProperty prop = i.Get (pos);
436 return (prop != null ? prop.Val : null);
439 public object GetProp (int pos, MSymbol key, out MProperty prop)
441 check_pos (pos, false);
443 MInterval i = (MInterval) intervals.Get (key);
445 return (prop = null);
447 return (prop != null ? prop.Val : null);
450 public object GetProp (int pos, MSymbol key, out MProperty[] array)
452 check_pos (pos, false);
454 MInterval i = (MInterval) intervals.Get (key);
456 return (array = null);
457 MProperty prop = i.Get (pos, out array);
458 return (prop != null ? prop.Val : null);
461 public void PushProp (int from, int to, MSymbol key, object val)
463 if (! check_range (from, to, true))
464 PushProp (from, to, new MProperty (key, val));
467 public void PushProp (int from, int to, MProperty prop)
471 if (default_property == null)
472 default_property = new MPlist ();
473 default_property.Push (prop.key, prop.val);
477 if (check_range (from, to, true))
480 MPlist p = intervals.Find (prop.key);
485 root = new MInterval (prop.key, this);
486 intervals.Push (prop.key, root);
489 root = (MInterval) p.Val;
491 if (root.isSensitive)
492 root.PopSensitive (from, to);
493 root.Push (from, to, prop);
494 root.MergeAfterChange (from, to);
499 public void PopProp (int from, int to, MSymbol key)
503 if (default_property == null)
505 MPlist p = default_property.Find (key);
512 if (check_range (from, to, true))
515 MPlist p = intervals.Find (key);
519 MInterval root = (MInterval) p.Val;
520 if (root.isSensitive)
521 root.PopSensitive (from, to);
524 root.MergeAfterChange (from, to);
530 public void DumpProp ()
533 foreach (MPlist p in intervals)
534 ((MInterval) p.Val).Dump (true);
535 Console.WriteLine (")");
538 public void DumpPropNested ()
540 Console.WriteLine ("total length = {0}", Length);
541 foreach (MPlist p in intervals)
542 ((MInterval) p.Val).DumpNested (true);
545 private class MInterval
547 // position: 0 1 2 3 4 5 6 7
548 // | A | B | C | D | E F | G |
549 // interval |---|---|---|<->|-------|---|
550 // |---|<->|---| |<----->|---|
554 // [3 (1 2)] [3 (4 6)]
555 // [1 (0 1)] [2 (2 3)] [1 (6 7)]
557 private static int count = 0;
560 private int From, To;
562 private MPlist Stack;
563 private MInterval Left, Right, Parent;
566 public MInterval (MSymbol key, MText mt, int length)
569 throw new Exception ("Invalid interval length");
573 Stack = new MPlist ();
577 public MInterval (MSymbol key, MText mt)
581 Length = mt.sb.Length;
584 Stack = new MPlist ();
588 /// POS must be smaller than Length;
589 public MProperty Get (int pos)
591 MInterval i = find_head (pos);
593 return (i.Stack.IsEmpty ? null : (MProperty) i.Stack.Val);
596 /// POS must be smaller than Length;
597 public MProperty Get (int pos, out MProperty[] array)
599 MInterval i = find_head (pos);
608 array = new MProperty[i.Stack.Count];
612 for (idx = 0, p = i.Stack; ! p.IsEmpty; idx++, p = p.Next)
613 array[idx] = (MProperty) p.Val;
617 private MInterval (MSymbol key, MText mt, int length, MPlist stack)
624 Stack = stack == null ? new MPlist () : stack.Clone ();
628 private bool isRearSticky
630 get { return ((Key.flags & MProperty.Flags.RearSticky)
631 != MProperty.Flags.None); }
634 private bool isFrontSticky
636 get { return ((Key.flags & MProperty.Flags.FrontSticky)
637 != MProperty.Flags.None); }
640 public bool isSensitive
642 get { return ((Key.flags & MProperty.Flags.Sensitive)
643 != MProperty.Flags.None); }
646 private void update_from_to ()
651 To = Length - RightLength;
653 else if (Parent.Left == this)
655 From = Parent.From - Length + LeftLength;
656 To = Parent.From - RightLength;
660 From = Parent.To + LeftLength;
661 To = Parent.To + Length - RightLength;
665 private int LeftLength
667 get { return (Left == null ? 0 : Left.Length); }
670 private int RightLength
672 get { return (Right == null ? 0 : Right.Length); }
675 private MInterval LeftMost
681 return Left.LeftMost;
685 private MInterval RightMost
691 return Right.RightMost;
695 private MInterval Prev {
701 for (i = Left; i.Right != null; i = i.Right)
707 MInterval child = this;
708 for (i = Parent; i != null && i.Left == child;
709 child = i, i = i.Parent);
715 private MInterval Next {
721 for (i = Right; i.Left != null; i = i.Left)
727 MInterval child = this;
728 for (i = Parent; i != null && i.Right == child;
729 child = i, i = i.Parent);
735 private MInterval find_head (int pos)
739 return Left.find_head (pos);
741 return Right.find_head (pos);
745 private MInterval find_tail (int pos)
749 return Left.find_tail (pos);
751 return Right.find_tail (pos);
755 private bool mergeable (MInterval i)
759 for (p1 = Stack, p2 = i.Stack; ! p1.IsEmpty && ! p2.IsEmpty;
760 p1 = p1.Next, p2 = p2.Next)
761 if (p1.Val != p2.Val)
763 return (p1.IsEmpty && p2.IsEmpty);
766 // p-. or .-p p-. or .-p
767 // .-this-. .-right-.
768 // left .-right-. -> .-this-. c2
770 private MInterval promote_right ()
772 MInterval c1 = Right.Left;
776 mtext.intervals.Put (Key, Right);
777 else if (Parent.Left == this)
780 Parent.Right = Right;
783 Right.Parent = Parent;
785 Right.Length += LeftLength + (To - From);
790 Length = LeftLength + (To - From) + RightLength;
792 // Update C1 if necessary.
799 // p-. or .-p p-. or .-p
801 // .-left-. .-right-. -> c1 .-this-.
803 private MInterval promote_left ()
805 MInterval c2 = Left.Right;
809 mtext.intervals.Put (Key, Left);
810 else if (Parent.Left == this)
816 Left.Parent = Parent;
818 Left.Length += (To - From) + RightLength;
823 Length = LeftLength + (To - From) + RightLength;
825 // Update C2 if necessary.
832 public MInterval Balance ()
839 // .-left-. .-right-.
841 int diff = i.LeftLength - i.RightLength;
846 new_diff = (i.Length - i.LeftLength
847 + i.Left.RightLength - i.Left.LeftLength);
848 if (Math.Abs (new_diff) >= diff)
850 M17n.DebugPrint ("balancing #{0} by promoting left...", i.ID);
851 i = i.promote_left ();
852 M17n.DebugPrint ("done\n");
857 new_diff = (i.Length - i.RightLength
858 + i.Right.LeftLength - i.Right.RightLength);
859 if (Math.Abs (new_diff) >= diff)
861 M17n.DebugPrint ("balancing #{0} by promoting right\n", i.ID);
862 i = i.promote_right ();
871 public MInterval Copy (MText mt, int start, int end)
873 MInterval copy, left_copy = null, right_copy = null;
880 return Left.Copy (mt, start, end);
881 left_copy = Left.Copy (mt, start, From);
886 return Right.Copy (mt, start, end);
887 right_copy = Right.Copy (mt, To, end);
890 copy = new MInterval (Key, null, end - start, Stack);
892 if (isSensitive && (From < start || end < To))
894 if (left_copy != null)
896 copy.Left = left_copy;
897 left_copy.Parent = copy;
899 if (right_copy != null)
901 copy.Right = right_copy;
902 right_copy.Parent = copy;
910 private MInterval divide_right (int pos)
912 MInterval interval = new MInterval (Key, mtext, To - pos, Stack);
914 M17n.DebugPrint ("divide-right({0}) at ", pos); DumpOne (false, true);
918 interval.Right = Right;
919 Right.Parent = interval;
920 interval.Length += Right.Length;
922 interval.Parent = this;
930 private MInterval divide_left (int pos)
932 MInterval interval = new MInterval (Key, mtext, pos - From, Stack);
934 M17n.DebugPrint ("divide-left({0}) at ", pos); DumpOne (false, true);
938 interval.Left = Left;
939 Left.Parent = interval;
940 interval.Length += Left.Length;
942 interval.Parent = this;
947 private void set_mtext (MText mt)
953 Right.set_mtext (mt);
956 private void enlarge (int len)
960 for (MInterval prev = this, i = this.Parent; i != null;
961 prev = i, i = i.Parent)
972 private int graft_forward (MInterval interval, int start, int end)
976 if (! Stack.IsEmpty && isRearSticky)
978 else if (interval == null)
979 len = Stack.IsEmpty ? end - start : 0;
982 MInterval i = interval.find_head (start);
985 while (i != null && mergeable (i))
987 M17n.DebugPrint (" grafting "); i.DumpOne (false, false);
988 len += i.To - i.From;
990 len -= start - i.From;
1000 M17n.DebugPrint (" grafted {0} in ", len); DumpOne (false, true);
1006 private int graft_backward (MInterval interval, int start, int end)
1010 if (! Stack.IsEmpty && isFrontSticky)
1012 else if (interval == null)
1013 len = Stack.IsEmpty ? end - start : 0;
1016 MInterval i = interval.find_tail (end);
1019 while (i != null && mergeable (i))
1021 M17n.DebugPrint (" grafting "); i.DumpOne (false, false);
1022 len += i.To - i.From;
1025 if (i.From <= start)
1027 len -= start - i.From;
1034 M17n.DebugPrint (" grafted {0} in ", len); DumpOne (false, true);
1040 public void Insert (int pos, MInterval interval, int start, int end)
1044 M17n.DebugPrint ("insert({0} to {1}) at {2} in ", start, end, pos);
1045 DumpOne (false, false);
1048 Left.Insert (pos, interval, start, end);
1049 else if (pos == From)
1053 MInterval prev = Prev;
1055 if (prev.isSensitive && prev.isRearSticky)
1056 prev.Stack.Clear ();
1057 start += prev.graft_forward (interval, start, end);
1059 if (isSensitive && isFrontSticky)
1063 end -= graft_backward (interval, start, end);
1066 if (interval != null)
1067 interval = interval.Copy (mtext, start, end);
1069 interval = new MInterval (Key, mtext, end - start, null);
1074 // .-this-. ==> .-this-.
1086 interval.Parent = i;
1087 for (; i != null; i = i.Parent)
1088 i.Length += interval.Length;
1097 int len = graft_forward (interval, start, end);
1102 end -= graft_backward (interval, start, end);
1105 if (interval != null)
1106 interval = interval.Copy (mtext, start, end);
1108 interval = new MInterval (Key, mtext, end - start, null);
1111 Right.Left = interval;
1112 interval.Parent = Right;
1113 for (MInterval i = Right; i != null; i = i.Parent)
1114 i.Length += interval.Length;
1122 MInterval next = Next;
1124 if (next.isSensitive && next.isFrontSticky)
1125 next.Stack.Clear ();
1126 end -= next.graft_backward (interval, start, end);
1128 if (isSensitive && isRearSticky)
1132 start += graft_forward (interval, start, end);
1135 if (interval != null)
1136 interval = interval.Copy (mtext, start, end);
1138 interval = new MInterval (Key, mtext, end - start, null);
1143 // .-this-. ==> .-this-.
1156 interval.Parent = i;
1157 for (; i != null; i = i.Parent)
1158 i.Length += interval.Length;
1163 Next.Insert (pos, interval, start, end);
1164 M17n.DebugPrint (" done\n");
1167 private void vacate_node (MInterval interval)
1169 vacate_node (interval, null);
1172 private void vacate_node (MInterval interval, MInterval stop)
1174 if (interval != null)
1175 M17n.DebugPrint ("vacate #{0} to #{1}\n", ID, interval.ID);
1177 M17n.DebugPrint ("vacate #{0} to null\n", ID);
1178 if (interval != null)
1179 interval.Parent = Parent;
1183 mtext.intervals.Put (Key, interval);
1187 if (this == Parent.Right)
1188 Parent.Right = interval;
1190 Parent.Left = interval;
1193 if (interval != null)
1194 diff -= interval.Length;
1195 for (MInterval i = Parent; i != stop; i = i.Parent)
1200 public void Delete (int start, int end)
1203 M17n.DebugPrint ("delete({0} {1}) from ", start, end); DumpOne (false, true);
1208 Left.Delete (start, end);
1211 Left.Delete (start, From);
1213 end -= From - start;
1220 Right.Delete (start, end);
1223 Right.Delete (To, end);
1226 if (start == From && end == To)
1238 for (i = Right; i.Left != null; i = i.Left)
1239 i.Length += Left.Length;
1240 i.Length += Left.Length;
1244 vacate_node (Right);
1249 int len = end - start;
1251 for (MInterval i = this; i != null; i = i.Parent)
1256 public void Push (int start, int end, MProperty prop)
1259 M17n.DebugPrint ("push({0} {1}) at ", start, end); DumpOne (false, true);
1264 Left.Push (start, end, prop);
1267 Left.Push (start, From, prop);
1274 Right.Push (start, end, prop);
1277 Right.Push (To, end, prop);
1282 divide_left (start);
1285 Stack.Push (prop.key, prop);
1288 /// Combine intervals between HEAD and TAIL (both inclusive) to
1289 /// the common parent of HEAD and TAIL while assuming that the
1290 /// intervals are mergeable.
1291 private static void combine (MInterval head, MInterval tail)
1293 M17n.DebugPrint ("merging "); head.DumpOne (true, false);
1294 M17n.DebugPrint (" through "); tail.DumpOne (true, false);
1296 int from = head.From;
1298 // The nearest common parent of HEAD and TAIL.
1301 for (root = head; root.To + root.RightLength < to;
1302 root = root.Parent);
1304 M17n.DebugPrint (" common root is "); root.DumpOne (false, true);
1306 if (from < root.From)
1308 MInterval prev = root.Prev;
1312 M17n.DebugPrint ("merging "); prev.DumpOne (false, true);
1313 prev.vacate_node (prev.Left, root);
1316 if (prev.Left != null)
1317 prev = prev.Left.RightMost;
1324 MInterval next = root.Next;
1328 M17n.DebugPrint ("merging "); next.DumpOne (false, true);
1329 next.vacate_node (next.Right, root);
1332 if (next.Right != null)
1333 next = next.Right.LeftMost;
1340 public void MergeAfterChange (int start, int end)
1344 MInterval head = find_head (start), tail = head, i;
1346 if (start == head.From && start > 0)
1349 if (head.mergeable (i))
1352 while (tail.To < end)
1355 if (! tail.mergeable (i))
1358 combine (head, tail);
1366 if (i == null || ! tail.mergeable (i))
1371 combine (head, tail);
1374 public void Pop (int start, int end)
1377 M17n.DebugPrint ("pop({0} {1}) at ", start, end); DumpOne (false, true);
1382 Left.Pop (start, end);
1385 Left.Pop (start, From);
1392 Right.Pop (start, end);
1395 Right.Pop (To, end);
1399 if (! Stack.IsEmpty)
1402 divide_left (start);
1409 public void PopSensitive (int start, int end)
1412 MInterval head = find_head (start);
1413 MInterval tail = find_tail (end);
1414 while (! head.Stack.IsEmpty && head.From > 0)
1416 MInterval prev = head.Prev;
1418 if (prev.Stack.IsEmpty || head.Stack.Val != prev.Stack.Val)
1422 while (! tail.Stack.IsEmpty && tail.To < mtext.Length)
1424 MInterval next = tail.Next;
1426 if (next.Stack.IsEmpty || tail.Stack.Val != next.Stack.Val)
1430 Pop (head.From, tail.To);
1433 private void DumpOne (bool with_prop, bool newline)
1435 DumpOne (with_prop, newline, false);
1438 private void DumpOne (bool with_prop, bool newline, bool force)
1440 if (force || M17n.debug)
1442 Console.Write ("#{0}({1} {2} {3}", ID, Length, From, To);
1444 foreach (MPlist p in Stack)
1445 Console.Write (" " + p.Val);
1446 Console.Write (")");
1448 Console.WriteLine ();
1450 throw new Exception ("Invalid interval length");
1454 public void Dump () { Dump (false); }
1456 public void Dump (bool force)
1458 if (force || M17n.debug)
1465 Console.Write (" ");
1466 DumpOne (true, false, force);
1473 get { return (Parent == null ? 0 : Parent.Depth + 1); }
1476 public void DumpNested (bool force)
1478 DumpNested (Key.ToString () + ":", force);
1481 public void DumpNested (string indent, bool force)
1483 if (force || M17n.debug)
1485 int indent_type = (Parent == null ? 1
1486 : Parent.Left == this ? 0 : 2);
1491 if (indent_type <= 1)
1492 Left.DumpNested (indent + " ", force);
1494 Left.DumpNested (indent + "| ", force);
1496 Console.Write (indent);
1497 if (indent_type == 0)
1498 Console.Write (".-");
1499 else if (indent_type == 2)
1500 Console.Write ("`-");
1501 DumpOne (true, true, true);
1504 if (indent_type >= 1)
1505 Right.DumpNested (indent + " ", force);
1507 Right.DumpNested (indent + "| ", force);
1513 private class MTextEnum : IEnumerator
1516 private int pos = -1;
1518 public MTextEnum (MText mt)
1523 public bool MoveNext ()
1526 return (pos < mt.nchars);
1529 public void Reset ()
1534 public object Current
1537 //if (pos < 0 || pos >= mt.nchars)
1538 //throw new InvalidOperationException ();