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
29 /// On inserting a text in between two characters, if the
30 /// preceding and following characters have Sticky properties of
31 /// the same key with same values, the inserted text inherits
32 /// those properties. In that case, properties of the inserted
33 /// text are overriden.
34 Sticky = 0x01, // 00000001
36 /// On inserting a text before a character, if the character has
37 /// FrontSticky properties, the inserted text inherits those
39 FrontSticky = 0x03, // 00000011
41 /// On inserting a text after a character, if the character has
42 /// RearSticky properties, the inserted text inherits those
44 RearSticky = 0x05, // 00000101
46 /// Like RearSticky, but if the inserted text inherits no
47 /// properties from the preceding character, it inherits
48 /// BothSticky properties from the following character if any.
49 BothSticky = 0x07, // 00000111
51 /// This property is deleted from a span of text if the span is
52 /// modified (i.e. one of a character is changed, a text is
53 /// inserted, some part is deleted). Here, "span" means a
54 /// sequence of characters that has this property with the same
55 /// value. This property is also deleted if a property of the
56 /// same key is added, which means that this property is not
57 /// stackable. In addition this property is never merged with
58 /// the same value of preceding or following property. At last,
59 /// this property can't be sticky in any way.
60 Sensitive = 0x10, // 00010000
62 /// Like Sensitive but also this property is deleted from a span
63 /// of text if a characeter just before the span is modified,
64 /// inserted, or deleted.
65 FrontSensitive = 0x30, // 00110000
67 /// Like Sensitive but also this property is deleted from a span
68 /// of text if a character just after the span is modified,
69 /// inserted, or deleted.
70 RearSensitive = 0x50, // 01010000
72 /// Same as (FrontSensitive | RearSensitive).
73 BothSensitive = 0x70, // 01110000
79 public MSymbol Key { get { return key; } }
80 public object Val { get { return val; } }
82 public MProperty (MSymbol key, object val)
84 if (key.flags == null)
85 key.flags = Flags.None;
90 public MProperty (string name, object val)
92 key = MSymbol.PropertyKey (name);
96 public static bool HasFlags (MSymbol key, Flags flags)
98 return ((key.flags & flags) == flags);
101 public override string ToString ()
103 return key.ToString () + ":" + val;
107 public class MText : IEnumerable, IEquatable<MText>, IComparable<MText>
110 public enum MTextFormat format;
112 private StringBuilder sb;
114 private int cache_pos;
115 private int cache_idx;
116 private MPlist intervals;
117 private MPlist default_property;
118 private bool read_only;
120 private static UTF8Encoding utf8 = new UTF8Encoding ();
122 private static int count_chars (String str)
124 int len = str.Length, n = 0;
126 for (int i = 0; i < len; i++, n++)
127 if (surrogate_high_p (str[i]))
132 private static int count_chars (StringBuilder str)
134 int len = str.Length, n = 0;
136 for (int i = 0; i < len; i++, n++)
137 if (surrogate_high_p (str[i]))
144 sb = new StringBuilder ();
145 intervals = new MPlist ();
148 public MText (byte[] str)
150 sb = new StringBuilder (utf8.GetString (str));
151 nchars = count_chars (sb);
152 intervals = new MPlist ();
155 public MText (byte[] str, int offset, int length)
157 sb = new StringBuilder (utf8.GetString (str, offset, length));
158 nchars = count_chars (sb);
159 intervals = new MPlist ();
162 public MText (String str)
164 sb = new StringBuilder (str);
165 nchars = count_chars (str);
166 intervals = new MPlist ();
169 public MText (StringBuilder str)
172 nchars = count_chars (str);
173 intervals = new MPlist ();
176 public static MText operator+ (object obj, MText mt)
180 MText mtnew = new MText ((string) obj);
181 return mtnew.Ins (mtnew.Length, mt);
183 throw new Exception ("Unknown object type: " + obj.GetType());
186 public static MText operator+ (MText mt, object obj)
189 return mt + new MText ((string) obj);
191 return mt.Dup ().Ins (mt.Length, (int) obj);
193 return mt.Dup ().Ins (mt.Length, (int) ((char) obj));
194 throw new Exception ("Unknown object type: " + obj.GetType());
197 public static MText operator+ (MText mt1, MText mt2)
199 return mt1.Dup ().Ins (mt1.Length, mt2);
203 public bool ReadOnly { get { return read_only; } }
204 public int Length { get { return nchars; } }
208 // for IEnumerable interface
209 public IEnumerator GetEnumerator() { return new MTextEnum (this); }
211 // for IEquatable interface
212 public bool Equals (MText other) { return this.sb.Equals (other.sb); }
214 // for IComparable interface
215 public int CompareTo (MText other)
217 return this.sb.ToString ().CompareTo (other.sb.ToString ());
220 public override string ToString () { return sb.ToString (); }
222 private static bool surrogate_high_p (char c)
224 return (c >= 0xD800 && c < 0xDC00);
227 private static bool surrogate_low_p (char c)
229 return (c >= 0xDC00 && c < 0xE000);
232 private static int inc_idx (StringBuilder sb, int i)
234 return (i + (surrogate_high_p (sb[i]) ? 2 : 1));
237 private static int dec_idx (StringBuilder sb, int i)
239 return (i - (surrogate_low_p (sb[i - 1]) ? 2 : 1));
242 private static int pos_to_idx (MText mt, int pos)
244 if (pos == mt.cache_pos)
250 if (pos < mt.cache_pos)
252 if (mt.cache_pos == mt.cache_idx)
254 if (pos < mt.cache_pos - pos)
261 p = mt.cache_pos; i = mt.cache_idx;
267 if (mt.nchars - mt.cache_pos == mt.sb.Length - mt.cache_idx)
268 return (mt.cache_idx + pos - mt.cache_pos);
269 if (pos - mt.cache_pos < mt.nchars - pos)
271 p = mt.cache_pos; i = mt.cache_idx;
276 p = mt.nchars; i = mt.sb.Length;
281 for (; p < pos; i = inc_idx (mt.sb, i), p++);
283 for (; p > pos; i = dec_idx (mt.sb, i), p--);
289 private void check_pos (int pos, bool tail_ok)
291 if (pos < 0 || (tail_ok ? pos > nchars : pos >= nchars))
292 throw new Exception ("Invalid MText position:" + pos);
295 private bool check_range (int from, int to, bool zero_ok)
297 if (from < 0 || (zero_ok ? from > to : from >= to)
299 throw new Exception ("Invalid MText range");
303 private void insert (int pos, MText mt2, int from, int to)
305 check_pos (pos, true);
309 Console.Write ("inserting {0} to {1} of ", from, to);
310 mt2.DumpPropNested ();
314 foreach (MPlist plist in intervals)
316 MInterval root = (MInterval) plist.Val;
317 MPlist p = mt2.intervals.Find (plist.Key);
318 MInterval i = p == null ? null : (MInterval) p.Val;
320 root.Insert (pos, i, from, to);
322 foreach (MPlist plist in mt2.intervals)
323 if (intervals.Find (plist.Key) == null)
328 root = ((MInterval) plist.Val).Copy (this, from, to);
331 root = new MInterval (plist.Key, this);
332 root.Insert (pos, (MInterval) plist.Val, from, to);
334 intervals.Push (plist.Key, root);
337 int pos_idx = pos_to_idx (this, pos);
338 int from_idx = pos_to_idx (mt2, from);
339 int to_idx = pos_to_idx (mt2, to);
341 sb.Insert (pos_idx, mt2.sb.ToString (from_idx, to_idx - from_idx));
345 private void insert (int pos, int c)
347 check_pos (pos, true);
349 int pos_idx = pos_to_idx (this, pos);
354 sb.Insert (pos_idx, ch);
358 char high = (char) (0xD800 + ((c - 0x10000) >> 10));
359 char low = (char) (0xDC00 + ((c - 0x10000) & 0x3FF));
360 sb.Insert (pos_idx, low);
361 sb.Insert (pos_idx, high);
364 foreach (MPlist plist in intervals)
365 ((MInterval) plist.Val).Insert (pos, null, 0, 1);
368 public int this[int i]
371 i = pos_to_idx (this, i);
374 if (surrogate_high_p (sb[i]))
376 sb[i] = (char) value;
380 char high = (char) (0xD800 + ((value - 0x10000) >> 10));
381 char low = (char) (0xDC00 + ((value - 0x10000) & 0x3FF));
383 if (! surrogate_high_p (sb[i]))
390 i = pos_to_idx (this, i);
391 return (surrogate_high_p (sb[i])
392 ? ((sb[i] - 0xD800) << 10) + (sb[i + 1] - 0xDC00) + 0x10000
399 MText mt = new MText (sb.ToString ());
401 foreach (MPlist p in intervals)
402 mt.intervals.Add (p.Key, ((MInterval) p.Val).Copy (mt, 0, Length));
406 public MText Dup (int from, int to)
408 if (check_range (from, to, true))
410 int from_idx = pos_to_idx (this, from);
411 int len = pos_to_idx (this, to) - from_idx;
412 MText mt = new MText (sb.ToString ().Substring (from_idx, len));
414 foreach (MPlist p in intervals)
415 mt.intervals.Add (p.Key, ((MInterval) p.Val).Copy (mt, from, to));
419 public MText Ins (int pos, int c)
425 public MText Ins (int pos, MText mt)
427 insert (pos, mt, 0, mt.nchars);
431 public MText Ins (int pos, MText mt, int from, int to)
433 insert (pos, mt, from, to);
437 public MText Cat (int c)
443 public MText Del (int from, int to)
445 if (check_range (from, to, true))
448 sb.Remove (from, pos_to_idx (this, to) - pos_to_idx (this, from));
452 foreach (MPlist plist in intervals)
454 MInterval root = (MInterval) plist.Val;
455 root.Delete (from, to);
456 if (from > 0 && from < nchars)
457 ((MInterval) plist.Val).MergeAfterChange (from, from);
466 public object GetProp (int pos, MSymbol key)
468 check_pos (pos, false);
470 MInterval i = (MInterval) intervals.Get (key);
474 MProperty prop = i.Get (pos);
475 return (prop != null ? prop.Val : null);
478 public object GetProp (int pos, MSymbol key, out MProperty prop)
480 check_pos (pos, false);
482 MInterval i = (MInterval) intervals.Get (key);
484 return (prop = null);
486 return (prop != null ? prop.Val : null);
489 public object GetProp (int pos, MSymbol key, out MProperty[] array)
491 check_pos (pos, false);
493 MInterval i = (MInterval) intervals.Get (key);
495 return (array = null);
496 MProperty prop = i.Get (pos, out array);
497 return (prop != null ? prop.Val : null);
500 public void PushProp (int from, int to, MSymbol key, object val)
502 if (! check_range (from, to, true))
503 PushProp (from, to, new MProperty (key, val));
506 public void PushProp (int from, int to, MProperty prop)
510 if (default_property == null)
511 default_property = new MPlist ();
512 default_property.Push (prop.key, prop.val);
516 if (check_range (from, to, true))
519 MPlist p = intervals.Find (prop.key);
524 root = new MInterval (prop.key, this);
525 intervals.Push (prop.key, root);
529 root = (MInterval) p.Val;
530 if (root.isSensitive)
532 root.PopSensitive (from, to);
533 root.MergeAfterChange (from, to);
534 root = (MInterval) p.Val;
539 root.Push (from, to, prop);
540 root.MergeAfterChange (from, to);
545 public void PopProp (int from, int to, MSymbol key)
549 if (default_property == null)
551 MPlist p = default_property.Find (key);
558 if (check_range (from, to, true))
561 MPlist p = intervals.Find (key);
565 MInterval root = (MInterval) p.Val;
566 if (root.isSensitive)
567 root.PopSensitive (from, to);
570 root = (MInterval) p.Val;
573 root.MergeAfterChange (from, to);
579 public void DumpProp ()
582 foreach (MPlist p in intervals)
583 ((MInterval) p.Val).Dump (true);
584 Console.WriteLine (")");
587 public void DumpPropNested ()
589 Console.WriteLine ("total length = {0}", Length);
590 foreach (MPlist p in intervals)
591 ((MInterval) p.Val).DumpNested (true);
594 private class MInterval
596 // position: 0 1 2 3 4 5 6 7
597 // | A | B | C | D | E F | G |
598 // interval |---|---|---|<->|-------|---|
599 // |---|<->|---| |<----->|---|
603 // [3 (1 2)] [3 (4 6)]
604 // [1 (0 1)] [2 (2 3)] [1 (6 7)]
606 private static int count = 0;
609 private int From, To;
611 private MPlist Stack;
612 private MInterval Left, Right, Parent;
615 public MInterval (MSymbol key, MText mt, int length)
618 throw new Exception ("Invalid interval length");
622 Stack = new MPlist ();
626 public MInterval (MSymbol key, MText mt)
630 Length = mt.sb.Length;
633 Stack = new MPlist ();
637 /// POS must be smaller than Length;
638 public MProperty Get (int pos)
640 MInterval i = find_head (pos);
642 return (i.Stack.IsEmpty ? null : (MProperty) i.Stack.Val);
645 /// POS must be smaller than Length;
646 public MProperty Get (int pos, out MProperty[] array)
648 MInterval i = find_head (pos);
657 array = new MProperty[i.Stack.Count];
661 for (idx = 0, p = i.Stack; ! p.IsEmpty; idx++, p = p.Next)
662 array[idx] = (MProperty) p.Val;
666 private MInterval (MSymbol key, MText mt, int length, MPlist stack)
673 Stack = stack == null ? new MPlist () : stack.Clone ();
677 private bool isRearSticky
679 get { return MProperty.HasFlags (Key, MProperty.Flags.RearSticky) ; }
682 private bool isFrontSticky
684 get { return MProperty.HasFlags (Key, MProperty.Flags.FrontSticky) ; }
687 public bool isSensitive
689 get { return MProperty.HasFlags (Key, MProperty.Flags.Sensitive) ; }
692 public bool isFrontSensitive
694 get { return MProperty.HasFlags (Key,
695 MProperty.Flags.FrontSensitive) ; }
698 public bool isRearSensitive
700 get { return MProperty.HasFlags (Key,
701 MProperty.Flags.RearSensitive) ; }
704 private void update_from_to ()
709 To = Length - RightLength;
711 else if (Parent.Left == this)
713 From = Parent.From - Length + LeftLength;
714 To = Parent.From - RightLength;
718 From = Parent.To + LeftLength;
719 To = Parent.To + Length - RightLength;
723 private int LeftLength
725 get { return (Left == null ? 0 : Left.Length); }
728 private int RightLength
730 get { return (Right == null ? 0 : Right.Length); }
733 private MInterval LeftMost
739 return Left.LeftMost;
743 private MInterval RightMost
749 return Right.RightMost;
753 private MInterval Prev {
759 for (i = Left; i.Right != null; i = i.Right)
765 MInterval child = this;
766 for (i = Parent; i != null && i.Left == child;
767 child = i, i = i.Parent);
773 private MInterval Next {
779 for (i = Right; i.Left != null; i = i.Left)
785 MInterval child = this;
786 for (i = Parent; i != null && i.Right == child;
787 child = i, i = i.Parent);
793 private MInterval find_head (int pos)
797 return Left.find_head (pos);
799 return Right.find_head (pos);
803 private MInterval find_tail (int pos)
807 return Left.find_tail (pos);
809 return Right.find_tail (pos);
813 private bool mergeable (MInterval i)
817 if (Stack.IsEmpty && i.Stack.IsEmpty)
821 for (p1 = Stack, p2 = i.Stack; ! p1.IsEmpty && ! p2.IsEmpty;
822 p1 = p1.Next, p2 = p2.Next)
823 if (p1.Val != p2.Val)
825 return (p1.IsEmpty && p2.IsEmpty);
828 // p-. or .-p p-. or .-p
829 // .-this-. .-right-.
830 // left .-right-. -> .-this-. c2
832 private MInterval promote_right ()
834 MInterval c1 = Right.Left;
838 mtext.intervals.Put (Key, Right);
839 else if (Parent.Left == this)
842 Parent.Right = Right;
845 Right.Parent = Parent;
847 Right.Length += LeftLength + (To - From);
852 Length = LeftLength + (To - From) + RightLength;
854 // Update C1 if necessary.
858 Parent.update_from_to ();
862 // p-. or .-p p-. or .-p
864 // .-left-. .-right-. -> c1 .-this-.
866 private MInterval promote_left ()
868 MInterval c2 = Left.Right;
872 mtext.intervals.Put (Key, Left);
873 else if (Parent.Left == this)
879 Left.Parent = Parent;
881 Left.Length += (To - From) + RightLength;
886 Length = LeftLength + (To - From) + RightLength;
888 // Update C2 if necessary.
892 Parent.update_from_to ();
896 public MInterval Balance ()
904 // .-left-. .-right-.
906 int diff = i.LeftLength - i.RightLength;
911 new_diff = (i.Length - i.LeftLength
912 + i.Left.RightLength - i.Left.LeftLength);
913 if (Math.Abs (new_diff) >= diff)
915 M17n.DebugPrint ("balancing #{0} by promoting left...", i.ID);
916 i = i.promote_left ();
917 M17n.DebugPrint ("done\n");
922 new_diff = (i.Length - i.RightLength
923 + i.Right.LeftLength - i.Right.RightLength);
924 if (Math.Abs (new_diff) >= diff)
926 M17n.DebugPrint ("balancing #{0} by promoting right\n", i.ID);
927 i = i.promote_right ();
936 public MInterval Copy (MText mt, int start, int end)
938 MInterval copy, left_copy = null, right_copy = null;
945 return Left.Copy (mt, start, end);
946 left_copy = Left.Copy (mt, start, From);
951 return Right.Copy (mt, start, end);
952 right_copy = Right.Copy (mt, To, end);
955 copy = new MInterval (Key, null, end - start, Stack);
957 if (isSensitive && (From < start || end < To))
959 if (left_copy != null)
961 copy.Left = left_copy;
962 left_copy.Parent = copy;
964 if (right_copy != null)
966 copy.Right = right_copy;
967 right_copy.Parent = copy;
972 public MInterval Copy (MText mt, int start, int end,
973 bool first, bool last)
975 MInterval copy = Copy (mt, start, end);
976 MInterval head = find_head (start);
977 MInterval tail = find_tail (end);
979 M17n.DebugPrint ("Copying: {0}", copy);
981 if (! head.Stack.IsEmpty
982 && (isSensitive && head.From < start
983 || (isFrontSensitive && ! first)))
985 M17n.DebugPrint (" clear head");
986 head = copy.find_head (0);
989 if (! tail.Stack.IsEmpty
990 && (isSensitive && end < tail.To
991 || (isRearSensitive && ! last)))
993 M17n.DebugPrint (" clear tail");
994 tail = copy.find_tail (copy.Length);
997 M17n.DebugPrint ("\n");
1001 // this-. ==> this-.
1004 private MInterval divide_right (int pos)
1006 MInterval interval = new MInterval (Key, mtext, To - pos, Stack);
1008 M17n.DebugPrint ("divide-right({0}) at {1}\n", pos, this);
1012 interval.Right = Right;
1013 Right.Parent = interval;
1014 interval.Length += Right.Length;
1016 interval.Parent = this;
1021 // .-this ==> .-this
1024 private MInterval divide_left (int pos)
1026 MInterval interval = new MInterval (Key, mtext, pos - From, Stack);
1028 M17n.DebugPrint ("divide-left({0}) at {1}\n", pos, this);
1032 interval.Left = Left;
1033 Left.Parent = interval;
1034 interval.Length += Left.Length;
1036 interval.Parent = this;
1041 private void set_mtext (MText mt)
1045 Left.set_mtext (mt);
1047 Right.set_mtext (mt);
1050 private void enlarge (int len)
1054 for (MInterval prev = this, i = this.Parent; i != null;
1055 prev = i, i = i.Parent)
1066 private int graft_forward (MInterval interval, int start, int end)
1070 if (! Stack.IsEmpty && isRearSticky)
1072 else if (interval == null)
1073 len = Stack.IsEmpty ? end - start : 0;
1076 MInterval i = interval.find_head (start);
1080 && (isFrontSensitive || (isSensitive && i.From < start)))
1082 M17n.DebugPrint (" forward grafting {0}", i);
1089 while (i != null && i.From < end && mergeable (i))
1091 M17n.DebugPrint (" forward grafting {0}", i);
1092 len += i.To - i.From;
1094 len -= start - i.From;
1104 M17n.DebugPrint (" grafted {0} in {1}\n", len, this);
1110 private int graft_backward (MInterval interval, int start, int end)
1114 if (! Stack.IsEmpty && isFrontSticky)
1116 else if (interval == null)
1117 len = Stack.IsEmpty ? end - start : 0;
1120 MInterval i = interval.find_tail (end);
1124 && (isRearSensitive || (isSensitive && end < i.To)))
1126 M17n.DebugPrint (" backward grafting {0}", i);
1127 if (i.From <= start)
1133 while (i != null && i.To <= start && mergeable (i))
1135 M17n.DebugPrint (" backward grafting {0}", i);
1136 len += i.To - i.From;
1139 if (i.From <= start)
1141 len -= start - i.From;
1148 M17n.DebugPrint (" grafted {0} in {1}\n", len, this);
1154 public void Insert (int pos, MInterval interval, int start, int end)
1158 M17n.DebugPrint ("insert({0} to {1}) at {2} in {3}",
1159 start, end, pos, this);
1162 Left.Insert (pos, interval, start, end);
1163 else if (pos == From)
1165 MInterval prev = Left != null ? Prev : null;
1167 if (isFrontSensitive)
1169 if (prev != null && isRearSensitive)
1170 prev.Stack.Clear ();
1171 if (prev != null && isRearSticky && ! prev.Stack.IsEmpty)
1173 prev.enlarge (end - start);
1174 M17n.DebugPrint (" done\n");
1177 if (isFrontSticky && ! Stack.IsEmpty)
1179 enlarge (end - start);
1180 M17n.DebugPrint (" done\n");
1183 bool front_grafted = false, rear_grafted = false;
1186 && (grafted = prev.graft_forward (interval, start, end)) > 0)
1191 M17n.DebugPrint (" done\n");
1194 front_grafted = true;
1196 if ((grafted = graft_backward (interval, start, end)) > 0)
1201 M17n.DebugPrint (" done\n");
1204 rear_grafted = true;
1207 if (interval != null)
1208 interval = interval.Copy (mtext, start, end,
1210 || (prev == null && start == 0)),
1213 interval = new MInterval (Key, mtext, end - start, null);
1218 // .-this-. ==> .-this-.
1230 interval.Parent = i;
1231 for (; i != null; i = i.Parent)
1232 i.Length += interval.Length;
1236 if (! Stack.IsEmpty)
1240 else if (isFrontSticky || isRearSticky)
1242 enlarge (end - start);
1246 bool front_grafted = false, rear_grafted = false;
1248 if ((grafted = graft_forward (interval, start, end)) > 0)
1253 M17n.DebugPrint (" done\n");
1256 front_grafted = true;
1259 if ((grafted = graft_backward (interval, start, end)) > 0)
1264 M17n.DebugPrint (" done\n");
1267 rear_grafted = true;
1269 if (interval != null)
1270 interval = interval.Copy (mtext, start, end,
1271 front_grafted, rear_grafted);
1273 interval = new MInterval (Key, mtext, end - start, null);
1276 Right.Left = interval;
1277 interval.Parent = Right;
1278 for (MInterval i = Right; i != null; i = i.Parent)
1279 i.Length += interval.Length;
1283 MInterval next = Right != null ? Next : null;
1285 if (isRearSensitive)
1287 if (next != null && isFrontSensitive)
1288 next.Stack.Clear ();
1289 if (isRearSticky && ! Stack.IsEmpty)
1291 enlarge (end - start);
1292 M17n.DebugPrint (" done by enlarging this\n");
1295 if (next != null && isFrontSticky && ! next.Stack.IsEmpty)
1297 M17n.DebugPrint (" next is {0}\n", next);
1298 next.enlarge (end - start);
1299 M17n.DebugPrint (" done by enlarging next\n");
1302 bool front_grafted = false, rear_grafted = false;
1305 && (grafted = next.graft_backward (interval, start, end)) > 0)
1310 M17n.DebugPrint (" done\n");
1313 rear_grafted = true;
1315 if ((grafted = graft_forward (interval, start, end)) > 0)
1320 M17n.DebugPrint (" done\n");
1323 front_grafted = true;
1325 if (interval != null)
1326 interval = interval.Copy (mtext, start, end,
1329 || (next == null && end == interval.mtext.Length)));
1331 interval = new MInterval (Key, mtext, end - start, null);
1336 // .-this-. ==> .-this-.
1349 interval.Parent = i;
1350 for (; i != null; i = i.Parent)
1351 i.Length += interval.Length;
1354 Right.Insert (pos, interval, start, end);
1355 M17n.DebugPrint (" done\n");
1358 private void vacate_node (MInterval interval)
1360 vacate_node (interval, null);
1363 private void vacate_node (MInterval interval, MInterval stop)
1365 if (interval != null)
1366 M17n.DebugPrint ("vacate #{0} to #{1}\n", ID, interval.ID);
1368 M17n.DebugPrint ("vacate #{0} to null\n", ID);
1369 if (interval != null)
1370 interval.Parent = Parent;
1374 mtext.intervals.Put (Key, interval);
1378 if (this == Parent.Right)
1379 Parent.Right = interval;
1381 Parent.Left = interval;
1384 if (interval != null)
1385 diff -= interval.Length;
1386 for (MInterval i = Parent; i != stop; i = i.Parent)
1391 public void Delete (int start, int end)
1394 M17n.DebugPrint ("delete({0} {1}) from {2}\n", start, end, this);
1396 bool front_checked = false;
1397 bool rear_checked = false;
1403 if (end == From && isFrontSensitive)
1405 Left.Delete (start, end);
1410 Left.Delete (start, From);
1412 end -= From - start;
1414 front_checked = true;
1420 if (start == To && isRearSensitive)
1422 Right.Delete (start, end);
1427 Right.Delete (To, end);
1429 rear_checked = true;
1435 Prev.Stack.Clear ();
1439 && isFrontSensitive)
1440 Next.Stack.Clear ();
1441 if (start == From && end == To)
1453 for (i = Right; i.Left != null; i = i.Left)
1454 i.Length += Left.Length;
1455 i.Length += Left.Length;
1459 vacate_node (Right);
1464 int len = end - start;
1468 for (MInterval i = this; i != null; i = i.Parent)
1473 public void Push (int start, int end, MProperty prop)
1476 M17n.DebugPrint ("push({0} {1}) at {2}\n", start, end, this);
1481 Left.Push (start, end, prop);
1484 Left.Push (start, From, prop);
1491 Right.Push (start, end, prop);
1494 Right.Push (To, end, prop);
1498 if (! Stack.IsEmpty && isSensitive)
1501 divide_left (start);
1504 Stack.Push (prop.key, prop);
1507 /// Combine intervals between HEAD and TAIL (both inclusive) to
1508 /// the common parent of HEAD and TAIL. Callers should assure
1509 /// that the intervals are mergeable in advance.
1510 private static void combine (MInterval head, MInterval tail)
1512 M17n.DebugPrint ("combining {0} through {1}", head, tail);
1514 head.update_from_to ();
1515 tail.update_from_to ();
1516 int from = head.From;
1519 // The nearest common parent of HEAD and TAIL.
1521 for (root = head; root.To + root.RightLength < to;
1522 root = root.Parent);
1524 M17n.DebugPrint (" with common root {0}\n", root);
1526 if (from < root.From)
1528 MInterval prev = root.Prev;
1532 M17n.DebugPrint ("merging {0}\n", prev);
1533 prev.vacate_node (prev.Left, root);
1536 if (prev.Left != null)
1537 prev = prev.Left.RightMost;
1541 root.update_from_to ();
1545 MInterval next = root.Next;
1549 M17n.DebugPrint ("merging {0}\n", next);
1550 next.vacate_node (next.Right, root);
1553 if (next.Right != null)
1554 next = next.Right.LeftMost;
1558 root.update_from_to ();
1562 public void MergeAfterChange (int start, int end)
1566 MInterval head = find_head (start), i = head;
1567 MInterval tail = start < end ? find_tail (end) : head;
1569 if (tail.To < Length)
1572 if (start == head.From && start > 0)
1574 MInterval prev = head.Prev;
1575 if (head.mergeable (prev))
1578 M17n.DebugPrint ("merge between {0} and {1}\n", head, tail);
1581 MInterval next = i.Next;
1583 if (! i.mergeable (next))
1595 public void Pop (int start, int end)
1598 M17n.DebugPrint ("pop({0} {1}) at {2}\n", start, end, this);
1603 Left.Pop (start, end);
1606 Left.Pop (start, From);
1613 Right.Pop (start, end);
1616 Right.Pop (To, end);
1620 if (! Stack.IsEmpty)
1627 divide_left (start);
1635 public void PopSensitive (int start, int end)
1638 MInterval head = find_head (start);
1639 MInterval tail = find_tail (end);
1640 Pop (head.From, tail.To);
1643 public override string ToString ()
1645 string str = String.Format ("#{0}({1} {2} {3} [", ID, Length, From, To);
1647 foreach (MPlist p in Stack)
1651 str += ((MProperty) p.Val).Val;
1655 str += " " + ((MProperty) p.Val).Val;
1657 return (str + "])");
1660 private void DumpOne (bool with_prop, bool newline, bool force)
1662 if (force || M17n.debug)
1664 Console.Write ("#{0}({1} {2} {3}", ID, Length, From, To);
1665 if (with_prop && ! Stack.IsEmpty)
1667 string prepend = " [";
1668 foreach (MPlist p in Stack)
1670 Console.Write (prepend + ((MProperty) p.Val).Val);
1673 Console.Write ("]");
1675 Console.Write (")");
1677 Console.WriteLine ();
1679 throw new Exception ("Invalid interval length");
1683 public void Dump () { Dump (false); }
1685 public void Dump (bool force)
1687 if (force || M17n.debug)
1694 Console.Write (" ");
1695 DumpOne (true, false, force);
1702 get { return (Parent == null ? 0 : Parent.Depth + 1); }
1705 public void DumpNested (bool force)
1707 DumpNested (" " + Key.ToString () + ":", force);
1710 public void DumpNested (string indent, bool force)
1712 if (force || M17n.debug)
1714 int indent_type = (Parent == null ? 1
1715 : Parent.Left == this ? 0 : 2);
1720 if (indent_type <= 1)
1721 Left.DumpNested (indent + " ", force);
1723 Left.DumpNested (indent + "| ", force);
1725 Console.Write (indent);
1726 if (indent_type == 0)
1727 Console.Write (".-");
1728 else if (indent_type == 2)
1729 Console.Write ("`-");
1730 DumpOne (true, true, true);
1733 if (indent_type >= 1)
1734 Right.DumpNested (indent + " ", force);
1736 Right.DumpNested (indent + "| ", force);
1742 private class MTextEnum : IEnumerator
1745 private int pos = -1;
1747 public MTextEnum (MText mt)
1752 public bool MoveNext ()
1755 return (pos < mt.nchars);
1758 public void Reset ()
1763 public object Current
1766 //if (pos < 0 || pos >= mt.nchars)
1767 //throw new InvalidOperationException ();