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 (String str)
157 sb = new StringBuilder (str);
158 nchars = count_chars (str);
159 intervals = new MPlist ();
162 public MText (StringBuilder str)
165 nchars = count_chars (str);
166 intervals = new MPlist ();
169 public static MText operator+ (object obj, MText mt)
173 MText mtnew = new MText ((string) obj);
174 return mtnew.Ins (mtnew.Length, mt);
176 throw new Exception ("Unknown object type: " + obj.GetType());
179 public static MText operator+ (MText mt, object obj)
182 return mt + new MText ((string) obj);
184 return mt.Dup ().Ins (mt.Length, (int) obj);
186 return mt.Dup ().Ins (mt.Length, (int) ((char) obj));
187 throw new Exception ("Unknown object type: " + obj.GetType());
190 public static MText operator+ (MText mt1, MText mt2)
192 return mt1.Dup ().Ins (mt1.Length, mt2);
196 public bool ReadOnly { get { return read_only; } }
197 public int Length { get { return nchars; } }
201 // for IEnumerable interface
202 public IEnumerator GetEnumerator() { return new MTextEnum (this); }
204 // for IEquatable interface
205 public bool Equals (MText other) { return this.sb.Equals (other.sb); }
207 // for IComparable interface
208 public int CompareTo (MText other)
210 return this.sb.ToString ().CompareTo (other.sb.ToString ());
213 public override string ToString () { return sb.ToString (); }
215 private static bool surrogate_high_p (char c)
217 return (c >= 0xD800 && c < 0xDC00);
220 private static bool surrogate_low_p (char c)
222 return (c >= 0xDC00 && c < 0xE000);
225 private static int inc_idx (StringBuilder sb, int i)
227 return (i + (surrogate_high_p (sb[i]) ? 2 : 1));
230 private static int dec_idx (StringBuilder sb, int i)
232 return (i - (surrogate_low_p (sb[i - 1]) ? 2 : 1));
235 private static int pos_to_idx (MText mt, int pos)
237 if (pos == mt.cache_pos)
243 if (pos < mt.cache_pos)
245 if (mt.cache_pos == mt.cache_idx)
247 if (pos < mt.cache_pos - pos)
254 p = mt.cache_pos; i = mt.cache_idx;
260 if (mt.nchars - mt.cache_pos == mt.sb.Length - mt.cache_idx)
261 return (mt.cache_idx + pos - mt.cache_pos);
262 if (pos - mt.cache_pos < mt.nchars - pos)
264 p = mt.cache_pos; i = mt.cache_idx;
269 p = mt.nchars; i = mt.sb.Length;
274 for (; p < pos; i = inc_idx (mt.sb, i), p++);
276 for (; p > pos; i = dec_idx (mt.sb, i), p--);
282 private void check_pos (int pos, bool tail_ok)
284 if (pos < 0 || (tail_ok ? pos > nchars : pos >= nchars))
285 throw new Exception ("Invalid MText position:" + pos);
288 private bool check_range (int from, int to, bool zero_ok)
290 if (from < 0 || (zero_ok ? from > to : from >= to)
292 throw new Exception ("Invalid MText range");
296 private void insert (int pos, MText mt2, int from, int to)
298 check_pos (pos, true);
302 Console.Write ("inserting {0} to {1} of ", from, to);
303 mt2.DumpPropNested ();
307 foreach (MPlist plist in intervals)
309 MInterval root = (MInterval) plist.Val;
310 MPlist p = mt2.intervals.Find (plist.Key);
311 MInterval i = p == null ? null : (MInterval) p.Val;
313 root.Insert (pos, i, from, to);
315 foreach (MPlist plist in mt2.intervals)
316 if (intervals.Find (plist.Key) == null)
321 root = ((MInterval) plist.Val).Copy (this, from, to);
324 root = new MInterval (plist.Key, this);
325 root.Insert (pos, (MInterval) plist.Val, from, to);
327 intervals.Push (plist.Key, root);
330 int pos_idx = pos_to_idx (this, pos);
331 int from_idx = pos_to_idx (mt2, from);
332 int to_idx = pos_to_idx (mt2, to);
334 sb.Insert (pos_idx, mt2.sb.ToString (from_idx, to_idx - from_idx));
338 private void insert (int pos, int c)
340 check_pos (pos, true);
342 int pos_idx = pos_to_idx (this, pos);
347 sb.Insert (pos_idx, ch);
351 char high = (char) (0xD800 + ((c - 0x10000) >> 10));
352 char low = (char) (0xDC00 + ((c - 0x10000) & 0x3FF));
353 sb.Insert (pos_idx, low);
354 sb.Insert (pos_idx, high);
357 foreach (MPlist plist in intervals)
358 ((MInterval) plist.Val).Insert (pos, null, 0, 1);
361 public int this[int i]
364 i = pos_to_idx (this, i);
367 if (surrogate_high_p (sb[i]))
369 sb[i] = (char) value;
373 char high = (char) (0xD800 + ((value - 0x10000) >> 10));
374 char low = (char) (0xDC00 + ((value - 0x10000) & 0x3FF));
376 if (! surrogate_high_p (sb[i]))
383 i = pos_to_idx (this, i);
384 return (surrogate_high_p (sb[i])
385 ? ((sb[i] - 0xD800) << 10) + (sb[i + 1] - 0xDC00) + 0x10000
392 MText mt = new MText (sb.ToString ());
394 foreach (MPlist p in intervals)
395 mt.intervals.Add (p.Key, ((MInterval) p.Val).Copy (mt, 0, Length));
399 public MText Dup (int from, int to)
401 if (check_range (from, to, true))
403 int from_idx = pos_to_idx (this, from);
404 int len = pos_to_idx (this, to) - from_idx;
405 MText mt = new MText (sb.ToString ().Substring (from_idx, len));
407 foreach (MPlist p in intervals)
408 mt.intervals.Add (p.Key, ((MInterval) p.Val).Copy (mt, from, to));
412 public MText Ins (int pos, int c)
418 public MText Ins (int pos, MText mt)
420 insert (pos, mt, 0, mt.nchars);
424 public MText Ins (int pos, MText mt, int from, int to)
426 insert (pos, mt, from, to);
430 public MText Cat (int c)
436 public MText Del (int from, int to)
438 if (check_range (from, to, true))
441 sb.Remove (from, pos_to_idx (this, to) - pos_to_idx (this, from));
445 foreach (MPlist plist in intervals)
447 MInterval root = (MInterval) plist.Val;
448 root.Delete (from, to);
449 if (from > 0 && from < nchars)
450 ((MInterval) plist.Val).MergeAfterChange (from, from);
459 public object GetProp (int pos, MSymbol key)
461 check_pos (pos, false);
463 MInterval i = (MInterval) intervals.Get (key);
467 MProperty prop = i.Get (pos);
468 return (prop != null ? prop.Val : null);
471 public object GetProp (int pos, MSymbol key, out MProperty prop)
473 check_pos (pos, false);
475 MInterval i = (MInterval) intervals.Get (key);
477 return (prop = null);
479 return (prop != null ? prop.Val : null);
482 public object GetProp (int pos, MSymbol key, out MProperty[] array)
484 check_pos (pos, false);
486 MInterval i = (MInterval) intervals.Get (key);
488 return (array = null);
489 MProperty prop = i.Get (pos, out array);
490 return (prop != null ? prop.Val : null);
493 public void PushProp (int from, int to, MSymbol key, object val)
495 if (! check_range (from, to, true))
496 PushProp (from, to, new MProperty (key, val));
499 public void PushProp (int from, int to, MProperty prop)
503 if (default_property == null)
504 default_property = new MPlist ();
505 default_property.Push (prop.key, prop.val);
509 if (check_range (from, to, true))
512 MPlist p = intervals.Find (prop.key);
517 root = new MInterval (prop.key, this);
518 intervals.Push (prop.key, root);
522 root = (MInterval) p.Val;
523 if (root.isSensitive)
525 root.PopSensitive (from, to);
526 root.MergeAfterChange (from, to);
527 root = (MInterval) p.Val;
532 root.Push (from, to, prop);
533 root.MergeAfterChange (from, to);
538 public void PopProp (int from, int to, MSymbol key)
542 if (default_property == null)
544 MPlist p = default_property.Find (key);
551 if (check_range (from, to, true))
554 MPlist p = intervals.Find (key);
558 MInterval root = (MInterval) p.Val;
559 if (root.isSensitive)
560 root.PopSensitive (from, to);
563 root = (MInterval) p.Val;
566 root.MergeAfterChange (from, to);
572 public void DumpProp ()
575 foreach (MPlist p in intervals)
576 ((MInterval) p.Val).Dump (true);
577 Console.WriteLine (")");
580 public void DumpPropNested ()
582 Console.WriteLine ("total length = {0}", Length);
583 foreach (MPlist p in intervals)
584 ((MInterval) p.Val).DumpNested (true);
587 private class MInterval
589 // position: 0 1 2 3 4 5 6 7
590 // | A | B | C | D | E F | G |
591 // interval |---|---|---|<->|-------|---|
592 // |---|<->|---| |<----->|---|
596 // [3 (1 2)] [3 (4 6)]
597 // [1 (0 1)] [2 (2 3)] [1 (6 7)]
599 private static int count = 0;
602 private int From, To;
604 private MPlist Stack;
605 private MInterval Left, Right, Parent;
608 public MInterval (MSymbol key, MText mt, int length)
611 throw new Exception ("Invalid interval length");
615 Stack = new MPlist ();
619 public MInterval (MSymbol key, MText mt)
623 Length = mt.sb.Length;
626 Stack = new MPlist ();
630 /// POS must be smaller than Length;
631 public MProperty Get (int pos)
633 MInterval i = find_head (pos);
635 return (i.Stack.IsEmpty ? null : (MProperty) i.Stack.Val);
638 /// POS must be smaller than Length;
639 public MProperty Get (int pos, out MProperty[] array)
641 MInterval i = find_head (pos);
650 array = new MProperty[i.Stack.Count];
654 for (idx = 0, p = i.Stack; ! p.IsEmpty; idx++, p = p.Next)
655 array[idx] = (MProperty) p.Val;
659 private MInterval (MSymbol key, MText mt, int length, MPlist stack)
666 Stack = stack == null ? new MPlist () : stack.Clone ();
670 private bool isRearSticky
672 get { return MProperty.HasFlags (Key, MProperty.Flags.RearSticky) ; }
675 private bool isFrontSticky
677 get { return MProperty.HasFlags (Key, MProperty.Flags.FrontSticky) ; }
680 public bool isSensitive
682 get { return MProperty.HasFlags (Key, MProperty.Flags.Sensitive) ; }
685 public bool isFrontSensitive
687 get { return MProperty.HasFlags (Key,
688 MProperty.Flags.FrontSensitive) ; }
691 public bool isRearSensitive
693 get { return MProperty.HasFlags (Key,
694 MProperty.Flags.RearSensitive) ; }
697 private void update_from_to ()
702 To = Length - RightLength;
704 else if (Parent.Left == this)
706 From = Parent.From - Length + LeftLength;
707 To = Parent.From - RightLength;
711 From = Parent.To + LeftLength;
712 To = Parent.To + Length - RightLength;
716 private int LeftLength
718 get { return (Left == null ? 0 : Left.Length); }
721 private int RightLength
723 get { return (Right == null ? 0 : Right.Length); }
726 private MInterval LeftMost
732 return Left.LeftMost;
736 private MInterval RightMost
742 return Right.RightMost;
746 private MInterval Prev {
752 for (i = Left; i.Right != null; i = i.Right)
758 MInterval child = this;
759 for (i = Parent; i != null && i.Left == child;
760 child = i, i = i.Parent);
766 private MInterval Next {
772 for (i = Right; i.Left != null; i = i.Left)
778 MInterval child = this;
779 for (i = Parent; i != null && i.Right == child;
780 child = i, i = i.Parent);
786 private MInterval find_head (int pos)
790 return Left.find_head (pos);
792 return Right.find_head (pos);
796 private MInterval find_tail (int pos)
800 return Left.find_tail (pos);
802 return Right.find_tail (pos);
806 private bool mergeable (MInterval i)
810 if (Stack.IsEmpty && i.Stack.IsEmpty)
814 for (p1 = Stack, p2 = i.Stack; ! p1.IsEmpty && ! p2.IsEmpty;
815 p1 = p1.Next, p2 = p2.Next)
816 if (p1.Val != p2.Val)
818 return (p1.IsEmpty && p2.IsEmpty);
821 // p-. or .-p p-. or .-p
822 // .-this-. .-right-.
823 // left .-right-. -> .-this-. c2
825 private MInterval promote_right ()
827 MInterval c1 = Right.Left;
831 mtext.intervals.Put (Key, Right);
832 else if (Parent.Left == this)
835 Parent.Right = Right;
838 Right.Parent = Parent;
840 Right.Length += LeftLength + (To - From);
845 Length = LeftLength + (To - From) + RightLength;
847 // Update C1 if necessary.
851 Parent.update_from_to ();
855 // p-. or .-p p-. or .-p
857 // .-left-. .-right-. -> c1 .-this-.
859 private MInterval promote_left ()
861 MInterval c2 = Left.Right;
865 mtext.intervals.Put (Key, Left);
866 else if (Parent.Left == this)
872 Left.Parent = Parent;
874 Left.Length += (To - From) + RightLength;
879 Length = LeftLength + (To - From) + RightLength;
881 // Update C2 if necessary.
885 Parent.update_from_to ();
889 public MInterval Balance ()
897 // .-left-. .-right-.
899 int diff = i.LeftLength - i.RightLength;
904 new_diff = (i.Length - i.LeftLength
905 + i.Left.RightLength - i.Left.LeftLength);
906 if (Math.Abs (new_diff) >= diff)
908 M17n.DebugPrint ("balancing #{0} by promoting left...", i.ID);
909 i = i.promote_left ();
910 M17n.DebugPrint ("done\n");
915 new_diff = (i.Length - i.RightLength
916 + i.Right.LeftLength - i.Right.RightLength);
917 if (Math.Abs (new_diff) >= diff)
919 M17n.DebugPrint ("balancing #{0} by promoting right\n", i.ID);
920 i = i.promote_right ();
929 public MInterval Copy (MText mt, int start, int end)
931 MInterval copy, left_copy = null, right_copy = null;
938 return Left.Copy (mt, start, end);
939 left_copy = Left.Copy (mt, start, From);
944 return Right.Copy (mt, start, end);
945 right_copy = Right.Copy (mt, To, end);
948 copy = new MInterval (Key, null, end - start, Stack);
950 if (isSensitive && (From < start || end < To))
952 if (left_copy != null)
954 copy.Left = left_copy;
955 left_copy.Parent = copy;
957 if (right_copy != null)
959 copy.Right = right_copy;
960 right_copy.Parent = copy;
965 public MInterval Copy (MText mt, int start, int end,
966 bool first, bool last)
968 MInterval copy = Copy (mt, start, end);
969 MInterval head = find_head (start);
970 MInterval tail = find_tail (end);
972 if (! head.Stack.IsEmpty
973 && (isSensitive && head.From < start
974 || isFrontSensitive && ! first))
976 head = copy.find_head (0);
979 if (! tail.Stack.IsEmpty
980 && (isSensitive && end < head.To
981 || isRearSensitive && ! last))
983 tail = copy.find_tail (copy.Length);
986 M17n.DebugPrint ("Copied: {0}\n", copy);
993 private MInterval divide_right (int pos)
995 MInterval interval = new MInterval (Key, mtext, To - pos, Stack);
997 M17n.DebugPrint ("divide-right({0}) at {1}\n", pos, this);
1001 interval.Right = Right;
1002 Right.Parent = interval;
1003 interval.Length += Right.Length;
1005 interval.Parent = this;
1010 // .-this ==> .-this
1013 private MInterval divide_left (int pos)
1015 MInterval interval = new MInterval (Key, mtext, pos - From, Stack);
1017 M17n.DebugPrint ("divide-left({0}) at {1}\n", pos, this);
1021 interval.Left = Left;
1022 Left.Parent = interval;
1023 interval.Length += Left.Length;
1025 interval.Parent = this;
1030 private void set_mtext (MText mt)
1034 Left.set_mtext (mt);
1036 Right.set_mtext (mt);
1039 private void enlarge (int len)
1043 for (MInterval prev = this, i = this.Parent; i != null;
1044 prev = i, i = i.Parent)
1055 private int graft_forward (MInterval interval, int start, int end)
1059 if (! Stack.IsEmpty && isRearSticky)
1061 else if (interval == null)
1062 len = Stack.IsEmpty ? end - start : 0;
1065 MInterval i = interval.find_head (start);
1069 && (isFrontSensitive || (isSensitive && i.From < start)))
1071 M17n.DebugPrint (" forward grafting {0}", i);
1078 while (i != null && i.From < end && mergeable (i))
1080 M17n.DebugPrint (" forward grafting {0}", i);
1081 len += i.To - i.From;
1083 len -= start - i.From;
1093 M17n.DebugPrint (" grafted {0} in {1}\n", len, this);
1099 private int graft_backward (MInterval interval, int start, int end)
1103 if (! Stack.IsEmpty && isFrontSticky)
1105 else if (interval == null)
1106 len = Stack.IsEmpty ? end - start : 0;
1109 MInterval i = interval.find_tail (end);
1113 && (isRearSensitive || (isSensitive && end < i.To)))
1115 M17n.DebugPrint (" backward grafting {0}", i);
1116 if (i.From <= start)
1122 while (i != null && i.To <= start && mergeable (i))
1124 M17n.DebugPrint (" backward grafting {0}", i);
1125 len += i.To - i.From;
1128 if (i.From <= start)
1130 len -= start - i.From;
1137 M17n.DebugPrint (" grafted {0} in {1}\n", len, this);
1143 public void Insert (int pos, MInterval interval, int start, int end)
1147 M17n.DebugPrint ("insert({0} to {1}) at {2} in {3}",
1148 start, end, pos, this);
1151 Left.Insert (pos, interval, start, end);
1152 else if (pos == From)
1154 MInterval prev = Left != null ? Prev : null;
1156 if (isFrontSensitive)
1158 if (prev != null && isRearSensitive)
1159 prev.Stack.Clear ();
1160 if (prev != null && isRearSticky && ! prev.Stack.IsEmpty)
1162 prev.enlarge (end - start);
1163 M17n.DebugPrint (" done\n");
1166 if (isFrontSticky && ! Stack.IsEmpty)
1168 enlarge (end - start);
1169 M17n.DebugPrint (" done\n");
1172 bool front_grafted = false, rear_grafted = false;
1175 && (grafted = prev.graft_forward (interval, start, end)) > 0)
1180 M17n.DebugPrint (" done\n");
1183 front_grafted = true;
1185 if ((grafted = graft_backward (interval, start, end)) > 0)
1190 M17n.DebugPrint (" done\n");
1193 rear_grafted = true;
1196 if (interval != null)
1197 interval = interval.Copy (mtext, start, end,
1199 || (prev == null && start == 0)),
1202 interval = new MInterval (Key, mtext, end - start, null);
1207 // .-this-. ==> .-this-.
1219 interval.Parent = i;
1220 for (; i != null; i = i.Parent)
1221 i.Length += interval.Length;
1225 if (! Stack.IsEmpty)
1229 else if (isFrontSticky || isRearSticky)
1231 enlarge (end - start);
1235 bool front_grafted = false, rear_grafted = false;
1237 if ((grafted = graft_forward (interval, start, end)) > 0)
1242 M17n.DebugPrint (" done\n");
1245 front_grafted = true;
1248 if ((grafted = graft_backward (interval, start, end)) > 0)
1253 M17n.DebugPrint (" done\n");
1256 rear_grafted = true;
1258 if (interval != null)
1259 interval = interval.Copy (mtext, start, end,
1260 front_grafted, rear_grafted);
1262 interval = new MInterval (Key, mtext, end - start, null);
1265 Right.Left = interval;
1266 interval.Parent = Right;
1267 for (MInterval i = Right; i != null; i = i.Parent)
1268 i.Length += interval.Length;
1272 MInterval next = Right != null ? Next : null;
1274 if (isRearSensitive)
1276 if (next != null && isFrontSensitive)
1277 next.Stack.Clear ();
1278 if (isRearSticky && ! Stack.IsEmpty)
1280 enlarge (end - start);
1281 M17n.DebugPrint (" done by enlarging this\n");
1284 if (next != null && isFrontSticky && ! next.Stack.IsEmpty)
1286 M17n.DebugPrint (" next is {0}\n", next);
1287 next.enlarge (end - start);
1288 M17n.DebugPrint (" done by enlarging next\n");
1291 bool front_grafted = false, rear_grafted = false;
1294 && (grafted = next.graft_backward (interval, start, end)) > 0)
1299 M17n.DebugPrint (" done\n");
1302 rear_grafted = true;
1304 if ((grafted = graft_forward (interval, start, end)) > 0)
1309 M17n.DebugPrint (" done\n");
1312 front_grafted = true;
1314 if (interval != null)
1315 interval = interval.Copy (mtext, start, end,
1318 || (next == null && end < interval.mtext.Length)));
1320 interval = new MInterval (Key, mtext, end - start, null);
1325 // .-this-. ==> .-this-.
1338 interval.Parent = i;
1339 for (; i != null; i = i.Parent)
1340 i.Length += interval.Length;
1343 Right.Insert (pos, interval, start, end);
1344 M17n.DebugPrint (" done\n");
1347 private void vacate_node (MInterval interval)
1349 vacate_node (interval, null);
1352 private void vacate_node (MInterval interval, MInterval stop)
1354 if (interval != null)
1355 M17n.DebugPrint ("vacate #{0} to #{1}\n", ID, interval.ID);
1357 M17n.DebugPrint ("vacate #{0} to null\n", ID);
1358 if (interval != null)
1359 interval.Parent = Parent;
1363 mtext.intervals.Put (Key, interval);
1367 if (this == Parent.Right)
1368 Parent.Right = interval;
1370 Parent.Left = interval;
1373 if (interval != null)
1374 diff -= interval.Length;
1375 for (MInterval i = Parent; i != stop; i = i.Parent)
1380 public void Delete (int start, int end)
1383 M17n.DebugPrint ("delete({0} {1}) from {2}\n", start, end, this);
1385 bool front_checked = false;
1386 bool rear_checked = false;
1392 if (end == From && isFrontSensitive)
1394 Left.Delete (start, end);
1399 Left.Delete (start, From);
1401 end -= From - start;
1403 front_checked = true;
1409 if (start == To && isRearSensitive)
1411 Right.Delete (start, end);
1416 Right.Delete (To, end);
1418 rear_checked = true;
1424 Prev.Stack.Clear ();
1428 && isFrontSensitive)
1429 Next.Stack.Clear ();
1430 if (start == From && end == To)
1442 for (i = Right; i.Left != null; i = i.Left)
1443 i.Length += Left.Length;
1444 i.Length += Left.Length;
1448 vacate_node (Right);
1453 int len = end - start;
1457 for (MInterval i = this; i != null; i = i.Parent)
1462 public void Push (int start, int end, MProperty prop)
1465 M17n.DebugPrint ("push({0} {1}) at {2}\n", start, end, this);
1470 Left.Push (start, end, prop);
1473 Left.Push (start, From, prop);
1480 Right.Push (start, end, prop);
1483 Right.Push (To, end, prop);
1487 if (! Stack.IsEmpty && isSensitive)
1490 divide_left (start);
1493 Stack.Push (prop.key, prop);
1496 /// Combine intervals between HEAD and TAIL (both inclusive) to
1497 /// the common parent of HEAD and TAIL. Callers should assure
1498 /// that the intervals are mergeable in advance.
1499 private static void combine (MInterval head, MInterval tail)
1501 M17n.DebugPrint ("combining {0} through {1}", head, tail);
1503 head.update_from_to ();
1504 tail.update_from_to ();
1505 int from = head.From;
1508 // The nearest common parent of HEAD and TAIL.
1510 for (root = head; root.To + root.RightLength < to;
1511 root = root.Parent);
1513 M17n.DebugPrint (" with common root {0}\n", root);
1515 if (from < root.From)
1517 MInterval prev = root.Prev;
1521 M17n.DebugPrint ("merging {0}\n", prev);
1522 prev.vacate_node (prev.Left, root);
1525 if (prev.Left != null)
1526 prev = prev.Left.RightMost;
1530 root.update_from_to ();
1534 MInterval next = root.Next;
1538 M17n.DebugPrint ("merging {0}\n", next);
1539 next.vacate_node (next.Right, root);
1542 if (next.Right != null)
1543 next = next.Right.LeftMost;
1547 root.update_from_to ();
1551 public void MergeAfterChange (int start, int end)
1555 MInterval head = find_head (start), i = head;
1556 MInterval tail = start < end ? find_tail (end) : head;
1558 if (tail.To < Length)
1561 if (start == head.From && start > 0)
1563 MInterval prev = head.Prev;
1564 if (head.mergeable (prev))
1567 M17n.DebugPrint ("merge between {0} and {1}\n", head, tail);
1570 MInterval next = i.Next;
1572 if (! i.mergeable (next))
1584 public void Pop (int start, int end)
1587 M17n.DebugPrint ("pop({0} {1}) at {2}\n", start, end, this);
1592 Left.Pop (start, end);
1595 Left.Pop (start, From);
1602 Right.Pop (start, end);
1605 Right.Pop (To, end);
1609 if (! Stack.IsEmpty)
1616 divide_left (start);
1624 public void PopSensitive (int start, int end)
1627 MInterval head = find_head (start);
1628 MInterval tail = find_tail (end);
1629 Pop (head.From, tail.To);
1632 public override string ToString ()
1634 string str = String.Format ("#{0}({1} {2} {3} [", ID, Length, From, To);
1636 foreach (MPlist p in Stack)
1640 str += ((MProperty) p.Val).Val;
1644 str += " " + ((MProperty) p.Val).Val;
1646 return (str + "])");
1649 private void DumpOne (bool with_prop, bool newline, bool force)
1651 if (force || M17n.debug)
1653 Console.Write ("#{0}({1} {2} {3}", ID, Length, From, To);
1654 if (with_prop && ! Stack.IsEmpty)
1656 string prepend = " [";
1657 foreach (MPlist p in Stack)
1659 Console.Write (prepend + ((MProperty) p.Val).Val);
1662 Console.Write ("]");
1664 Console.Write (")");
1666 Console.WriteLine ();
1668 throw new Exception ("Invalid interval length");
1672 public void Dump () { Dump (false); }
1674 public void Dump (bool force)
1676 if (force || M17n.debug)
1683 Console.Write (" ");
1684 DumpOne (true, false, force);
1691 get { return (Parent == null ? 0 : Parent.Depth + 1); }
1694 public void DumpNested (bool force)
1696 DumpNested (" " + Key.ToString () + ":", force);
1699 public void DumpNested (string indent, bool force)
1701 if (force || M17n.debug)
1703 int indent_type = (Parent == null ? 1
1704 : Parent.Left == this ? 0 : 2);
1709 if (indent_type <= 1)
1710 Left.DumpNested (indent + " ", force);
1712 Left.DumpNested (indent + "| ", force);
1714 Console.Write (indent);
1715 if (indent_type == 0)
1716 Console.Write (".-");
1717 else if (indent_type == 2)
1718 Console.Write ("`-");
1719 DumpOne (true, true, true);
1722 if (indent_type >= 1)
1723 Right.DumpNested (indent + " ", force);
1725 Right.DumpNested (indent + "| ", force);
1731 private class MTextEnum : IEnumerator
1734 private int pos = -1;
1736 public MTextEnum (MText mt)
1741 public bool MoveNext ()
1744 return (pos < mt.nchars);
1747 public void Reset ()
1752 public object Current
1755 //if (pos < 0 || pos >= mt.nchars)
1756 //throw new InvalidOperationException ();