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)
58 public MTextProperty (MSymbol key, object val,
59 bool front_sticky, bool rear_sticky, bool sensitive)
64 flags |= Flag.FrontSticky;
66 flags |= Flag.RearSticky;
68 flags |= Flag.Sensitive;
71 public override string ToString ()
73 return key.ToString () + ":" + val;
77 public class MText : IEnumerable, IEquatable<MText>, IComparable<MText>
80 public enum MTextFormat format;
82 private StringBuilder sb;
84 private int cache_pos;
85 private int cache_idx;
86 private MPlist intervals;
87 private MPlist default_property;
88 private bool read_only;
90 private static UTF8Encoding utf8 = new UTF8Encoding ();
92 private static int count_chars (String str)
94 int len = str.Length, n = 0;
96 for (int i = 0; i < len; i++, n++)
97 if (surrogate_high_p (str[i]))
102 private static int count_chars (StringBuilder str)
104 int len = str.Length, n = 0;
106 for (int i = 0; i < len; i++, n++)
107 if (surrogate_high_p (str[i]))
114 sb = new StringBuilder ();
115 intervals = new MPlist ();
118 public MText (byte[] str)
120 sb = new StringBuilder (utf8.GetString (str));
121 nchars = count_chars (sb);
122 intervals = new MPlist ();
125 public MText (String str)
127 sb = new StringBuilder (str);
128 nchars = count_chars (str);
129 intervals = new MPlist ();
132 public MText (StringBuilder str)
135 nchars = count_chars (str);
136 intervals = new MPlist ();
139 public static MText operator+ (MText mt1, MText mt2)
141 MText mt = new MText ();
143 mt.sb.Append (mt1.sb);
144 mt.sb.Append (mt2.sb);
145 mt.nchars = mt1.nchars + mt2.nchars;
150 public bool ReadOnly { get { return read_only; } }
151 public int Length { get { return nchars; } }
155 // for IEnumerable interface
156 public IEnumerator GetEnumerator() { return new MTextEnum (this); }
158 // for IEquatable interface
159 public bool Equals (MText other) { return this.sb.Equals (other.sb); }
161 // for IComparable interface
162 public int CompareTo (MText other)
164 return this.sb.ToString ().CompareTo (other.sb.ToString ());
167 public override String ToString () { return "\"" + sb.ToString () + "\""; }
169 private static bool surrogate_high_p (char c)
171 return (c >= 0xD800 && c < 0xDC00);
174 private static bool surrogate_low_p (char c)
176 return (c >= 0xDC00 && c < 0xE000);
179 private static int inc_idx (StringBuilder sb, int i)
181 return (i + (surrogate_high_p (sb[i]) ? 2 : 1));
184 private static int dec_idx (StringBuilder sb, int i)
186 return (i - (surrogate_low_p (sb[i - 1]) ? 2 : 1));
189 private static int pos_to_idx (MText mt, int pos)
191 if (pos == mt.cache_pos)
197 if (pos < mt.cache_pos)
199 if (mt.cache_pos == mt.cache_idx)
201 if (pos < mt.cache_pos - pos)
208 p = mt.cache_pos; i = mt.cache_idx;
214 if (mt.nchars - mt.cache_pos == mt.sb.Length - mt.cache_idx)
215 return (mt.cache_idx + pos - mt.cache_pos);
216 if (pos - mt.cache_pos < mt.nchars - pos)
218 p = mt.cache_pos; i = mt.cache_idx;
223 p = mt.nchars; i = mt.sb.Length;
228 for (; p < pos; i = inc_idx (mt.sb, i), p++);
230 for (; p > pos; i = dec_idx (mt.sb, i), p--);
236 private void check_pos (int pos, bool tail_ok)
238 if (pos < 0 || (tail_ok ? pos > nchars : pos >= nchars))
239 throw new Exception ("Invalid MText position:" + pos);
242 private bool check_range (int from, int to, bool zero_ok)
244 if (from < 0 || (zero_ok ? from > to : from >= to)
246 throw new Exception ("Invalid MText range");
250 private void insert (int pos, MText mt2, int from, int to)
252 check_pos (pos, true);
256 int pos_idx = pos_to_idx (this, pos);
257 int from_idx = pos_to_idx (mt2, from);
258 int to_idx = pos_to_idx (mt2, to);
260 sb.Insert (pos_idx, mt2.sb.ToString (from_idx, to_idx - from_idx));
263 foreach (MPlist plist in intervals)
265 MPlist p = mt2.intervals.Find (plist.Key);
269 i = new MInterval (plist.Key, this, to - from);
271 i = ((MInterval) p.Val).Copy (this, from, to);
272 ((MInterval) plist.Val).Insert (pos, i);
274 foreach (MPlist plist in mt2.intervals)
275 if (intervals.Find (plist.Key) == null)
277 MInterval i = (((MInterval) plist.Val).Copy (this, from, to));
278 intervals.Push (plist.Key, i);
282 private void insert (int pos, int c)
284 check_pos (pos, true);
286 int pos_idx = pos_to_idx (this, pos);
291 sb.Insert (pos_idx, ch);
295 char high = (char) (0xD800 + ((c - 0x10000) >> 10));
296 char low = (char) (0xDC00 + ((c - 0x10000) & 0x3FF));
297 sb.Insert (pos_idx, low);
298 sb.Insert (pos_idx, high);
301 foreach (MPlist plist in intervals)
302 ((MInterval) plist.Val).Insert (pos,
303 new MInterval (plist.Key, this, 1));
306 public int this[int i]
309 i = pos_to_idx (this, i);
312 if (surrogate_high_p (sb[i]))
314 sb[i] = (char) value;
318 char high = (char) (0xD800 + ((value - 0x10000) >> 10));
319 char low = (char) (0xDC00 + ((value - 0x10000) & 0x3FF));
321 if (! surrogate_high_p (sb[i]))
328 i = pos_to_idx (this, i);
329 return (surrogate_high_p (sb[i])
330 ? ((sb[i] - 0xD800) << 10) + (sb[i + 1] - 0xDC00) + 0x10000
337 MText mt = new MText (sb.ToString ());
339 foreach (MPlist p in intervals)
340 mt.intervals.Add (p.Key, ((MInterval) p.Val).Copy (mt, 0, Length));
344 public MText Ins (int pos, int c)
350 public MText Ins (int pos, MText mt)
352 insert (pos, mt, 0, mt.nchars);
356 public MText Ins (int pos, MText mt, int from, int to)
358 insert (pos, mt, from, to);
362 public MText Cat (int c)
368 public MText Del (int from, int to)
370 if (check_range (from, to, true))
373 sb.Remove (from, pos_to_idx (this, to) - pos_to_idx (this, from));
377 foreach (MPlist plist in intervals)
378 ((MInterval) plist.Val).Delete (from, to);
380 intervals = new MPlist ();
386 public object GetProp (int pos, MSymbol key)
388 check_pos (pos, false);
390 MInterval i = (MInterval) intervals.Get (key);
394 MTextProperty prop = i.Get (pos);
395 return (prop != null ? prop.Val : null);
398 public object GetProp (int pos, MSymbol key, out MTextProperty prop)
400 check_pos (pos, false);
402 MInterval i = (MInterval) intervals.Get (key);
404 return (prop = null);
406 return (prop != null ? prop.Val : null);
409 public object GetProp (int pos, MSymbol key, out MTextProperty[] array)
411 check_pos (pos, false);
413 MInterval i = (MInterval) intervals.Get (key);
415 return (array = null);
416 MTextProperty prop = i.Get (pos, out array);
417 return (prop != null ? prop.Val : null);
420 public void PushProp (int from, int to, MSymbol key, object val)
422 if (! check_range (from, to, true))
423 PushProp (from, to, new MTextProperty (key, val));
426 public void PushProp (int from, int to, MTextProperty prop)
430 if (default_property == null)
431 default_property = new MPlist ();
432 default_property.Push (prop.key, prop.val);
436 if (check_range (from, to, true))
439 MPlist p = intervals.Find (prop.key);
444 root = new MInterval (prop.key, this);
445 intervals.Push (prop.key, root);
448 root = (MInterval) p.Val;
450 root.Push (from, to, prop);
454 public void PopProp (int from, int to, MSymbol key)
458 if (default_property == null)
460 MPlist p = default_property.Find (key);
467 if (check_range (from, to, true))
470 MPlist p = intervals.Find (key);
474 MInterval root = (MInterval) p.Val;
476 root.MergeAfterChange (from, to);
481 public void DumpProp ()
484 foreach (MPlist p in intervals)
485 ((MInterval) p.Val).Dump (true);
486 Console.WriteLine (")");
489 public void DumpPropNested ()
491 Console.WriteLine ("total length = {0}", Length);
492 foreach (MPlist p in intervals)
493 ((MInterval) p.Val).DumpNested (true);
496 private class MInterval
498 // position: 0 1 2 3 4 5 6 7
499 // | A | B | C | D | E F | G |
500 // interval |---|---|---|<->|-------|---|
501 // |---|<->|---| |<----->|---|
505 // [3 (1 2)] [3 (4 6)]
506 // [1 (0 1)] [2 (2 3)] [1 (6 7)]
508 private static int count = 0;
511 private int From, To;
513 private MPlist Stack;
514 private MInterval Left, Right, Parent;
517 public MInterval (MSymbol key, MText mt, int length)
520 throw new Exception ("Invalid interval length");
524 Stack = new MPlist ();
528 public MInterval (MSymbol key, MText mt)
532 Length = mt.sb.Length;
535 Stack = new MPlist ();
539 public MTextProperty Get (int pos)
541 MInterval i = find (pos);
543 return (i.Stack.IsEmpty ? null : (MTextProperty) i.Stack.Val);
546 public MTextProperty Get (int pos, out MTextProperty[] array)
548 MInterval i = find (pos);
555 array = new MTextProperty[i.Stack.Count];
559 for (idx = 0, p = i.Stack; ! p.IsEmpty; idx++, p = p.Next)
560 array[idx] = (MTextProperty) p.Val;
564 private MInterval (MSymbol key, MText mt, int length, MPlist stack)
571 Stack = stack.Clone ();
575 private void update_from_to ()
580 To = Length - RightLength;
582 else if (Parent.Left == this)
584 From = Parent.From - Length + LeftLength;
585 To = Parent.From - RightLength;
589 From = Parent.To + LeftLength;
590 To = Parent.To + Length - RightLength;
594 private int LeftLength
596 get { return (Left == null ? 0 : Left.Length); }
599 private int RightLength
601 get { return (Right == null ? 0 : Right.Length); }
604 private MInterval LeftMost
610 return Left.LeftMost;
614 private MInterval RightMost
620 return Right.RightMost;
624 private MInterval Prev {
629 for (i = Left; i.Right != null; i = i.Right)
633 MInterval child = this;
634 for (i = Parent; i != null && i.Left == child;
635 child = i, i = i.Parent);
641 private MInterval Next {
646 for (i = Right; i.Left != null; i = i.Left)
650 MInterval child = this;
651 for (i = Parent; i != null && i.Right == child;
652 child = i, i = i.Parent);
658 private MInterval find (int pos)
662 return Left.find (pos);
664 return Right.find (pos);
668 private bool mergeable (MInterval i)
672 for (p1 = Stack, p2 = i.Stack; ! p1.IsEmpty && ! p2.IsEmpty;
673 p1 = p1.Next, p2 = p2.Next)
674 if (p1.Val != p2.Val)
676 return (p1.IsEmpty && p2.IsEmpty);
679 // p-. or .-p p-. or .-p
680 // .-this-. .-right-.
681 // left .-right-. -> .-this-. c2
683 private MInterval promote_right ()
685 int right_length = Right.Length;
689 mtext.intervals.Put (Key, Right);
690 else if (Parent.Left == this)
693 Parent.Right = Right;
694 Right.Parent = Parent;
700 Parent.Length += Length;
701 Length -= right_length;
705 Parent.Length -= c1.Length;
711 // p-. or .-p p-. or .-p
713 // .-left-. .-right-. -> c1 .-this-.
715 private MInterval promote_left ()
717 int left_length = Left.Length;
721 mtext.intervals.Put (Key, Left);
722 else if (Parent.Left == this)
726 Left.Parent = Parent;
732 Parent.Length += Length;
733 Length -= left_length;
737 Parent.Length -= c1.Length;
743 private MInterval balance ()
750 // .-left-. .-right-.
752 int diff = i.LeftLength - i.RightLength;
757 new_diff = (i.Length - i.LeftLength
758 + i.Left.RightLength - i.Left.LeftLength);
759 if (Math.Abs (new_diff) >= diff)
761 i = i.promote_left ();
766 new_diff = (i.Length - i.RightLength
767 + i.Right.LeftLength - i.Right.RightLength);
768 if (Math.Abs (new_diff) >= diff)
770 i = i.promote_right ();
777 public MInterval Copy (MText mt, int start, int end)
779 MInterval copy, left_copy = null, right_copy = null;
786 return Left.Copy (mt, start, end);
787 left_copy = Left.Copy (mt, start, From);
792 return Right.Copy (mt, start, end);
793 right_copy = Right.Copy (mt, To, end);
796 copy = new MInterval (Key, null, end - start, Stack);
798 remove_properties (MTextProperty.Flag.Sensitive);
799 if (left_copy != null)
801 copy.Left = left_copy;
802 left_copy.Parent = copy;
804 if (right_copy != null)
806 copy.Right = right_copy;
807 right_copy.Parent = copy;
815 private MInterval divide_right (int pos)
817 MInterval interval = new MInterval (Key, mtext, To - pos, Stack);
819 M17N.DebugPrint ("divide-right({0}) at ", pos); DumpOne (false, true);
823 interval.Right = Right;
824 Right.Parent = interval;
825 interval.Length += Right.Length;
827 interval.Parent = this;
835 private MInterval divide_left (int pos)
837 MInterval interval = new MInterval (Key, mtext, pos - From, Stack);
839 M17N.DebugPrint ("divide-left({0}) at ", pos); DumpOne (false, true);
843 interval.Left = Left;
844 Left.Parent = interval;
845 interval.Length += Left.Length;
847 interval.Parent = this;
852 private void remove_properties (MTextProperty.Flag flags)
854 for (MPlist p = Stack; ! p.IsEmpty;)
856 MTextProperty prop = (MTextProperty) p.Val;
858 if ((prop.flags & flags) == flags)
865 private void inherit_front_properties (MPlist plist)
867 for (MInterval i = LeftMost; i != null; i = i.Next)
871 for (MPlist p = plist; ! p.IsEmpty; p = p.Next)
873 MTextProperty prop = (MTextProperty) p.Val;
875 if ((prop.flags & MTextProperty.Flag.RearSticky)
876 == MTextProperty.Flag.RearSticky)
877 i.Stack.Add (prop.key, prop);
882 private void inherit_rear_properties (MPlist plist)
884 for (MInterval i = RightMost; i != null; i = i.Prev)
888 for (MPlist p = plist; ! p.IsEmpty; p = p.Next)
890 MTextProperty prop = (MTextProperty) p.Val;
892 if ((prop.flags & MTextProperty.Flag.FrontSticky)
893 == MTextProperty.Flag.FrontSticky)
894 i.Stack.Add (prop.key, prop);
899 private MInterval delete_node_forward ()
903 int len = Length - RightLength;
905 for (MInterval i = Parent; i != null; i = i.Parent)
907 if (Parent.Left == this)
910 Parent.Right = Right;
915 Right.Parent = Parent;
916 return Right.LeftMost;
921 private MInterval delete_node_backward ()
925 int len = Length - LeftLength;
927 for (MInterval i = Parent; i != null; i = i.Parent)
929 if (Parent.Left == this)
937 Left.Parent = Parent;
938 return Left.RightMost;
943 private void set_mtext (MText mt)
949 Right.set_mtext (mt);
952 private MInterval graft (MInterval interval, bool forward, out int len)
959 i = interval.LeftMost;
964 len += i.Length - i.RightLength;
965 i = i.delete_node_forward ();
970 i = interval.RightMost;
975 len += i.Length - i.LeftLength;
976 i = i.delete_node_backward ();
984 M17N.DebugPrint ("grafted {0} in ", len); DumpOne (false, true);
985 for (MInterval prev = this, ii = this.Parent; ii != null;
986 prev = ii, ii = ii.Parent)
996 while (i.Parent != null) i = i.Parent;
1000 public void Insert (int pos, MInterval interval)
1003 M17N.DebugPrint ("insert({0}) at {1} in ", interval.Length, pos);
1004 DumpOne (false, false);
1006 interval.set_mtext (mtext);
1009 Prev.Insert (pos, interval);
1010 else if (pos == From)
1012 MInterval prev = Prev;
1018 prev.Insert (pos, interval);
1021 prev.remove_properties
1022 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.RearSticky);
1023 interval.inherit_front_properties (prev.Stack);
1026 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.FrontSticky);
1027 interval.inherit_rear_properties (Stack);
1030 interval = graft (interval, false, out len);
1031 if (interval != null && prev != null)
1032 interval = prev.graft (interval, true, out len);
1033 if (interval != null)
1039 // .-this-. ==> .-this-.
1051 interval.Parent = i;
1052 for (; i != null; i = i.Parent)
1053 i.Length += interval.Length;
1058 remove_properties (MTextProperty.Flag.Sensitive);
1061 interval = graft (interval, true, out len);
1063 if (interval != null)
1064 interval = graft (interval, false, out len);
1065 if (interval != null)
1068 Right.Left = interval;
1069 interval.Parent = Right;
1070 for (MInterval i = Right; i != null; i = i.Parent)
1071 i.Length += interval.Length;
1076 MInterval next = Next;
1082 next.Insert (pos, interval);
1085 next.remove_properties
1086 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.FrontSticky);
1087 interval.inherit_rear_properties (next.Stack);
1090 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.RearSticky);
1091 interval.inherit_front_properties (Stack);
1094 interval = graft (interval, true, out len);
1095 if (interval != null && next != null)
1096 interval = next.graft (interval, false, out len);
1097 if (interval != null)
1103 // .-this-. ==> .-this-.
1116 interval.Parent = i;
1117 for (; i != null; i = i.Parent)
1118 i.Length += interval.Length;
1122 Next.Insert (pos, interval);
1123 M17N.DebugPrint (" done\n");
1126 private void vacate_node (MInterval interval)
1128 vacate_node (interval, null);
1131 private void vacate_node (MInterval interval, MInterval stop)
1133 if (interval != null)
1134 M17N.DebugPrint ("vacate #{0} to #{1}\n", ID, interval.ID);
1136 M17N.DebugPrint ("vacate #{0} to null\n", ID);
1137 if (interval != null)
1138 interval.Parent = Parent;
1142 mtext.intervals.Put (Key, interval);
1146 if (this == Parent.Right)
1147 Parent.Right = interval;
1149 Parent.Left = interval;
1152 if (interval != null)
1153 diff -= interval.Length;
1154 for (MInterval i = Parent; i != stop; i = i.Parent)
1159 public void Delete (int start, int end)
1162 M17N.DebugPrint ("delete({0} {1}) from ", start, end); DumpOne (false, true);
1167 Left.Delete (start, end);
1170 Left.Delete (start, From);
1172 end -= From - start;
1179 Right.Delete (start, end);
1182 Right.Delete (To, end);
1185 if (start == From && end == To)
1197 for (i = Right; i.Left != null; i = i.Left)
1198 i.Length += Left.Length;
1199 i.Length += Left.Length;
1203 vacate_node (Right);
1208 int len = end - start;
1210 for (MInterval i = this; i != null; i = i.Parent)
1215 public void Push (int start, int end, MTextProperty prop)
1218 M17N.DebugPrint ("push({0} {1}) at ", start, end); DumpOne (false, true);
1223 Left.Push (start, end, prop);
1226 Left.Push (start, From, prop);
1233 Right.Push (start, end, prop);
1236 Right.Push (To, end, prop);
1241 divide_left (start);
1244 Stack.Push (prop.key, prop);
1247 private static void merge_nodes (MInterval head, MInterval tail)
1249 M17N.DebugPrint ("merging "); head.DumpOne (true, false);
1250 M17N.DebugPrint (" through "); tail.DumpOne (true, false);
1252 int from = head.From;
1256 for (root = head; root.To + root.RightLength < to;
1257 root = root.Parent);
1259 M17N.DebugPrint (" common root is "); root.DumpOne (false, true);
1261 if (from < root.From)
1263 MInterval prev = root.Prev;
1267 M17N.DebugPrint ("merging "); prev.DumpOne (false, true);
1268 prev.vacate_node (prev.Left, root);
1271 if (prev.Left != null)
1272 prev = prev.Left.RightMost;
1279 MInterval next = root.Next;
1283 M17N.DebugPrint ("merging "); next.DumpOne (false, true);
1284 next.vacate_node (next.Right, root);
1287 if (next.Right != null)
1288 next = next.Right.LeftMost;
1295 public void MergeAfterChange (int start, int end)
1300 Prev.MergeAfterChange (start, end);
1304 MInterval head = this, tail = this, i;
1306 if (start == From && start > 0)
1312 while (tail.To < end)
1315 if (! tail.mergeable (i))
1318 merge_nodes (head, tail);
1326 if (i == null || ! tail.mergeable (i))
1331 merge_nodes (head, tail);
1334 public void Pop (int start, int end)
1337 M17N.DebugPrint ("pop({0} {1}) at ", start, end); DumpOne (false, true);
1342 Left.Pop (start, end);
1345 Left.Pop (start, From);
1352 Right.Pop (start, end);
1355 Right.Pop (To, end);
1359 if (! Stack.IsEmpty)
1362 divide_left (start);
1369 private void DumpOne (bool with_prop, bool newline)
1371 DumpOne (with_prop, newline, false);
1374 private void DumpOne (bool with_prop, bool newline, bool force)
1376 if (force || M17N.debug)
1378 Console.Write ("#{0}({1} {2} {3}", ID, Length, From, To);
1380 foreach (MPlist p in Stack)
1381 Console.Write (" " + p.Val);
1382 Console.Write (")");
1384 Console.WriteLine ();
1386 throw new Exception ("Invalid interval length");
1390 public void Dump () { Dump (false); }
1392 public void Dump (bool force)
1394 if (force || M17N.debug)
1401 Console.Write (" ");
1402 DumpOne (true, false, force);
1409 get { return (Parent == null ? 0 : Parent.Depth + 1); }
1412 public void DumpNested (bool force)
1414 DumpNested ("", force);
1417 public void DumpNested (string indent, bool force)
1419 if (force || M17N.debug)
1421 int indent_type = (Parent == null ? 1
1422 : Parent.Left == this ? 0 : 2);
1427 if (indent_type <= 1)
1428 Left.DumpNested (indent + " ", force);
1430 Left.DumpNested (indent + "| ", force);
1432 if (indent_type == 0)
1433 Console.Write (indent + ".-");
1434 else if (indent_type == 2)
1435 Console.Write (indent + "`-");
1436 DumpOne (true, true, true);
1439 if (indent_type >= 1)
1440 Right.DumpNested (indent + " ", force);
1442 Right.DumpNested (indent + "| ", force);
1448 private class MTextEnum : IEnumerator
1451 private int pos = -1;
1453 public MTextEnum (MText mt)
1458 public bool MoveNext ()
1461 return (pos < mt.nchars);
1464 public void Reset ()
1469 public object Current
1472 //if (pos < 0 || pos >= mt.nchars)
1473 //throw new InvalidOperationException ();