3 using System.Collections;
4 using System.Collections.Generic;
10 public enum MTextFormat
12 MTEXT_FORMAT_US_ASCII,
14 MTEXT_FORMAT_UTF_16BE,
15 MTEXT_FORMAT_UTF_16LE,
16 MTEXT_FORMAT_UTF_32BE,
17 MTEXT_FORMAT_UTF_32LE,
21 public class MTextProperty
27 internal enum Flag : byte
36 public MSymbol Key { get { return key; } }
37 public object Val { get { return val; } }
38 public bool FrontSticky
40 get { return (flags & Flag.FrontSticky) != Flag.None; }
42 public bool RearSticky
44 get { return (flags & Flag.RearSticky) != Flag.None; }
48 get { return (flags & Flag.Sensitive) != Flag.None; }
51 public MTextProperty (MSymbol key, object val)
55 flags |= Flag.RearSticky;
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);
254 int pos_idx = pos_to_idx (this, pos);
255 int from_idx = pos_to_idx (mt2, from);
256 int to_idx = pos_to_idx (mt2, to);
258 sb.Insert (pos_idx, mt2.sb.ToString (from_idx, to_idx - from_idx));
261 foreach (MPlist plist in mt2.intervals)
262 if (intervals.Find (plist.Key) == null)
263 intervals.Push (plist.Key, new MInterval (plist.Key, this));
264 foreach (MPlist plist in intervals)
266 MPlist p = mt2.intervals.Find (plist.Key);
270 interval = new MInterval (plist.Key, to - from);
272 interval = ((MInterval) p.Val).copy (from, to);
273 ((MInterval) plist.Val).Insert (pos, interval);
277 public int this[int i]
280 i = pos_to_idx (this, i);
283 if (surrogate_high_p (sb[i]))
285 sb[i] = (char) value;
289 char high = (char) (0xD800 + ((value - 0x10000) >> 10));
290 char low = (char) (0xDC00 + ((value - 0x10000) & 0x3FF));
292 if (! surrogate_high_p (sb[i]))
299 i = pos_to_idx (this, i);
300 return (surrogate_high_p (sb[i])
301 ? ((sb[i] - 0xD800) << 10) + (sb[i + 1] - 0xDC00) + 0x10000
308 return (new MText (sb.ToString ()));
311 public MText Ins (int pos, MText mt)
313 insert (pos, mt, 0, mt.nchars);
317 public MText Ins (int pos, MText mt, int from, int to)
319 insert (pos, mt, from, to);
323 public MText Del (int from, int to)
325 if (check_range (from, to, true))
328 sb.Remove (from, pos_to_idx (this, to) - pos_to_idx (this, from));
332 foreach (MPlist plist in intervals)
333 ((MInterval) plist.Val).Delete (from, to);
335 intervals = new MPlist ();
339 public object GetProp (int pos, MSymbol key)
341 check_pos (pos, false);
343 MInterval i = (MInterval) intervals.Find (key).Val;
348 MTextProperty prop = i.Get (pos);
349 return (prop != null ? prop.Val : null);
352 public object GetProp (int pos, MSymbol key, out MTextProperty prop)
354 check_pos (pos, false);
356 MInterval i = (MInterval) intervals.Find (key).Val;
359 return (prop = null);
361 return (prop != null ? prop.Val : null);
364 public object GetProp (int pos, MSymbol key, out MTextProperty[] array)
366 check_pos (pos, false);
368 MInterval i = (MInterval) intervals.Find (key).Val;
371 return (array = null);
372 MTextProperty prop = i.Get (pos, out array);
373 return (prop != null ? prop.Val : null);
376 public void PushProp (int from, int to, MSymbol key, object val)
378 if (! check_range (from, to, true))
379 PushProp (from, to, new MTextProperty (key, val));
382 public void PushProp (int from, int to, MTextProperty prop)
386 if (default_property == null)
387 default_property = new MPlist ();
388 default_property.Push (prop.key, prop.val);
392 if (check_range (from, to, true))
395 MPlist p = intervals.Find (prop.key);
400 root = new MInterval (prop.key, this);
401 intervals.Push (prop.key, root);
404 root = (MInterval) p.Val;
406 root.Push (from, to, prop);
410 public void PopProp (int from, int to, MSymbol key)
414 if (default_property == null)
416 MPlist p = default_property.Find (key);
423 if (check_range (from, to, true))
426 MPlist p = intervals.Find (key);
429 ((MInterval) p.Val).Pop (from, to);
433 public void DumpProp ()
436 foreach (MPlist p in intervals)
437 ((MInterval) p.Val).Dump ();
438 Console.WriteLine (")");
441 private class MInterval
443 // position: 0 1 2 3 4 5 6 7
444 // | A | B | C | D | E F | G |
445 // interval |---|---|---|<->|-------|---|
446 // |---|<->|---| |<----->|---|
450 // [3 (1 2)] [3 (4 6)]
451 // [1 (0 1)] [2 (2 3)] [1 (6 7)]
453 private static int count = 0;
455 private int total_length;
456 private int from, to;
458 private MPlist stack;
459 private MInterval left, right, parent;
462 public MInterval (MSymbol key, int length)
465 throw new Exception ("Invalid interval length");
467 total_length = length;
468 stack = new MPlist ();
472 public MInterval (MSymbol key, MText mt)
476 total_length = mt.sb.Length;
479 stack = new MPlist ();
483 public MTextProperty Get (int pos)
485 MInterval i = find (pos);
487 return (i.stack.IsEmpty ? null : (MTextProperty) i.stack.Val);
490 public MTextProperty Get (int pos, out MTextProperty[] array)
492 MInterval i = find (pos);
499 array = new MTextProperty[i.stack.Count];
503 for (idx = 0, p = i.stack; ! p.IsEmpty; idx++, p = p.Next)
504 array[idx] = (MTextProperty) p.Val;
505 return array[idx - 1];
508 private MInterval (MSymbol key, int length, MPlist stack)
511 total_length = length;
514 this.stack = stack.Clone ();
518 private void update_from_to ()
523 to = total_length - RightLength;
525 else if (parent.left == this)
527 from = parent.from - total_length + LeftLength;
528 to = parent.from - RightLength;
532 from = parent.to + LeftLength;
533 to = parent.to + total_length - RightLength;
537 private int LeftLength
539 get { return (left == null ? 0 : left.total_length); }
542 private int RightLength
544 get { return (right == null ? 0 : right.total_length); }
547 private MInterval left_most_node
549 get { return (left == null ? this : left.left_most_node); }
552 private MInterval right_most_node
554 get { return (right == null ? this : right.right_most_node); }
557 private MInterval prev {
562 for (i = left; i.right != null; i = i.right);
564 for (i = parent; i != null && i.left == null; i = i.parent);
569 private MInterval next {
574 for (i = right; i.left != null; i = i.left);
576 for (i = parent; i != null && i.right == null; i = i.parent);
581 private MInterval find (int pos)
585 return left.find (pos);
587 return right.find (pos);
591 // p-. or .-p p-. or .-p
592 // .-this-. .-right-.
593 // left .-right-. -> .-this-. c2
595 private MInterval promote_right ()
597 int right_length = right.total_length;
601 mtext.intervals.Put (key, right);
602 else if (parent.left == this)
605 parent.right = right;
606 right.parent = parent;
612 parent.total_length += total_length;
613 total_length -= right_length;
617 parent.total_length -= c1.total_length;
618 total_length += c1.total_length;
623 // p-. or .-p p-. or .-p
625 // .-left-. .-right-. -> c1 .-this-.
627 private MInterval promote_left ()
629 int left_length = left.total_length;
633 mtext.intervals.Put (key, left);
634 else if (parent.left == this)
638 left.parent = parent;
644 parent.total_length += total_length;
645 total_length -= left_length;
649 parent.total_length -= c1.total_length;
650 total_length += c1.total_length;
655 private MInterval balance ()
662 // .-left-. .-right-.
664 int diff = i.LeftLength - i.RightLength;
669 new_diff = (i.total_length - i.LeftLength
670 + i.left.RightLength - i.left.LeftLength);
671 if (Math.Abs (new_diff) >= diff)
673 i = i.promote_left ();
678 new_diff = (i.total_length - i.RightLength
679 + i.right.LeftLength - i.right.RightLength);
680 if (Math.Abs (new_diff) >= diff)
682 i = i.promote_right ();
689 public MInterval copy (int start, int end)
691 MInterval this_copy, left_copy = null, right_copy = null;
697 return left.copy (start, end);
698 left_copy = left.copy (start, from);
703 return right.copy (start, end);
704 right_copy = right.copy (to, end);
706 this_copy = new MInterval (key, end - start, stack);
707 this_copy.left = left_copy;
708 this_copy.right = right_copy;
716 private MInterval divide_right (int pos)
718 MInterval interval = new MInterval (key, to - pos, stack);
720 Console.Write ("divide-right({0}) at ", pos); DumpOne (false, true);
724 interval.right = right;
725 right.parent = interval;
726 interval.total_length += right.total_length;
728 interval.parent = this;
736 private MInterval divide_left (int pos)
738 MInterval interval = new MInterval (key, pos - from, stack);
740 Console.Write ("divide-left({0}) at ", pos); DumpOne (false, true);
744 interval.left = left;
745 left.parent = interval;
746 interval.total_length += left.total_length;
748 interval.parent = this;
753 private void remove_properties (MTextProperty.Flag flags)
755 for (MPlist p = stack; ! p.IsEmpty;)
757 MTextProperty prop = (MTextProperty) p.Val;
759 if ((prop.flags & flags) == flags)
766 private void merge_front_properties (MPlist plist)
768 for (MInterval i = left_most_node; i != null; i = i.next)
772 for (MPlist p = plist; ! p.IsEmpty; p = p.Next)
774 MTextProperty prop = (MTextProperty) p.Val;
776 if ((prop.flags & MTextProperty.Flag.RearSticky)
777 == MTextProperty.Flag.RearSticky)
778 i.stack.Add (prop.key, prop);
783 private void merge_rear_properties (MPlist plist)
785 for (MInterval i = right_most_node; i != null; i = i.prev)
789 for (MPlist p = plist; ! p.IsEmpty; p = p.Next)
791 MTextProperty prop = (MTextProperty) p.Val;
793 if ((prop.flags & MTextProperty.Flag.FrontSticky)
794 == MTextProperty.Flag.FrontSticky)
795 i.stack.Add (prop.key, prop);
800 public void Insert (int pos, MInterval interval)
803 Console.Write ("insert({0}) at ", pos); DumpOne (false, true);
804 if (pos < from || (pos == from && left == null && pos > 0))
806 prev.Insert (pos, interval);
809 if (pos > to || (pos == to && right == null && next != null))
811 next.Insert (pos, interval);
814 if (pos > from && pos < to)
816 remove_properties (MTextProperty.Flag.Sensitive);
817 divide_right (pos).Insert (pos, interval);
824 prev.remove_properties
825 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.RearSticky);
826 interval.merge_front_properties (prev.stack);
829 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.FrontSticky);
830 interval.merge_rear_properties (stack);
832 // INTERVAL is ready to insert.
834 // .-this-. ==> .-this-.
841 MInterval i = left.right_most_node;
845 for (; i != null; i = i.parent)
846 i.total_length += interval.total_length;
852 for (MInterval i = this; i != null; i = i.parent)
853 i.total_length += interval.total_length;
860 MInterval left_most = right.left_most_node;
862 left_most.remove_properties
863 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.FrontSticky);
864 interval.merge_rear_properties (left_most.stack);
867 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.RearSticky);
868 interval.merge_front_properties (stack);
870 // INTERVAL is ready to insert.
872 // .-this-. ==> .-this-.
879 MInterval i = right.left_most_node;
883 for (; i != null; i = i.parent)
884 i.total_length += interval.total_length;
890 for (MInterval i = this; i != null; i = i.parent)
891 i.total_length += interval.total_length;
896 private void vacate_node (MInterval interval)
898 Console.WriteLine ("vacate #{0} to #{1}", id, interval.id);
899 if (interval != null)
900 interval.parent = parent;
903 mtext.intervals.Put (key, interval);
907 if (this == parent.right)
908 parent.right = interval;
910 parent.left = interval;
912 int diff = total_length;
913 if (interval != null)
914 diff -= interval.total_length;
915 for (MInterval i = parent; i != null; i = i.parent)
916 i.total_length -= diff;
920 public void Delete (int start, int end)
923 Console.Write ("delete({0} {1}) at ", start, end); DumpOne (false, true);
928 left.Delete (start, end);
931 left.Delete (start, from);
940 right.Delete (start, end);
943 right.Delete (to, end);
946 if (start == from && end == to)
958 for (i = right; i.left != null; i = i.left)
959 i.total_length += left.total_length;
960 i.total_length += left.total_length;
969 int len = end - start;
971 for (MInterval i = this; i != null; i = i.parent)
972 i.total_length -= len;
976 public void Push (int start, int end, MTextProperty prop)
979 Console.Write ("push({0} {1}) at ", start, end); DumpOne (false, true);
984 left.Push (start, end, prop);
987 left.Push (start, from, prop);
994 right.Push (start, end, prop);
997 right.Push (to, end, prop);
1002 divide_left (start);
1005 stack.Push (prop.key, prop);
1008 public void Pop (int start, int end)
1011 Console.Write ("pop({0} {1}) at ", start, end); DumpOne (false, true);
1016 left.Pop (start, end);
1019 left.Pop (start, from);
1026 right.Pop (start, end);
1029 right.Pop (to, end);
1033 if (! stack.IsEmpty)
1036 divide_left (start);
1043 private void DumpOne (bool with_prop, bool newline)
1045 Console.Write ("#{0}({1} {2} {3}", id, total_length, from, to);
1047 foreach (MPlist p in stack)
1048 Console.Write (" " + p.Val);
1049 Console.Write (")");
1051 Console.WriteLine ();
1061 Console.Write (" ");
1062 DumpOne (true, false);
1068 private class MTextEnum : IEnumerator
1071 private int pos = -1;
1073 public MTextEnum (MText mt)
1078 public bool MoveNext ()
1081 return (pos < mt.nchars);
1084 public void Reset ()
1089 public object Current
1092 //if (pos < 0 || pos >= mt.nchars)
1093 //throw new InvalidOperationException ();