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);
521 root = (MInterval) p.Val;
523 if (root.isSensitive)
524 root.PopSensitive (from, to);
525 root.Push (from, to, prop);
526 root.MergeAfterChange (from, to);
531 public void PopProp (int from, int to, MSymbol key)
535 if (default_property == null)
537 MPlist p = default_property.Find (key);
544 if (check_range (from, to, true))
547 MPlist p = intervals.Find (key);
551 MInterval root = (MInterval) p.Val;
552 if (root.isSensitive)
553 root.PopSensitive (from, to);
556 root = (MInterval) p.Val;
559 root.MergeAfterChange (from, to);
565 public void DumpProp ()
568 foreach (MPlist p in intervals)
569 ((MInterval) p.Val).Dump (true);
570 Console.WriteLine (")");
573 public void DumpPropNested ()
575 Console.WriteLine ("total length = {0}", Length);
576 foreach (MPlist p in intervals)
577 ((MInterval) p.Val).DumpNested (true);
580 private class MInterval
582 // position: 0 1 2 3 4 5 6 7
583 // | A | B | C | D | E F | G |
584 // interval |---|---|---|<->|-------|---|
585 // |---|<->|---| |<----->|---|
589 // [3 (1 2)] [3 (4 6)]
590 // [1 (0 1)] [2 (2 3)] [1 (6 7)]
592 private static int count = 0;
595 private int From, To;
597 private MPlist Stack;
598 private MInterval Left, Right, Parent;
601 public MInterval (MSymbol key, MText mt, int length)
604 throw new Exception ("Invalid interval length");
608 Stack = new MPlist ();
612 public MInterval (MSymbol key, MText mt)
616 Length = mt.sb.Length;
619 Stack = new MPlist ();
623 /// POS must be smaller than Length;
624 public MProperty Get (int pos)
626 MInterval i = find_head (pos);
628 return (i.Stack.IsEmpty ? null : (MProperty) i.Stack.Val);
631 /// POS must be smaller than Length;
632 public MProperty Get (int pos, out MProperty[] array)
634 MInterval i = find_head (pos);
643 array = new MProperty[i.Stack.Count];
647 for (idx = 0, p = i.Stack; ! p.IsEmpty; idx++, p = p.Next)
648 array[idx] = (MProperty) p.Val;
652 private MInterval (MSymbol key, MText mt, int length, MPlist stack)
659 Stack = stack == null ? new MPlist () : stack.Clone ();
663 private bool isRearSticky
665 get { return MProperty.HasFlags (Key, MProperty.Flags.RearSticky) ; }
668 private bool isFrontSticky
670 get { return MProperty.HasFlags (Key, MProperty.Flags.FrontSticky) ; }
673 public bool isSensitive
675 get { return MProperty.HasFlags (Key, MProperty.Flags.Sensitive) ; }
678 public bool isFrontSensitive
680 get { return MProperty.HasFlags (Key,
681 MProperty.Flags.FrontSensitive) ; }
684 public bool isRearSensitive
686 get { return MProperty.HasFlags (Key,
687 MProperty.Flags.RearSensitive) ; }
690 private void update_from_to ()
695 To = Length - RightLength;
697 else if (Parent.Left == this)
699 From = Parent.From - Length + LeftLength;
700 To = Parent.From - RightLength;
704 From = Parent.To + LeftLength;
705 To = Parent.To + Length - RightLength;
709 private int LeftLength
711 get { return (Left == null ? 0 : Left.Length); }
714 private int RightLength
716 get { return (Right == null ? 0 : Right.Length); }
719 private MInterval LeftMost
725 return Left.LeftMost;
729 private MInterval RightMost
735 return Right.RightMost;
739 private MInterval Prev {
745 for (i = Left; i.Right != null; i = i.Right)
751 MInterval child = this;
752 for (i = Parent; i != null && i.Left == child;
753 child = i, i = i.Parent);
759 private MInterval Next {
765 for (i = Right; i.Left != null; i = i.Left)
771 MInterval child = this;
772 for (i = Parent; i != null && i.Right == child;
773 child = i, i = i.Parent);
779 private MInterval find_head (int pos)
783 return Left.find_head (pos);
785 return Right.find_head (pos);
789 private MInterval find_tail (int pos)
793 return Left.find_tail (pos);
795 return Right.find_tail (pos);
799 private bool mergeable (MInterval i)
803 if (Stack.IsEmpty && i.Stack.IsEmpty)
807 for (p1 = Stack, p2 = i.Stack; ! p1.IsEmpty && ! p2.IsEmpty;
808 p1 = p1.Next, p2 = p2.Next)
809 if (p1.Val != p2.Val)
811 return (p1.IsEmpty && p2.IsEmpty);
814 // p-. or .-p p-. or .-p
815 // .-this-. .-right-.
816 // left .-right-. -> .-this-. c2
818 private MInterval promote_right ()
820 MInterval c1 = Right.Left;
824 mtext.intervals.Put (Key, Right);
825 else if (Parent.Left == this)
828 Parent.Right = Right;
831 Right.Parent = Parent;
833 Right.Length += LeftLength + (To - From);
838 Length = LeftLength + (To - From) + RightLength;
840 // Update C1 if necessary.
844 Parent.update_from_to ();
848 // p-. or .-p p-. or .-p
850 // .-left-. .-right-. -> c1 .-this-.
852 private MInterval promote_left ()
854 MInterval c2 = Left.Right;
858 mtext.intervals.Put (Key, Left);
859 else if (Parent.Left == this)
865 Left.Parent = Parent;
867 Left.Length += (To - From) + RightLength;
872 Length = LeftLength + (To - From) + RightLength;
874 // Update C2 if necessary.
878 Parent.update_from_to ();
882 public MInterval Balance ()
890 // .-left-. .-right-.
892 int diff = i.LeftLength - i.RightLength;
897 new_diff = (i.Length - i.LeftLength
898 + i.Left.RightLength - i.Left.LeftLength);
899 if (Math.Abs (new_diff) >= diff)
901 M17n.DebugPrint ("balancing #{0} by promoting left...", i.ID);
902 i = i.promote_left ();
903 M17n.DebugPrint ("done\n");
908 new_diff = (i.Length - i.RightLength
909 + i.Right.LeftLength - i.Right.RightLength);
910 if (Math.Abs (new_diff) >= diff)
912 M17n.DebugPrint ("balancing #{0} by promoting right\n", i.ID);
913 i = i.promote_right ();
922 public MInterval Copy (MText mt, int start, int end)
924 MInterval copy, left_copy = null, right_copy = null;
931 return Left.Copy (mt, start, end);
932 left_copy = Left.Copy (mt, start, From);
937 return Right.Copy (mt, start, end);
938 right_copy = Right.Copy (mt, To, end);
941 copy = new MInterval (Key, null, end - start, Stack);
943 if (isSensitive && (From < start || end < To))
945 if (left_copy != null)
947 copy.Left = left_copy;
948 left_copy.Parent = copy;
950 if (right_copy != null)
952 copy.Right = right_copy;
953 right_copy.Parent = copy;
958 public MInterval Copy (MText mt, int start, int end,
959 bool first, bool last)
961 MInterval copy = Copy (mt, start, end);
962 MInterval head = find_head (start);
963 MInterval tail = find_tail (end);
965 if (! head.Stack.IsEmpty
966 && (isSensitive && head.From < start
967 || isFrontSensitive && ! first))
969 head = copy.find_head (0);
972 if (! tail.Stack.IsEmpty
973 && (isSensitive && end < head.To
974 || isRearSensitive && ! last))
976 tail = copy.find_tail (copy.Length);
979 M17n.DebugPrint ("Copied: {0}\n", copy);
986 private MInterval divide_right (int pos)
988 MInterval interval = new MInterval (Key, mtext, To - pos, Stack);
990 M17n.DebugPrint ("divide-right({0}) at {1}\n", pos, this);
994 interval.Right = Right;
995 Right.Parent = interval;
996 interval.Length += Right.Length;
998 interval.Parent = this;
1003 // .-this ==> .-this
1006 private MInterval divide_left (int pos)
1008 MInterval interval = new MInterval (Key, mtext, pos - From, Stack);
1010 M17n.DebugPrint ("divide-left({0}) at {1}\n", pos, this);
1014 interval.Left = Left;
1015 Left.Parent = interval;
1016 interval.Length += Left.Length;
1018 interval.Parent = this;
1023 private void set_mtext (MText mt)
1027 Left.set_mtext (mt);
1029 Right.set_mtext (mt);
1032 private void enlarge (int len)
1036 for (MInterval prev = this, i = this.Parent; i != null;
1037 prev = i, i = i.Parent)
1048 private int graft_forward (MInterval interval, int start, int end)
1052 if (! Stack.IsEmpty && isRearSticky)
1054 else if (interval == null)
1055 len = Stack.IsEmpty ? end - start : 0;
1058 MInterval i = interval.find_head (start);
1062 && (isFrontSensitive || (isSensitive && i.From < start)))
1064 M17n.DebugPrint (" forward grafting {0}", i);
1071 while (i != null && i.From < end && mergeable (i))
1073 M17n.DebugPrint (" forward grafting {0}", i);
1074 len += i.To - i.From;
1076 len -= start - i.From;
1086 M17n.DebugPrint (" grafted {0} in {1}\n", len, this);
1092 private int graft_backward (MInterval interval, int start, int end)
1096 if (! Stack.IsEmpty && isFrontSticky)
1098 else if (interval == null)
1099 len = Stack.IsEmpty ? end - start : 0;
1102 MInterval i = interval.find_tail (end);
1106 && (isRearSensitive || (isSensitive && end < i.To)))
1108 M17n.DebugPrint (" backward grafting {0}", i);
1109 if (i.From <= start)
1115 while (i != null && i.To <= start && mergeable (i))
1117 M17n.DebugPrint (" backward grafting {0}", i);
1118 len += i.To - i.From;
1121 if (i.From <= start)
1123 len -= start - i.From;
1130 M17n.DebugPrint (" grafted {0} in {1}\n", len, this);
1136 public void Insert (int pos, MInterval interval, int start, int end)
1140 M17n.DebugPrint ("insert({0} to {1}) at {2} in {3}",
1141 start, end, pos, this);
1144 Left.Insert (pos, interval, start, end);
1145 else if (pos == From)
1147 MInterval prev = Left != null ? Prev : null;
1149 if (isFrontSensitive)
1151 if (prev != null && isRearSensitive)
1152 prev.Stack.Clear ();
1153 if (prev != null && isRearSticky && ! prev.Stack.IsEmpty)
1155 prev.enlarge (end - start);
1156 M17n.DebugPrint (" done\n");
1159 if (isFrontSticky && ! Stack.IsEmpty)
1161 enlarge (end - start);
1162 M17n.DebugPrint (" done\n");
1165 bool front_grafted = false, rear_grafted = false;
1168 && (grafted = prev.graft_forward (interval, start, end)) > 0)
1173 M17n.DebugPrint (" done\n");
1176 front_grafted = true;
1178 if ((grafted = graft_backward (interval, start, end)) > 0)
1183 M17n.DebugPrint (" done\n");
1186 rear_grafted = true;
1189 if (interval != null)
1190 interval = interval.Copy (mtext, start, end,
1192 || (prev == null && start == 0)),
1195 interval = new MInterval (Key, mtext, end - start, null);
1200 // .-this-. ==> .-this-.
1212 interval.Parent = i;
1213 for (; i != null; i = i.Parent)
1214 i.Length += interval.Length;
1218 if (! Stack.IsEmpty)
1222 else if (isFrontSticky || isRearSticky)
1224 enlarge (end - start);
1228 bool front_grafted = false, rear_grafted = false;
1230 if ((grafted = graft_forward (interval, start, end)) > 0)
1235 M17n.DebugPrint (" done\n");
1238 front_grafted = true;
1241 if ((grafted = graft_backward (interval, start, end)) > 0)
1246 M17n.DebugPrint (" done\n");
1249 rear_grafted = true;
1251 if (interval != null)
1252 interval = interval.Copy (mtext, start, end,
1253 front_grafted, rear_grafted);
1255 interval = new MInterval (Key, mtext, end - start, null);
1258 Right.Left = interval;
1259 interval.Parent = Right;
1260 for (MInterval i = Right; i != null; i = i.Parent)
1261 i.Length += interval.Length;
1265 MInterval next = Right != null ? Next : null;
1267 if (isRearSensitive)
1269 if (next != null && isFrontSensitive)
1270 next.Stack.Clear ();
1271 if (isRearSticky && ! Stack.IsEmpty)
1273 enlarge (end - start);
1274 M17n.DebugPrint (" done by enlarging this\n");
1277 if (next != null && isFrontSticky && ! next.Stack.IsEmpty)
1279 M17n.DebugPrint (" next is {0}\n", next);
1280 next.enlarge (end - start);
1281 M17n.DebugPrint (" done by enlarging next\n");
1284 bool front_grafted = false, rear_grafted = false;
1287 && (grafted = next.graft_backward (interval, start, end)) > 0)
1292 M17n.DebugPrint (" done\n");
1295 rear_grafted = true;
1297 if ((grafted = graft_forward (interval, start, end)) > 0)
1302 M17n.DebugPrint (" done\n");
1305 front_grafted = true;
1307 if (interval != null)
1308 interval = interval.Copy (mtext, start, end,
1311 || (next == null && end < interval.mtext.Length)));
1313 interval = new MInterval (Key, mtext, end - start, null);
1318 // .-this-. ==> .-this-.
1331 interval.Parent = i;
1332 for (; i != null; i = i.Parent)
1333 i.Length += interval.Length;
1336 Right.Insert (pos, interval, start, end);
1337 M17n.DebugPrint (" done\n");
1340 private void vacate_node (MInterval interval)
1342 vacate_node (interval, null);
1345 private void vacate_node (MInterval interval, MInterval stop)
1347 if (interval != null)
1348 M17n.DebugPrint ("vacate #{0} to #{1}\n", ID, interval.ID);
1350 M17n.DebugPrint ("vacate #{0} to null\n", ID);
1351 if (interval != null)
1352 interval.Parent = Parent;
1356 mtext.intervals.Put (Key, interval);
1360 if (this == Parent.Right)
1361 Parent.Right = interval;
1363 Parent.Left = interval;
1366 if (interval != null)
1367 diff -= interval.Length;
1368 for (MInterval i = Parent; i != stop; i = i.Parent)
1373 public void Delete (int start, int end)
1376 M17n.DebugPrint ("delete({0} {1}) from {2}\n", start, end, this);
1378 bool front_checked = false;
1379 bool rear_checked = false;
1385 if (end == From && isFrontSensitive)
1387 Left.Delete (start, end);
1392 Left.Delete (start, From);
1394 end -= From - start;
1396 front_checked = true;
1402 if (start == To && isRearSensitive)
1404 Right.Delete (start, end);
1409 Right.Delete (To, end);
1411 rear_checked = true;
1417 Prev.Stack.Clear ();
1421 && isFrontSensitive)
1422 Next.Stack.Clear ();
1423 if (start == From && end == To)
1435 for (i = Right; i.Left != null; i = i.Left)
1436 i.Length += Left.Length;
1437 i.Length += Left.Length;
1441 vacate_node (Right);
1446 int len = end - start;
1450 for (MInterval i = this; i != null; i = i.Parent)
1455 public void Push (int start, int end, MProperty prop)
1458 M17n.DebugPrint ("push({0} {1}) at {2}\n", start, end, this);
1463 Left.Push (start, end, prop);
1466 Left.Push (start, From, prop);
1473 Right.Push (start, end, prop);
1476 Right.Push (To, end, prop);
1480 if (! Stack.IsEmpty && isSensitive)
1483 divide_left (start);
1486 Stack.Push (prop.key, prop);
1489 /// Combine intervals between HEAD and TAIL (both inclusive) to
1490 /// the common parent of HEAD and TAIL. Callers should assure
1491 /// that the intervals are mergeable in advance.
1492 private static void combine (MInterval head, MInterval tail)
1494 M17n.DebugPrint ("combining {0} through {1}", head, tail);
1496 head.update_from_to ();
1497 tail.update_from_to ();
1498 int from = head.From;
1501 // The nearest common parent of HEAD and TAIL.
1503 for (root = head; root.To + root.RightLength < to;
1504 root = root.Parent);
1506 M17n.DebugPrint (" with common root {0}\n", root);
1508 if (from < root.From)
1510 MInterval prev = root.Prev;
1514 M17n.DebugPrint ("merging {0}\n", prev);
1515 prev.vacate_node (prev.Left, root);
1518 if (prev.Left != null)
1519 prev = prev.Left.RightMost;
1523 root.update_from_to ();
1527 MInterval next = root.Next;
1531 M17n.DebugPrint ("merging {0}\n", next);
1532 next.vacate_node (next.Right, root);
1535 if (next.Right != null)
1536 next = next.Right.LeftMost;
1540 root.update_from_to ();
1544 public void MergeAfterChange (int start, int end)
1548 MInterval head = find_head (start), i = head;
1549 MInterval tail = start < end ? find_tail (end) : head;
1551 if (tail.To < Length)
1554 if (start == head.From && start > 0)
1556 MInterval prev = head.Prev;
1557 if (head.mergeable (prev))
1560 M17n.DebugPrint ("merge between {0} and {1}\n", head, tail);
1563 MInterval next = i.Next;
1565 if (! i.mergeable (next))
1577 public void Pop (int start, int end)
1580 M17n.DebugPrint ("pop({0} {1}) at {2}\n", start, end, this);
1585 Left.Pop (start, end);
1588 Left.Pop (start, From);
1595 Right.Pop (start, end);
1598 Right.Pop (To, end);
1602 if (! Stack.IsEmpty)
1609 divide_left (start);
1617 public void PopSensitive (int start, int end)
1620 MInterval head = find_head (start);
1621 MInterval tail = find_tail (end);
1622 while (! head.Stack.IsEmpty && head.From > 0)
1624 MInterval prev = head.Prev;
1626 if (prev.Stack.IsEmpty || head.Stack.Val != prev.Stack.Val)
1630 while (! tail.Stack.IsEmpty && tail.To < mtext.Length)
1632 MInterval next = tail.Next;
1634 if (next.Stack.IsEmpty || tail.Stack.Val != next.Stack.Val)
1638 Pop (head.From, tail.To);
1641 public override string ToString ()
1643 string str = String.Format ("#{0}({1} {2} {3} [", ID, Length, From, To);
1645 foreach (MPlist p in Stack)
1649 str += ((MProperty) p.Val).Val;
1653 str += " " + ((MProperty) p.Val).Val;
1655 return (str + "])");
1658 private void DumpOne (bool with_prop, bool newline, bool force)
1660 if (force || M17n.debug)
1662 Console.Write ("#{0}({1} {2} {3}", ID, Length, From, To);
1663 if (with_prop && ! Stack.IsEmpty)
1665 string prepend = " [";
1666 foreach (MPlist p in Stack)
1668 Console.Write (prepend + ((MProperty) p.Val).Val);
1671 Console.Write ("]");
1673 Console.Write (")");
1675 Console.WriteLine ();
1677 throw new Exception ("Invalid interval length");
1681 public void Dump () { Dump (false); }
1683 public void Dump (bool force)
1685 if (force || M17n.debug)
1692 Console.Write (" ");
1693 DumpOne (true, false, force);
1700 get { return (Parent == null ? 0 : Parent.Depth + 1); }
1703 public void DumpNested (bool force)
1705 DumpNested (" " + Key.ToString () + ":", force);
1708 public void DumpNested (string indent, bool force)
1710 if (force || M17n.debug)
1712 int indent_type = (Parent == null ? 1
1713 : Parent.Left == this ? 0 : 2);
1718 if (indent_type <= 1)
1719 Left.DumpNested (indent + " ", force);
1721 Left.DumpNested (indent + "| ", force);
1723 Console.Write (indent);
1724 if (indent_type == 0)
1725 Console.Write (".-");
1726 else if (indent_type == 2)
1727 Console.Write ("`-");
1728 DumpOne (true, true, true);
1731 if (indent_type >= 1)
1732 Right.DumpNested (indent + " ", force);
1734 Right.DumpNested (indent + "| ", force);
1740 private class MTextEnum : IEnumerator
1743 private int pos = -1;
1745 public MTextEnum (MText mt)
1750 public bool MoveNext ()
1753 return (pos < mt.nchars);
1756 public void Reset ()
1761 public object Current
1764 //if (pos < 0 || pos >= mt.nchars)
1765 //throw new InvalidOperationException ();