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 M17n.DebugPrint ("Copying: {0}", copy);
974 if (! head.Stack.IsEmpty
975 && (isSensitive && head.From < start
976 || (isFrontSensitive && ! first)))
978 M17n.DebugPrint (" clear head");
979 head = copy.find_head (0);
982 if (! tail.Stack.IsEmpty
983 && (isSensitive && end < tail.To
984 || (isRearSensitive && ! last)))
986 M17n.DebugPrint (" clear tail");
987 tail = copy.find_tail (copy.Length);
990 M17n.DebugPrint ("\n");
997 private MInterval divide_right (int pos)
999 MInterval interval = new MInterval (Key, mtext, To - pos, Stack);
1001 M17n.DebugPrint ("divide-right({0}) at {1}\n", pos, this);
1005 interval.Right = Right;
1006 Right.Parent = interval;
1007 interval.Length += Right.Length;
1009 interval.Parent = this;
1014 // .-this ==> .-this
1017 private MInterval divide_left (int pos)
1019 MInterval interval = new MInterval (Key, mtext, pos - From, Stack);
1021 M17n.DebugPrint ("divide-left({0}) at {1}\n", pos, this);
1025 interval.Left = Left;
1026 Left.Parent = interval;
1027 interval.Length += Left.Length;
1029 interval.Parent = this;
1034 private void set_mtext (MText mt)
1038 Left.set_mtext (mt);
1040 Right.set_mtext (mt);
1043 private void enlarge (int len)
1047 for (MInterval prev = this, i = this.Parent; i != null;
1048 prev = i, i = i.Parent)
1059 private int graft_forward (MInterval interval, int start, int end)
1063 if (! Stack.IsEmpty && isRearSticky)
1065 else if (interval == null)
1066 len = Stack.IsEmpty ? end - start : 0;
1069 MInterval i = interval.find_head (start);
1073 && (isFrontSensitive || (isSensitive && i.From < start)))
1075 M17n.DebugPrint (" forward grafting {0}", i);
1082 while (i != null && i.From < end && mergeable (i))
1084 M17n.DebugPrint (" forward grafting {0}", i);
1085 len += i.To - i.From;
1087 len -= start - i.From;
1097 M17n.DebugPrint (" grafted {0} in {1}\n", len, this);
1103 private int graft_backward (MInterval interval, int start, int end)
1107 if (! Stack.IsEmpty && isFrontSticky)
1109 else if (interval == null)
1110 len = Stack.IsEmpty ? end - start : 0;
1113 MInterval i = interval.find_tail (end);
1117 && (isRearSensitive || (isSensitive && end < i.To)))
1119 M17n.DebugPrint (" backward grafting {0}", i);
1120 if (i.From <= start)
1126 while (i != null && i.To <= start && mergeable (i))
1128 M17n.DebugPrint (" backward grafting {0}", i);
1129 len += i.To - i.From;
1132 if (i.From <= start)
1134 len -= start - i.From;
1141 M17n.DebugPrint (" grafted {0} in {1}\n", len, this);
1147 public void Insert (int pos, MInterval interval, int start, int end)
1151 M17n.DebugPrint ("insert({0} to {1}) at {2} in {3}",
1152 start, end, pos, this);
1155 Left.Insert (pos, interval, start, end);
1156 else if (pos == From)
1158 MInterval prev = Left != null ? Prev : null;
1160 if (isFrontSensitive)
1162 if (prev != null && isRearSensitive)
1163 prev.Stack.Clear ();
1164 if (prev != null && isRearSticky && ! prev.Stack.IsEmpty)
1166 prev.enlarge (end - start);
1167 M17n.DebugPrint (" done\n");
1170 if (isFrontSticky && ! Stack.IsEmpty)
1172 enlarge (end - start);
1173 M17n.DebugPrint (" done\n");
1176 bool front_grafted = false, rear_grafted = false;
1179 && (grafted = prev.graft_forward (interval, start, end)) > 0)
1184 M17n.DebugPrint (" done\n");
1187 front_grafted = true;
1189 if ((grafted = graft_backward (interval, start, end)) > 0)
1194 M17n.DebugPrint (" done\n");
1197 rear_grafted = true;
1200 if (interval != null)
1201 interval = interval.Copy (mtext, start, end,
1203 || (prev == null && start == 0)),
1206 interval = new MInterval (Key, mtext, end - start, null);
1211 // .-this-. ==> .-this-.
1223 interval.Parent = i;
1224 for (; i != null; i = i.Parent)
1225 i.Length += interval.Length;
1229 if (! Stack.IsEmpty)
1233 else if (isFrontSticky || isRearSticky)
1235 enlarge (end - start);
1239 bool front_grafted = false, rear_grafted = false;
1241 if ((grafted = graft_forward (interval, start, end)) > 0)
1246 M17n.DebugPrint (" done\n");
1249 front_grafted = true;
1252 if ((grafted = graft_backward (interval, start, end)) > 0)
1257 M17n.DebugPrint (" done\n");
1260 rear_grafted = true;
1262 if (interval != null)
1263 interval = interval.Copy (mtext, start, end,
1264 front_grafted, rear_grafted);
1266 interval = new MInterval (Key, mtext, end - start, null);
1269 Right.Left = interval;
1270 interval.Parent = Right;
1271 for (MInterval i = Right; i != null; i = i.Parent)
1272 i.Length += interval.Length;
1276 MInterval next = Right != null ? Next : null;
1278 if (isRearSensitive)
1280 if (next != null && isFrontSensitive)
1281 next.Stack.Clear ();
1282 if (isRearSticky && ! Stack.IsEmpty)
1284 enlarge (end - start);
1285 M17n.DebugPrint (" done by enlarging this\n");
1288 if (next != null && isFrontSticky && ! next.Stack.IsEmpty)
1290 M17n.DebugPrint (" next is {0}\n", next);
1291 next.enlarge (end - start);
1292 M17n.DebugPrint (" done by enlarging next\n");
1295 bool front_grafted = false, rear_grafted = false;
1298 && (grafted = next.graft_backward (interval, start, end)) > 0)
1303 M17n.DebugPrint (" done\n");
1306 rear_grafted = true;
1308 if ((grafted = graft_forward (interval, start, end)) > 0)
1313 M17n.DebugPrint (" done\n");
1316 front_grafted = true;
1318 if (interval != null)
1319 interval = interval.Copy (mtext, start, end,
1322 || (next == null && end == interval.mtext.Length)));
1324 interval = new MInterval (Key, mtext, end - start, null);
1329 // .-this-. ==> .-this-.
1342 interval.Parent = i;
1343 for (; i != null; i = i.Parent)
1344 i.Length += interval.Length;
1347 Right.Insert (pos, interval, start, end);
1348 M17n.DebugPrint (" done\n");
1351 private void vacate_node (MInterval interval)
1353 vacate_node (interval, null);
1356 private void vacate_node (MInterval interval, MInterval stop)
1358 if (interval != null)
1359 M17n.DebugPrint ("vacate #{0} to #{1}\n", ID, interval.ID);
1361 M17n.DebugPrint ("vacate #{0} to null\n", ID);
1362 if (interval != null)
1363 interval.Parent = Parent;
1367 mtext.intervals.Put (Key, interval);
1371 if (this == Parent.Right)
1372 Parent.Right = interval;
1374 Parent.Left = interval;
1377 if (interval != null)
1378 diff -= interval.Length;
1379 for (MInterval i = Parent; i != stop; i = i.Parent)
1384 public void Delete (int start, int end)
1387 M17n.DebugPrint ("delete({0} {1}) from {2}\n", start, end, this);
1389 bool front_checked = false;
1390 bool rear_checked = false;
1396 if (end == From && isFrontSensitive)
1398 Left.Delete (start, end);
1403 Left.Delete (start, From);
1405 end -= From - start;
1407 front_checked = true;
1413 if (start == To && isRearSensitive)
1415 Right.Delete (start, end);
1420 Right.Delete (To, end);
1422 rear_checked = true;
1428 Prev.Stack.Clear ();
1432 && isFrontSensitive)
1433 Next.Stack.Clear ();
1434 if (start == From && end == To)
1446 for (i = Right; i.Left != null; i = i.Left)
1447 i.Length += Left.Length;
1448 i.Length += Left.Length;
1452 vacate_node (Right);
1457 int len = end - start;
1461 for (MInterval i = this; i != null; i = i.Parent)
1466 public void Push (int start, int end, MProperty prop)
1469 M17n.DebugPrint ("push({0} {1}) at {2}\n", start, end, this);
1474 Left.Push (start, end, prop);
1477 Left.Push (start, From, prop);
1484 Right.Push (start, end, prop);
1487 Right.Push (To, end, prop);
1491 if (! Stack.IsEmpty && isSensitive)
1494 divide_left (start);
1497 Stack.Push (prop.key, prop);
1500 /// Combine intervals between HEAD and TAIL (both inclusive) to
1501 /// the common parent of HEAD and TAIL. Callers should assure
1502 /// that the intervals are mergeable in advance.
1503 private static void combine (MInterval head, MInterval tail)
1505 M17n.DebugPrint ("combining {0} through {1}", head, tail);
1507 head.update_from_to ();
1508 tail.update_from_to ();
1509 int from = head.From;
1512 // The nearest common parent of HEAD and TAIL.
1514 for (root = head; root.To + root.RightLength < to;
1515 root = root.Parent);
1517 M17n.DebugPrint (" with common root {0}\n", root);
1519 if (from < root.From)
1521 MInterval prev = root.Prev;
1525 M17n.DebugPrint ("merging {0}\n", prev);
1526 prev.vacate_node (prev.Left, root);
1529 if (prev.Left != null)
1530 prev = prev.Left.RightMost;
1534 root.update_from_to ();
1538 MInterval next = root.Next;
1542 M17n.DebugPrint ("merging {0}\n", next);
1543 next.vacate_node (next.Right, root);
1546 if (next.Right != null)
1547 next = next.Right.LeftMost;
1551 root.update_from_to ();
1555 public void MergeAfterChange (int start, int end)
1559 MInterval head = find_head (start), i = head;
1560 MInterval tail = start < end ? find_tail (end) : head;
1562 if (tail.To < Length)
1565 if (start == head.From && start > 0)
1567 MInterval prev = head.Prev;
1568 if (head.mergeable (prev))
1571 M17n.DebugPrint ("merge between {0} and {1}\n", head, tail);
1574 MInterval next = i.Next;
1576 if (! i.mergeable (next))
1588 public void Pop (int start, int end)
1591 M17n.DebugPrint ("pop({0} {1}) at {2}\n", start, end, this);
1596 Left.Pop (start, end);
1599 Left.Pop (start, From);
1606 Right.Pop (start, end);
1609 Right.Pop (To, end);
1613 if (! Stack.IsEmpty)
1620 divide_left (start);
1628 public void PopSensitive (int start, int end)
1631 MInterval head = find_head (start);
1632 MInterval tail = find_tail (end);
1633 Pop (head.From, tail.To);
1636 public override string ToString ()
1638 string str = String.Format ("#{0}({1} {2} {3} [", ID, Length, From, To);
1640 foreach (MPlist p in Stack)
1644 str += ((MProperty) p.Val).Val;
1648 str += " " + ((MProperty) p.Val).Val;
1650 return (str + "])");
1653 private void DumpOne (bool with_prop, bool newline, bool force)
1655 if (force || M17n.debug)
1657 Console.Write ("#{0}({1} {2} {3}", ID, Length, From, To);
1658 if (with_prop && ! Stack.IsEmpty)
1660 string prepend = " [";
1661 foreach (MPlist p in Stack)
1663 Console.Write (prepend + ((MProperty) p.Val).Val);
1666 Console.Write ("]");
1668 Console.Write (")");
1670 Console.WriteLine ();
1672 throw new Exception ("Invalid interval length");
1676 public void Dump () { Dump (false); }
1678 public void Dump (bool force)
1680 if (force || M17n.debug)
1687 Console.Write (" ");
1688 DumpOne (true, false, force);
1695 get { return (Parent == null ? 0 : Parent.Depth + 1); }
1698 public void DumpNested (bool force)
1700 DumpNested (" " + Key.ToString () + ":", force);
1703 public void DumpNested (string indent, bool force)
1705 if (force || M17n.debug)
1707 int indent_type = (Parent == null ? 1
1708 : Parent.Left == this ? 0 : 2);
1713 if (indent_type <= 1)
1714 Left.DumpNested (indent + " ", force);
1716 Left.DumpNested (indent + "| ", force);
1718 Console.Write (indent);
1719 if (indent_type == 0)
1720 Console.Write (".-");
1721 else if (indent_type == 2)
1722 Console.Write ("`-");
1723 DumpOne (true, true, true);
1726 if (indent_type >= 1)
1727 Right.DumpNested (indent + " ", force);
1729 Right.DumpNested (indent + "| ", force);
1735 private class MTextEnum : IEnumerator
1738 private int pos = -1;
1740 public MTextEnum (MText mt)
1745 public bool MoveNext ()
1748 return (pos < mt.nchars);
1751 public void Reset ()
1756 public object Current
1759 //if (pos < 0 || pos >= mt.nchars)
1760 //throw new InvalidOperationException ();