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 private void insert (int pos, int c)
280 check_pos (pos, true);
282 int pos_idx = pos_to_idx (this, pos);
287 sb.Insert (pos_idx, ch);
291 char high = (char) (0xD800 + ((c - 0x10000) >> 10));
292 char low = (char) (0xDC00 + ((c - 0x10000) & 0x3FF));
293 sb.Insert (pos_idx, low);
294 sb.Insert (pos_idx, high);
297 foreach (MPlist plist in intervals)
298 ((MInterval) plist.Val).Insert (pos,
299 new MInterval (plist.Key, this, 1));
302 public int this[int i]
305 i = pos_to_idx (this, i);
308 if (surrogate_high_p (sb[i]))
310 sb[i] = (char) value;
314 char high = (char) (0xD800 + ((value - 0x10000) >> 10));
315 char low = (char) (0xDC00 + ((value - 0x10000) & 0x3FF));
317 if (! surrogate_high_p (sb[i]))
324 i = pos_to_idx (this, i);
325 return (surrogate_high_p (sb[i])
326 ? ((sb[i] - 0xD800) << 10) + (sb[i + 1] - 0xDC00) + 0x10000
333 return (new MText (sb.ToString ()));
336 public MText Ins (int pos, int c)
342 public MText Ins (int pos, MText mt)
344 insert (pos, mt, 0, mt.nchars);
348 public MText Ins (int pos, MText mt, int from, int to)
350 insert (pos, mt, from, to);
354 public MText Cat (int c)
360 public MText Del (int from, int to)
362 if (check_range (from, to, true))
365 sb.Remove (from, pos_to_idx (this, to) - pos_to_idx (this, from));
369 foreach (MPlist plist in intervals)
370 ((MInterval) plist.Val).Delete (from, to);
372 intervals = new MPlist ();
376 public object GetProp (int pos, MSymbol key)
378 check_pos (pos, false);
380 MInterval i = (MInterval) intervals.Get (key);
384 MTextProperty prop = i.Get (pos);
385 return (prop != null ? prop.Val : null);
388 public object GetProp (int pos, MSymbol key, out MTextProperty prop)
390 check_pos (pos, false);
392 MInterval i = (MInterval) intervals.Get (key);
394 return (prop = null);
396 return (prop != null ? prop.Val : null);
399 public object GetProp (int pos, MSymbol key, out MTextProperty[] array)
401 check_pos (pos, false);
403 MInterval i = (MInterval) intervals.Get (key);
405 return (array = null);
406 MTextProperty prop = i.Get (pos, out array);
407 return (prop != null ? prop.Val : null);
410 public void PushProp (int from, int to, MSymbol key, object val)
412 if (! check_range (from, to, true))
413 PushProp (from, to, new MTextProperty (key, val));
416 public void PushProp (int from, int to, MTextProperty prop)
420 if (default_property == null)
421 default_property = new MPlist ();
422 default_property.Push (prop.key, prop.val);
426 if (check_range (from, to, true))
429 MPlist p = intervals.Find (prop.key);
434 root = new MInterval (prop.key, this);
435 intervals.Push (prop.key, root);
438 root = (MInterval) p.Val;
440 root.Push (from, to, prop);
444 public void PopProp (int from, int to, MSymbol key)
448 if (default_property == null)
450 MPlist p = default_property.Find (key);
457 if (check_range (from, to, true))
460 MPlist p = intervals.Find (key);
463 ((MInterval) p.Val).Pop (from, to);
467 public void DumpProp ()
470 foreach (MPlist p in intervals)
471 ((MInterval) p.Val).Dump (true);
472 Console.WriteLine (")");
475 private class MInterval
477 // position: 0 1 2 3 4 5 6 7
478 // | A | B | C | D | E F | G |
479 // interval |---|---|---|<->|-------|---|
480 // |---|<->|---| |<----->|---|
484 // [3 (1 2)] [3 (4 6)]
485 // [1 (0 1)] [2 (2 3)] [1 (6 7)]
487 private static int count = 0;
490 private int From, To;
492 private MPlist Stack;
493 private MInterval Left, Right, Parent;
496 public MInterval (MSymbol key, MText mt, int length)
499 throw new Exception ("Invalid interval length");
503 Stack = new MPlist ();
507 public MInterval (MSymbol key, MText mt)
511 Length = mt.sb.Length;
514 Stack = new MPlist ();
518 public MTextProperty Get (int pos)
520 MInterval i = find (pos);
522 return (i.Stack.IsEmpty ? null : (MTextProperty) i.Stack.Val);
525 public MTextProperty Get (int pos, out MTextProperty[] array)
527 MInterval i = find (pos);
534 array = new MTextProperty[i.Stack.Count];
538 for (idx = 0, p = i.Stack; ! p.IsEmpty; idx++, p = p.Next)
539 array[idx] = (MTextProperty) p.Val;
540 return array[idx - 1];
543 private MInterval (MSymbol key, MText mt, int length, MPlist stack)
550 Stack = stack.Clone ();
554 private void update_from_to ()
559 To = Length - RightLength;
561 else if (Parent.Left == this)
563 From = Parent.From - Length + LeftLength;
564 To = Parent.From - RightLength;
568 From = Parent.To + LeftLength;
569 To = Parent.To + Length - RightLength;
573 private int LeftLength
575 get { return (Left == null ? 0 : Left.Length); }
578 private int RightLength
580 get { return (Right == null ? 0 : Right.Length); }
583 private MInterval LeftMost
585 get { return (Left == null ? this : Left.LeftMost); }
588 private MInterval RightMost
590 get { return (Right == null ? this : Right.RightMost); }
593 private MInterval Prev {
598 for (i = Left; i.Right != null; i = i.Right);
601 MInterval child = this;
602 for (i = Parent; i != null && i.Left == child;
603 child = i, i = i.Parent);
609 private MInterval Next {
614 for (i = Right; i.Left != null; i = i.Left);
617 MInterval child = this;
618 for (i = Parent; i != null && i.Right == child;
619 child = i, i = i.Parent);
625 private MInterval find (int pos)
629 return Left.find (pos);
631 return Right.find (pos);
635 private bool mergeable (MInterval i)
639 for (p1 = Stack, p2 = i.Stack; ! p1.IsEmpty && ! p2.IsEmpty;
640 p1 = p1.Next, p2 = p2.Next)
641 if (p1.Val != p2.Val)
643 return (p1.IsEmpty && p2.IsEmpty);
646 // p-. or .-p p-. or .-p
647 // .-this-. .-right-.
648 // left .-right-. -> .-this-. c2
650 private MInterval promote_right ()
652 int right_length = Right.Length;
656 mtext.intervals.Put (Key, Right);
657 else if (Parent.Left == this)
660 Parent.Right = Right;
661 Right.Parent = Parent;
667 Parent.Length += Length;
668 Length -= right_length;
672 Parent.Length -= c1.Length;
678 // p-. or .-p p-. or .-p
680 // .-left-. .-right-. -> c1 .-this-.
682 private MInterval promote_left ()
684 int left_length = Left.Length;
688 mtext.intervals.Put (Key, Left);
689 else if (Parent.Left == this)
693 Left.Parent = Parent;
699 Parent.Length += Length;
700 Length -= left_length;
704 Parent.Length -= c1.Length;
710 private MInterval balance ()
717 // .-left-. .-right-.
719 int diff = i.LeftLength - i.RightLength;
724 new_diff = (i.Length - i.LeftLength
725 + i.Left.RightLength - i.Left.LeftLength);
726 if (Math.Abs (new_diff) >= diff)
728 i = i.promote_left ();
733 new_diff = (i.Length - i.RightLength
734 + i.Right.LeftLength - i.Right.RightLength);
735 if (Math.Abs (new_diff) >= diff)
737 i = i.promote_right ();
744 public MInterval Copy (int start, int end)
746 MInterval copy, left_copy = null, right_copy = null;
752 return Left.Copy (start, end);
753 left_copy = Left.Copy (start, From);
758 return Right.Copy (start, end);
759 right_copy = Right.Copy (To, end);
762 copy = new MInterval (Key, null, end - start, Stack);
763 remove_properties (MTextProperty.Flag.Sensitive);
764 copy.Left = left_copy;
765 copy.Right = right_copy;
773 private MInterval divide_right (int pos)
775 MInterval interval = new MInterval (Key, mtext, To - pos, Stack);
777 M17N.DebugPrint ("divide-right({0}) at ", pos); DumpOne (false, true);
781 interval.Right = Right;
782 Right.Parent = interval;
783 interval.Length += Right.Length;
785 interval.Parent = this;
793 private MInterval divide_left (int pos)
795 MInterval interval = new MInterval (Key, mtext, pos - From, Stack);
797 M17N.DebugPrint ("divide-left({0}) at ", pos); DumpOne (false, true);
801 interval.Left = Left;
802 Left.Parent = interval;
803 interval.Length += Left.Length;
805 interval.Parent = this;
810 private void remove_properties (MTextProperty.Flag flags)
812 for (MPlist p = Stack; ! p.IsEmpty;)
814 MTextProperty prop = (MTextProperty) p.Val;
816 if ((prop.flags & flags) == flags)
823 private void inherit_front_properties (MPlist plist)
825 for (MInterval i = LeftMost; i != null; i = i.Next)
829 for (MPlist p = plist; ! p.IsEmpty; p = p.Next)
831 MTextProperty prop = (MTextProperty) p.Val;
833 if ((prop.flags & MTextProperty.Flag.RearSticky)
834 == MTextProperty.Flag.RearSticky)
835 i.Stack.Add (prop.key, prop);
840 private void inherit_rear_properties (MPlist plist)
842 for (MInterval i = RightMost; i != null; i = i.Prev)
846 for (MPlist p = plist; ! p.IsEmpty; p = p.Next)
848 MTextProperty prop = (MTextProperty) p.Val;
850 if ((prop.flags & MTextProperty.Flag.FrontSticky)
851 == MTextProperty.Flag.FrontSticky)
852 i.Stack.Add (prop.key, prop);
857 private MInterval delete_node_forward ()
861 int len = Length - RightLength;
863 for (MInterval i = Parent; i != null; i = i.Parent)
865 if (Parent.Left == this)
868 Parent.Right = Right;
873 Right.Parent = Parent;
874 return Right.LeftMost;
879 private MInterval delete_node_backward ()
883 int len = Length - RightLength;
885 for (MInterval i = Parent; i != null; i = i.Parent)
887 if (Parent.Left == this)
895 Left.Parent = Parent;
896 return Left.RightMost;
901 private void set_mtext (MText mt)
907 Right.set_mtext (mt);
910 private MInterval graft (MInterval interval, bool forward, out int len)
917 i = interval.LeftMost;
922 len += i.Length - i.RightLength;
923 i = i.delete_node_forward ();
928 i = interval.RightMost;
933 len += i.Length - i.LeftLength;
934 i = i.delete_node_backward ();
940 for (MInterval prev = this, ii = this.Parent; ii != null;
941 prev = ii, ii = ii.Parent)
951 while (i.Parent != null) i = i.Parent;
955 public void Insert (int pos, MInterval interval)
958 M17N.DebugPrint ("insert({0}) at {1} in ", interval.Length, pos);
959 DumpOne (false, true);
961 interval.set_mtext (mtext);
964 Prev.Insert (pos, interval);
965 else if (pos == From)
967 MInterval prev = Prev;
973 prev.Insert (pos, interval);
976 prev.remove_properties
977 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.RearSticky);
978 interval.inherit_front_properties (prev.Stack);
981 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.FrontSticky);
982 interval.inherit_rear_properties (Stack);
985 interval = graft (interval, false, out len);
986 if (interval != null && prev != null)
987 interval = prev.graft (interval, true, out len);
988 if (interval != null)
994 // .-this-. ==> .-this-.
1006 interval.Parent = i;
1007 for (; i != null; i = i.Parent)
1008 i.Length += interval.Length;
1013 remove_properties (MTextProperty.Flag.Sensitive);
1016 interval = graft (interval, true, out len);
1018 if (interval != null)
1019 interval = graft (interval, false, out len);
1020 if (interval != null)
1023 Right.Left = interval;
1024 interval.Parent = Right;
1025 for (MInterval i = Right; i != null; i = i.Parent)
1026 i.Length += interval.Length;
1031 MInterval next = Next;
1037 next.Insert (pos, interval);
1040 next.remove_properties
1041 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.FrontSticky);
1042 interval.inherit_rear_properties (next.Stack);
1045 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.RearSticky);
1046 interval.inherit_front_properties (Stack);
1049 interval = graft (interval, true, out len);
1050 if (interval != null && next != null)
1051 interval = next.graft (interval, false, out len);
1052 if (interval != null)
1058 // .-this-. ==> .-this-.
1071 interval.Parent = i;
1072 for (; i != null; i = i.Parent)
1073 i.Length += interval.Length;
1077 Next.Insert (pos, interval);
1080 private void vacate_node (MInterval interval)
1082 M17N.DebugPrint ("vacate #{0} to #{1}", ID, interval.ID);
1083 if (interval != null)
1084 interval.Parent = Parent;
1088 mtext.intervals.Put (Key, interval);
1092 if (this == Parent.Right)
1093 Parent.Right = interval;
1095 Parent.Left = interval;
1098 if (interval != null)
1099 diff -= interval.Length;
1100 for (MInterval i = Parent; i != null; i = i.Parent)
1105 public void Delete (int start, int end)
1108 M17N.DebugPrint ("delete({0} {1}) from ", start, end); DumpOne (false, true);
1113 Left.Delete (start, end);
1116 Left.Delete (start, From);
1118 end -= From - start;
1125 Right.Delete (start, end);
1128 Right.Delete (To, end);
1131 if (start == From && end == To)
1143 for (i = Right; i.Left != null; i = i.Left)
1144 i.Length += Left.Length;
1145 i.Length += Left.Length;
1149 vacate_node (Right);
1154 int len = end - start;
1156 for (MInterval i = this; i != null; i = i.Parent)
1161 public void Push (int start, int end, MTextProperty prop)
1164 M17N.DebugPrint ("push({0} {1}) at ", start, end); DumpOne (false, true);
1169 Left.Push (start, end, prop);
1172 Left.Push (start, From, prop);
1179 Right.Push (start, end, prop);
1182 Right.Push (To, end, prop);
1187 divide_left (start);
1190 Stack.Push (prop.key, prop);
1193 private bool try_merge_prev ()
1195 MInterval prev = Prev;
1197 if (! mergeable (prev))
1200 int len = prev.Length - prev.LeftLength;
1202 // PREV is Left, Left.Right, ..., or Left....Right.
1205 prev.Parent.Right = prev.Left;
1206 while (prev.Parent != Left)
1213 if (Left.Length == Left.LeftLength)
1218 private bool try_merge_next ()
1220 MInterval next = Next;
1222 if (! mergeable (next))
1225 int len = next.Length - next.LeftLength;
1227 // NEXT is Right, Right.Left, ..., or Right....Left.
1230 next.Parent.Left = next.Right;
1231 while (next.Parent != Right)
1237 Right.Length -= len;
1238 if (Right.Length == Right.LeftLength)
1245 public void Pop (int start, int end)
1248 M17N.DebugPrint ("pop({0} {1}) at ", start, end); DumpOne (false, true);
1253 Left.Pop (start, end);
1256 Left.Pop (start, From);
1263 Right.Pop (start, end);
1266 Right.Pop (To, end);
1270 if (! Stack.IsEmpty)
1272 bool check_prev = start == From && start > 0;
1273 bool check_next = end == To && end < mtext.Length;
1276 divide_left (start);
1280 if (check_prev && Left != null)
1281 check_prev = try_merge_prev () && (Left != null);
1282 if (check_next && Right != null)
1283 check_next = try_merge_next () && (Right != null);
1286 if (Prev.try_merge_next () && check_next)
1287 Prev.try_merge_next ();
1289 else if (check_next)
1291 Next.try_merge_prev ();
1296 private void DumpOne (bool with_prop, bool newline)
1298 DumpOne (with_prop, newline, false);
1301 private void DumpOne (bool with_prop, bool newline, bool force)
1303 if (force || M17N.debug)
1305 Console.Write ("#{0}({1} {2} {3}", ID, Length, From, To);
1307 foreach (MPlist p in Stack)
1308 Console.Write (" " + p.Val);
1309 Console.Write (")");
1311 Console.WriteLine ();
1315 public void Dump () { Dump (false); }
1318 public void Dump (bool force)
1320 if (force || M17N.debug)
1327 Console.Write (" ");
1328 DumpOne (true, false, force);
1335 private class MTextEnum : IEnumerator
1338 private int pos = -1;
1340 public MTextEnum (MText mt)
1345 public bool MoveNext ()
1348 return (pos < mt.nchars);
1351 public void Reset ()
1356 public object Current
1359 //if (pos < 0 || pos >= mt.nchars)
1360 //throw new InvalidOperationException ();