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 MTextProperty
28 internal enum Flag : byte
37 public MSymbol Key { get { return key; } }
38 public object Val { get { return val; } }
39 public bool FrontSticky
41 get { return (flags & Flag.FrontSticky) != Flag.None; }
43 public bool RearSticky
45 get { return (flags & Flag.RearSticky) != Flag.None; }
49 get { return (flags & Flag.Sensitive) != Flag.None; }
52 public MTextProperty (MSymbol key, object val)
56 flags |= Flag.RearSticky;
59 public MTextProperty (MSymbol key, object val,
60 bool front_sticky, bool rear_sticky, bool sensitive)
65 flags |= Flag.FrontSticky;
67 flags |= Flag.RearSticky;
69 flags |= Flag.Sensitive;
72 public override string ToString ()
74 return key.ToString () + ":" + val;
78 public class MText : IEnumerable, IEquatable<MText>, IComparable<MText>
81 public enum MTextFormat format;
83 private StringBuilder sb;
85 private int cache_pos;
86 private int cache_idx;
87 private MPlist intervals;
88 private MPlist default_property;
89 private bool read_only;
91 private static UTF8Encoding utf8 = new UTF8Encoding ();
93 private static int count_chars (String str)
95 int len = str.Length, n = 0;
97 for (int i = 0; i < len; i++, n++)
98 if (surrogate_high_p (str[i]))
103 private static int count_chars (StringBuilder str)
105 int len = str.Length, n = 0;
107 for (int i = 0; i < len; i++, n++)
108 if (surrogate_high_p (str[i]))
115 sb = new StringBuilder ();
116 intervals = new MPlist ();
119 public MText (byte[] str)
121 sb = new StringBuilder (utf8.GetString (str));
122 nchars = count_chars (sb);
123 intervals = new MPlist ();
126 public MText (String str)
128 sb = new StringBuilder (str);
129 nchars = count_chars (str);
130 intervals = new MPlist ();
133 public MText (StringBuilder str)
136 nchars = count_chars (str);
137 intervals = new MPlist ();
140 public static MText operator+ (MText mt1, MText mt2)
142 MText mt = new MText ();
144 mt.sb.Append (mt1.sb);
145 mt.sb.Append (mt2.sb);
146 mt.nchars = mt1.nchars + mt2.nchars;
151 public bool ReadOnly { get { return read_only; } }
152 public int Length { get { return nchars; } }
156 // for IEnumerable interface
157 public IEnumerator GetEnumerator() { return new MTextEnum (this); }
159 // for IEquatable interface
160 public bool Equals (MText other) { return this.sb.Equals (other.sb); }
162 // for IComparable interface
163 public int CompareTo (MText other)
165 return this.sb.ToString ().CompareTo (other.sb.ToString ());
168 public override String ToString () { return "\"" + sb.ToString () + "\""; }
170 private static bool surrogate_high_p (char c)
172 return (c >= 0xD800 && c < 0xDC00);
175 private static bool surrogate_low_p (char c)
177 return (c >= 0xDC00 && c < 0xE000);
180 private static int inc_idx (StringBuilder sb, int i)
182 return (i + (surrogate_high_p (sb[i]) ? 2 : 1));
185 private static int dec_idx (StringBuilder sb, int i)
187 return (i - (surrogate_low_p (sb[i - 1]) ? 2 : 1));
190 private static int pos_to_idx (MText mt, int pos)
192 if (pos == mt.cache_pos)
198 if (pos < mt.cache_pos)
200 if (mt.cache_pos == mt.cache_idx)
202 if (pos < mt.cache_pos - pos)
209 p = mt.cache_pos; i = mt.cache_idx;
215 if (mt.nchars - mt.cache_pos == mt.sb.Length - mt.cache_idx)
216 return (mt.cache_idx + pos - mt.cache_pos);
217 if (pos - mt.cache_pos < mt.nchars - pos)
219 p = mt.cache_pos; i = mt.cache_idx;
224 p = mt.nchars; i = mt.sb.Length;
229 for (; p < pos; i = inc_idx (mt.sb, i), p++);
231 for (; p > pos; i = dec_idx (mt.sb, i), p--);
237 private void check_pos (int pos, bool tail_ok)
239 if (pos < 0 || (tail_ok ? pos > nchars : pos >= nchars))
240 throw new Exception ("Invalid MText position:" + pos);
243 private bool check_range (int from, int to, bool zero_ok)
245 if (from < 0 || (zero_ok ? from > to : from >= to)
247 throw new Exception ("Invalid MText range");
251 private void insert (int pos, MText mt2, int from, int to)
253 check_pos (pos, true);
255 int pos_idx = pos_to_idx (this, pos);
256 int from_idx = pos_to_idx (mt2, from);
257 int to_idx = pos_to_idx (mt2, to);
259 sb.Insert (pos_idx, mt2.sb.ToString (from_idx, to_idx - from_idx));
262 foreach (MPlist plist in mt2.intervals)
263 if (intervals.Find (plist.Key) == null)
264 intervals.Push (plist.Key, new MInterval (plist.Key, this));
265 foreach (MPlist plist in intervals)
267 MPlist p = mt2.intervals.Find (plist.Key);
271 interval = new MInterval (plist.Key, this, to - from);
273 interval = ((MInterval) p.Val).Copy (from, to);
274 ((MInterval) plist.Val).Insert (pos, interval);
278 public int this[int i]
281 i = pos_to_idx (this, i);
284 if (surrogate_high_p (sb[i]))
286 sb[i] = (char) value;
290 char high = (char) (0xD800 + ((value - 0x10000) >> 10));
291 char low = (char) (0xDC00 + ((value - 0x10000) & 0x3FF));
293 if (! surrogate_high_p (sb[i]))
300 i = pos_to_idx (this, i);
301 return (surrogate_high_p (sb[i])
302 ? ((sb[i] - 0xD800) << 10) + (sb[i + 1] - 0xDC00) + 0x10000
309 return (new MText (sb.ToString ()));
312 public MText Ins (int pos, MText mt)
314 insert (pos, mt, 0, mt.nchars);
318 public MText Ins (int pos, MText mt, int from, int to)
320 insert (pos, mt, from, to);
324 public MText Del (int from, int to)
326 if (check_range (from, to, true))
329 sb.Remove (from, pos_to_idx (this, to) - pos_to_idx (this, from));
333 foreach (MPlist plist in intervals)
334 ((MInterval) plist.Val).Delete (from, to);
336 intervals = new MPlist ();
340 public object GetProp (int pos, MSymbol key)
342 check_pos (pos, false);
344 MInterval i = (MInterval) intervals.Find (key).Val;
349 MTextProperty prop = i.Get (pos);
350 return (prop != null ? prop.Val : null);
353 public object GetProp (int pos, MSymbol key, out MTextProperty prop)
355 check_pos (pos, false);
357 MInterval i = (MInterval) intervals.Find (key).Val;
360 return (prop = null);
362 return (prop != null ? prop.Val : null);
365 public object GetProp (int pos, MSymbol key, out MTextProperty[] array)
367 check_pos (pos, false);
369 MInterval i = (MInterval) intervals.Find (key).Val;
372 return (array = null);
373 MTextProperty prop = i.Get (pos, out array);
374 return (prop != null ? prop.Val : null);
377 public void PushProp (int from, int to, MSymbol key, object val)
379 if (! check_range (from, to, true))
380 PushProp (from, to, new MTextProperty (key, val));
383 public void PushProp (int from, int to, MTextProperty prop)
387 if (default_property == null)
388 default_property = new MPlist ();
389 default_property.Push (prop.key, prop.val);
393 if (check_range (from, to, true))
396 MPlist p = intervals.Find (prop.key);
401 root = new MInterval (prop.key, this);
402 intervals.Push (prop.key, root);
405 root = (MInterval) p.Val;
407 root.Push (from, to, prop);
411 public void PopProp (int from, int to, MSymbol key)
415 if (default_property == null)
417 MPlist p = default_property.Find (key);
424 if (check_range (from, to, true))
427 MPlist p = intervals.Find (key);
430 ((MInterval) p.Val).Pop (from, to);
434 public void DumpProp ()
437 foreach (MPlist p in intervals)
438 ((MInterval) p.Val).Dump (true);
439 Console.WriteLine (")");
442 private class MInterval
444 // position: 0 1 2 3 4 5 6 7
445 // | A | B | C | D | E F | G |
446 // interval |---|---|---|<->|-------|---|
447 // |---|<->|---| |<----->|---|
451 // [3 (1 2)] [3 (4 6)]
452 // [1 (0 1)] [2 (2 3)] [1 (6 7)]
454 private static int count = 0;
457 private int From, To;
459 private MPlist Stack;
460 private MInterval Left, Right, Parent;
463 public MInterval (MSymbol key, MText mt, int length)
466 throw new Exception ("Invalid interval length");
470 Stack = new MPlist ();
474 public MInterval (MSymbol key, MText mt)
478 Length = mt.sb.Length;
481 Stack = new MPlist ();
485 public MTextProperty Get (int pos)
487 MInterval i = find (pos);
489 return (i.Stack.IsEmpty ? null : (MTextProperty) i.Stack.Val);
492 public MTextProperty Get (int pos, out MTextProperty[] array)
494 MInterval i = find (pos);
501 array = new MTextProperty[i.Stack.Count];
505 for (idx = 0, p = i.Stack; ! p.IsEmpty; idx++, p = p.Next)
506 array[idx] = (MTextProperty) p.Val;
507 return array[idx - 1];
510 private MInterval (MSymbol key, MText mt, int length, MPlist stack)
517 Stack = stack.Clone ();
521 private void update_from_to ()
526 To = Length - RightLength;
528 else if (Parent.Left == this)
530 From = Parent.From - Length + LeftLength;
531 To = Parent.From - RightLength;
535 From = Parent.To + LeftLength;
536 To = Parent.To + Length - RightLength;
540 private int LeftLength
542 get { return (Left == null ? 0 : Left.Length); }
545 private int RightLength
547 get { return (Right == null ? 0 : Right.Length); }
550 private MInterval LeftMost
552 get { return (Left == null ? this : Left.LeftMost); }
555 private MInterval RightMost
557 get { return (Right == null ? this : Right.RightMost); }
560 private MInterval Prev {
565 for (i = Left; i.Right != null; i = i.Right);
568 MInterval child = this;
569 for (i = Parent; i != null && i.Left == child;
570 child = i, i = i.Parent);
576 private MInterval Next {
581 for (i = Right; i.Left != null; i = i.Left);
584 MInterval child = this;
585 for (i = Parent; i != null && i.Right == child;
586 child = i, i = i.Parent);
592 private MInterval find (int pos)
596 return Left.find (pos);
598 return Right.find (pos);
602 private bool mergeable (MInterval i)
606 for (p1 = Stack, p2 = i.Stack; ! p1.IsEmpty && ! p2.IsEmpty;
607 p1 = p1.Next, p2 = p2.Next)
608 if (p1.Val != p2.Val)
610 return (p1.IsEmpty && p2.IsEmpty);
613 // p-. or .-p p-. or .-p
614 // .-this-. .-right-.
615 // left .-right-. -> .-this-. c2
617 private MInterval promote_right ()
619 int right_length = Right.Length;
623 mtext.intervals.Put (Key, Right);
624 else if (Parent.Left == this)
627 Parent.Right = Right;
628 Right.Parent = Parent;
634 Parent.Length += Length;
635 Length -= right_length;
639 Parent.Length -= c1.Length;
645 // p-. or .-p p-. or .-p
647 // .-left-. .-right-. -> c1 .-this-.
649 private MInterval promote_left ()
651 int left_length = Left.Length;
655 mtext.intervals.Put (Key, Left);
656 else if (Parent.Left == this)
660 Left.Parent = Parent;
666 Parent.Length += Length;
667 Length -= left_length;
671 Parent.Length -= c1.Length;
677 private MInterval balance ()
684 // .-left-. .-right-.
686 int diff = i.LeftLength - i.RightLength;
691 new_diff = (i.Length - i.LeftLength
692 + i.Left.RightLength - i.Left.LeftLength);
693 if (Math.Abs (new_diff) >= diff)
695 i = i.promote_left ();
700 new_diff = (i.Length - i.RightLength
701 + i.Right.LeftLength - i.Right.RightLength);
702 if (Math.Abs (new_diff) >= diff)
704 i = i.promote_right ();
711 public MInterval Copy (int start, int end)
713 MInterval copy, left_copy = null, right_copy = null;
719 return Left.Copy (start, end);
720 left_copy = Left.Copy (start, From);
725 return Right.Copy (start, end);
726 right_copy = Right.Copy (To, end);
729 copy = new MInterval (Key, null, end - start, Stack);
730 remove_properties (MTextProperty.Flag.Sensitive);
731 copy.Left = left_copy;
732 copy.Right = right_copy;
740 private MInterval divide_right (int pos)
742 MInterval interval = new MInterval (Key, mtext, To - pos, Stack);
744 M17N.DebugPrint ("divide-right({0}) at ", pos); DumpOne (false, true);
748 interval.Right = Right;
749 Right.Parent = interval;
750 interval.Length += Right.Length;
752 interval.Parent = this;
760 private MInterval divide_left (int pos)
762 MInterval interval = new MInterval (Key, mtext, pos - From, Stack);
764 M17N.DebugPrint ("divide-left({0}) at ", pos); DumpOne (false, true);
768 interval.Left = Left;
769 Left.Parent = interval;
770 interval.Length += Left.Length;
772 interval.Parent = this;
777 private void remove_properties (MTextProperty.Flag flags)
779 for (MPlist p = Stack; ! p.IsEmpty;)
781 MTextProperty prop = (MTextProperty) p.Val;
783 if ((prop.flags & flags) == flags)
790 private void inherit_front_properties (MPlist plist)
792 for (MInterval i = LeftMost; i != null; i = i.Next)
796 for (MPlist p = plist; ! p.IsEmpty; p = p.Next)
798 MTextProperty prop = (MTextProperty) p.Val;
800 if ((prop.flags & MTextProperty.Flag.RearSticky)
801 == MTextProperty.Flag.RearSticky)
802 i.Stack.Add (prop.key, prop);
807 private void inherit_rear_properties (MPlist plist)
809 for (MInterval i = RightMost; i != null; i = i.Prev)
813 for (MPlist p = plist; ! p.IsEmpty; p = p.Next)
815 MTextProperty prop = (MTextProperty) p.Val;
817 if ((prop.flags & MTextProperty.Flag.FrontSticky)
818 == MTextProperty.Flag.FrontSticky)
819 i.Stack.Add (prop.key, prop);
824 private MInterval delete_node_forward ()
828 int len = Length - RightLength;
830 for (MInterval i = Parent; i != null; i = i.Parent)
832 if (Parent.Left == this)
835 Parent.Right = Right;
840 Right.Parent = Parent;
841 return Right.LeftMost;
846 private MInterval delete_node_backward ()
850 int len = Length - RightLength;
852 for (MInterval i = Parent; i != null; i = i.Parent)
854 if (Parent.Left == this)
862 Left.Parent = Parent;
863 return Left.RightMost;
868 private void set_mtext (MText mt)
874 Right.set_mtext (mt);
877 private MInterval graft (MInterval interval, bool forward, out int len)
884 i = interval.LeftMost;
889 len += i.Length - i.RightLength;
890 i = i.delete_node_forward ();
895 i = interval.RightMost;
900 len += i.Length - i.LeftLength;
901 i = i.delete_node_backward ();
907 for (MInterval prev = this, ii = this.Parent; ii != null;
908 prev = ii, ii = ii.Parent)
918 while (i.Parent != null) i = i.Parent;
922 public void Insert (int pos, MInterval interval)
925 M17N.DebugPrint ("insert({0}) at {1} in ", interval.Length, pos);
926 DumpOne (false, true);
928 interval.set_mtext (mtext);
931 Prev.Insert (pos, interval);
932 else if (pos == From)
934 MInterval prev = Prev;
940 prev.Insert (pos, interval);
943 prev.remove_properties
944 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.RearSticky);
945 interval.inherit_front_properties (prev.Stack);
948 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.FrontSticky);
949 interval.inherit_rear_properties (Stack);
952 interval = graft (interval, false, out len);
953 if (interval != null && prev != null)
954 interval = prev.graft (interval, true, out len);
955 if (interval != null)
961 // .-this-. ==> .-this-.
974 for (; i != null; i = i.Parent)
975 i.Length += interval.Length;
980 remove_properties (MTextProperty.Flag.Sensitive);
983 interval = graft (interval, true, out len);
985 if (interval != null)
986 interval = graft (interval, false, out len);
987 if (interval != null)
990 Right.Left = interval;
991 interval.Parent = Right;
992 for (MInterval i = Right; i != null; i = i.Parent)
993 i.Length += interval.Length;
998 MInterval next = Next;
1004 next.Insert (pos, interval);
1007 next.remove_properties
1008 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.FrontSticky);
1009 interval.inherit_rear_properties (next.Stack);
1012 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.RearSticky);
1013 interval.inherit_front_properties (Stack);
1016 interval = graft (interval, true, out len);
1017 if (interval != null && next != null)
1018 interval = next.graft (interval, false, out len);
1019 if (interval != null)
1025 // .-this-. ==> .-this-.
1038 interval.Parent = i;
1039 for (; i != null; i = i.Parent)
1040 i.Length += interval.Length;
1044 Next.Insert (pos, interval);
1047 private void vacate_node (MInterval interval)
1049 M17N.DebugPrint ("vacate #{0} to #{1}", ID, interval.ID);
1050 if (interval != null)
1051 interval.Parent = Parent;
1055 mtext.intervals.Put (Key, interval);
1059 if (this == Parent.Right)
1060 Parent.Right = interval;
1062 Parent.Left = interval;
1065 if (interval != null)
1066 diff -= interval.Length;
1067 for (MInterval i = Parent; i != null; i = i.Parent)
1072 public void Delete (int start, int end)
1075 M17N.DebugPrint ("delete({0} {1}) from ", start, end); DumpOne (false, true);
1080 Left.Delete (start, end);
1083 Left.Delete (start, From);
1085 end -= From - start;
1092 Right.Delete (start, end);
1095 Right.Delete (To, end);
1098 if (start == From && end == To)
1110 for (i = Right; i.Left != null; i = i.Left)
1111 i.Length += Left.Length;
1112 i.Length += Left.Length;
1116 vacate_node (Right);
1121 int len = end - start;
1123 for (MInterval i = this; i != null; i = i.Parent)
1128 public void Push (int start, int end, MTextProperty prop)
1131 M17N.DebugPrint ("push({0} {1}) at ", start, end); DumpOne (false, true);
1136 Left.Push (start, end, prop);
1139 Left.Push (start, From, prop);
1146 Right.Push (start, end, prop);
1149 Right.Push (To, end, prop);
1154 divide_left (start);
1157 Stack.Push (prop.key, prop);
1160 private bool try_merge_prev ()
1162 MInterval prev = Prev;
1164 if (! mergeable (prev))
1167 int len = prev.Length - prev.LeftLength;
1169 // PREV is Left, Left.Right, ..., or Left....Right.
1172 prev.Parent.Right = prev.Left;
1173 while (prev.Parent != Left)
1180 if (Left.Length == Left.LeftLength)
1185 private bool try_merge_next ()
1187 MInterval next = Next;
1189 if (! mergeable (next))
1192 int len = next.Length - next.LeftLength;
1194 // NEXT is Right, Right.Left, ..., or Right....Left.
1197 next.Parent.Left = next.Right;
1198 while (next.Parent != Right)
1204 Right.Length -= len;
1205 if (Right.Length == Right.LeftLength)
1212 public void Pop (int start, int end)
1215 M17N.DebugPrint ("pop({0} {1}) at ", start, end); DumpOne (false, true);
1220 Left.Pop (start, end);
1223 Left.Pop (start, From);
1230 Right.Pop (start, end);
1233 Right.Pop (To, end);
1237 if (! Stack.IsEmpty)
1239 bool check_prev = start == From && start > 0;
1240 bool check_next = end == To && end < mtext.Length;
1243 divide_left (start);
1247 if (check_prev && Left != null)
1248 check_prev = try_merge_prev () && (Left != null);
1249 if (check_next && Right != null)
1250 check_next = try_merge_next () && (Right != null);
1253 if (Prev.try_merge_next () && check_next)
1254 Prev.try_merge_next ();
1256 else if (check_next)
1258 Next.try_merge_prev ();
1263 private void DumpOne (bool with_prop, bool newline)
1265 DumpOne (with_prop, newline, false);
1268 private void DumpOne (bool with_prop, bool newline, bool force)
1270 if (force || M17N.debug)
1272 Console.Write ("#{0}({1} {2} {3}", ID, Length, From, To);
1274 foreach (MPlist p in Stack)
1275 Console.Write (" " + p.Val);
1276 Console.Write (")");
1278 Console.WriteLine ();
1282 public void Dump () { Dump (false); }
1285 public void Dump (bool force)
1287 if (force || M17N.debug)
1294 Console.Write (" ");
1295 DumpOne (true, false, force);
1302 private class MTextEnum : IEnumerator
1305 private int pos = -1;
1307 public MTextEnum (MText mt)
1312 public bool MoveNext ()
1315 return (pos < mt.nchars);
1318 public void Reset ()
1323 public object Current
1326 //if (pos < 0 || pos >= mt.nchars)
1327 //throw new InvalidOperationException ();