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 void 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");
249 private void insert (int pos, MText mt2, int from, int to)
251 check_pos (pos, true);
253 int pos_idx = pos_to_idx (this, pos);
254 int from_idx = pos_to_idx (mt2, from);
255 int to_idx = pos_to_idx (mt2, to);
257 sb.Insert (pos_idx, mt2.sb.ToString (from_idx, to_idx - from_idx));
260 foreach (MPlist plist in mt2.intervals)
261 if (intervals.Find (plist.Key) == null)
262 intervals.Push (plist.Key, new MInterval (plist.Key, this));
263 foreach (MPlist plist in intervals)
265 MPlist p = mt2.intervals.Find (plist.Key);
269 interval = new MInterval (plist.Key, to - from);
271 interval = ((MInterval) p.Val).copy (from, to);
272 ((MInterval) plist.Val).Insert (pos, interval);
276 public int this[int i]
279 i = pos_to_idx (this, i);
282 if (surrogate_high_p (sb[i]))
284 sb[i] = (char) value;
288 char high = (char) (0xD800 + ((value - 0x10000) >> 10));
289 char low = (char) (0xDC00 + ((value - 0x10000) & 0x3FF));
291 if (! surrogate_high_p (sb[i]))
298 i = pos_to_idx (this, i);
299 return (surrogate_high_p (sb[i])
300 ? ((sb[i] - 0xD800) << 10) + (sb[i + 1] - 0xDC00) + 0x10000
307 return (new MText (sb.ToString ()));
310 public MText Ins (int pos, MText mt)
312 insert (pos, mt, 0, mt.nchars);
316 public MText Ins (int pos, MText mt, int from, int to)
318 insert (pos, mt, from, to);
322 public MText Del (int from, int to)
324 check_range (from, to, true);
326 sb.Remove (from, pos_to_idx (this, to) - pos_to_idx (this, from));
330 foreach (MPlist plist in intervals)
331 ((MInterval) plist.Val).Delete (from, to);
333 intervals = new MPlist ();
337 public object GetProp (int pos, MSymbol key)
339 check_pos (pos, false);
341 MInterval i = (MInterval) intervals.Find (key).Val;
346 MTextProperty prop = i.Get (pos);
347 return (prop != null ? prop.Val : null);
350 public object GetProp (int pos, MSymbol key, out MTextProperty prop)
352 check_pos (pos, false);
354 MInterval i = (MInterval) intervals.Find (key).Val;
357 return (prop = null);
359 return (prop != null ? prop.Val : null);
362 public object GetProp (int pos, MSymbol key, out MTextProperty[] array)
364 check_pos (pos, false);
366 MInterval i = (MInterval) intervals.Find (key).Val;
369 return (array = null);
370 MTextProperty prop = i.Get (pos, out array);
371 return (prop != null ? prop.Val : null);
374 public void PushProp (int from, int to, MSymbol key, object val)
376 check_range (from, to, false);
378 PushProp (from, to, new MTextProperty (key, val));
381 public void PushProp (int from, int to, MTextProperty prop)
385 if (default_property == null)
386 default_property = new MPlist ();
387 default_property.Push (prop.key, prop.val);
391 MInterval root = (MInterval) intervals.Find (prop.key).Val;
395 root = new MInterval (prop.key, this);
396 intervals.Push (prop.key, root);
398 root.Push (from, to, prop);
402 public void DumpProp ()
405 foreach (MPlist p in intervals)
406 ((MInterval) p.Val).Dump ();
407 Console.WriteLine (")");
410 private class MInterval
412 // position: 0 1 2 3 4 5 6 7
413 // | A | B | C | D | E F | G |
414 // interval |---|---|---|<->|-------|---|
415 // |---|<->|---| |<----->|---|
419 // [3 (1 2)] [3 (4 6)]
420 // [1 (0 1)] [2 (2 3)] [1 (6 7)]
422 private static int count = 0;
424 private int total_length;
425 private int from, to;
427 private MPlist stack;
428 private MInterval left, right, parent;
431 public MInterval (MSymbol key, int length)
434 throw new Exception ("Invalid interval length");
436 total_length = length;
437 stack = new MPlist ();
441 public MInterval (MSymbol key, MText mt)
445 total_length = mt.sb.Length;
448 stack = new MPlist ();
452 public MTextProperty Get (int pos)
454 MInterval i = find (pos);
456 return (i.stack.IsEmpty ? null : (MTextProperty) i.stack.Val);
459 public MTextProperty Get (int pos, out MTextProperty[] array)
461 MInterval i = find (pos);
468 array = new MTextProperty[i.stack.Count];
472 for (idx = 0, p = i.stack; ! p.IsEmpty; idx++, p = p.Next)
473 array[idx] = (MTextProperty) p.Val;
474 return array[idx - 1];
477 private MInterval (MSymbol key, int length, MPlist stack)
480 total_length = length;
483 this.stack = stack.Clone ();
487 private void update_from_to ()
492 to = total_length - RightLength;
494 else if (parent.left == this)
496 from = parent.from - total_length + LeftLength;
497 to = parent.from - RightLength;
501 from = parent.to + LeftLength;
502 to = parent.to + total_length - RightLength;
506 private int LeftLength
508 get { return (left == null ? 0 : left.total_length); }
511 private int RightLength
513 get { return (right == null ? 0 : right.total_length); }
516 private MInterval left_most_node
518 get { return (left == null ? this : left.left_most_node); }
521 private MInterval right_most_node
523 get { return (right == null ? this : right.right_most_node); }
526 private MInterval prev {
531 for (i = left; i.right != null; i = i.right);
533 for (i = parent; i != null && i.left == null; i = i.parent);
538 private MInterval next {
543 for (i = right; i.left != null; i = i.left);
545 for (i = parent; i != null && i.right == null; i = i.parent);
550 private MInterval find (int pos)
554 return left.find (pos);
556 return right.find (pos);
560 // p-. or .-p p-. or .-p
561 // .-this-. .-right-.
562 // left .-right-. -> .-this-. c2
564 private MInterval promote_right ()
566 int right_length = right.total_length;
570 mtext.intervals.Put (key, right);
571 else if (parent.left == this)
574 parent.right = right;
575 right.parent = parent;
581 parent.total_length += total_length;
582 total_length -= right_length;
586 parent.total_length -= c1.total_length;
587 total_length += c1.total_length;
592 // p-. or .-p p-. or .-p
594 // .-left-. .-right-. -> c1 .-this-.
596 private MInterval promote_left ()
598 int left_length = left.total_length;
602 mtext.intervals.Put (key, left);
603 else if (parent.left == this)
607 left.parent = parent;
613 parent.total_length += total_length;
614 total_length -= left_length;
618 parent.total_length -= c1.total_length;
619 total_length += c1.total_length;
624 private MInterval balance ()
631 // .-left-. .-right-.
633 int diff = i.LeftLength - i.RightLength;
638 new_diff = (i.total_length - i.LeftLength
639 + i.left.RightLength - i.left.LeftLength);
640 if (Math.Abs (new_diff) >= diff)
642 i = i.promote_left ();
647 new_diff = (i.total_length - i.RightLength
648 + i.right.LeftLength - i.right.RightLength);
649 if (Math.Abs (new_diff) >= diff)
651 i = i.promote_right ();
658 public MInterval copy (int start, int end)
660 MInterval this_copy, left_copy = null, right_copy = null;
666 return left.copy (start, end);
667 left_copy = left.copy (start, from);
672 return right.copy (start, end);
673 right_copy = right.copy (to, end);
675 this_copy = new MInterval (key, end - start, stack);
676 this_copy.left = left_copy;
677 this_copy.right = right_copy;
685 private MInterval divide_right (int pos)
687 MInterval interval = new MInterval (key, to - pos, stack);
689 Console.Write ("divide-right({0}) at ", pos); DumpOne (false, true);
693 interval.right = right;
694 right.parent = interval;
695 interval.total_length += right.total_length;
697 interval.parent = this;
705 private MInterval divide_left (int pos)
707 MInterval interval = new MInterval (key, pos - from, stack);
709 Console.Write ("divide-reft({0}) at ", pos); DumpOne (false, true);
713 interval.left = left;
714 left.parent = interval;
715 interval.total_length += left.total_length;
717 interval.parent = this;
722 private void remove_properties (MTextProperty.Flag flags)
724 for (MPlist p = stack; ! p.IsEmpty;)
726 MTextProperty prop = (MTextProperty) p.Val;
728 if ((prop.flags & flags) == flags)
735 private void merge_front_properties (MPlist plist)
737 for (MInterval i = left_most_node; i != null; i = i.next)
741 for (MPlist p = plist; ! p.IsEmpty; p = p.Next)
743 MTextProperty prop = (MTextProperty) p.Val;
745 if ((prop.flags & MTextProperty.Flag.RearSticky)
746 == MTextProperty.Flag.RearSticky)
747 i.stack.Push (prop.key, prop);
752 private void merge_rear_properties (MPlist plist)
754 for (MInterval i = right_most_node; i != null; i = i.prev)
758 for (MPlist p = plist; ! p.IsEmpty; p = p.Next)
760 MTextProperty prop = (MTextProperty) p.Val;
762 if ((prop.flags & MTextProperty.Flag.FrontSticky)
763 == MTextProperty.Flag.FrontSticky)
764 i.stack.Push (prop.key, prop);
769 public void Insert (int pos, MInterval interval)
772 Console.Write ("insert({0}) at ", pos); DumpOne (false, true);
773 if (pos < from || (pos == from && left == null && pos > 0))
775 prev.Insert (pos, interval);
778 if (pos > to || (pos == to && right == null && next != null))
780 next.Insert (pos, interval);
783 if (pos > from && pos < to)
785 remove_properties (MTextProperty.Flag.Sensitive);
786 divide_right (pos).Insert (pos, interval);
793 prev.remove_properties
794 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.RearSticky);
795 interval.merge_front_properties (prev.stack);
798 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.FrontSticky);
799 interval.merge_rear_properties (stack);
801 // INTERVAL is ready to insert.
803 // .-this-. ==> .-this-.
810 MInterval i = left.right_most_node;
814 for (; i != null; i = i.parent)
815 i.total_length += interval.total_length;
821 for (MInterval i = this; i != null; i = i.parent)
822 i.total_length += interval.total_length;
829 MInterval left_most = right.left_most_node;
831 left_most.remove_properties
832 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.FrontSticky);
833 interval.merge_rear_properties (left_most.stack);
836 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.RearSticky);
837 interval.merge_front_properties (stack);
839 // INTERVAL is ready to insert.
841 // .-this-. ==> .-this-.
848 MInterval i = right.left_most_node;
852 for (; i != null; i = i.parent)
853 i.total_length += interval.total_length;
859 for (MInterval i = this; i != null; i = i.parent)
860 i.total_length += interval.total_length;
865 private void vacate_node (MInterval interval)
867 Console.WriteLine ("vacate #{0} to #{1}", id, interval.id);
868 if (interval != null)
869 interval.parent = parent;
872 mtext.intervals.Put (key, interval);
876 if (this == parent.right)
877 parent.right = interval;
879 parent.left = interval;
881 int diff = total_length;
882 if (interval != null)
883 diff -= interval.total_length;
884 for (MInterval i = parent; i != null; i = i.parent)
885 i.total_length -= diff;
889 public void Delete (int start, int end)
892 Console.Write ("delete({0} {1}) at ", start, end); DumpOne (false, true);
897 left.Delete (start, end);
900 left.Delete (start, from);
909 right.Delete (start, end);
912 right.Delete (to, end);
915 if (start == from && end == to)
927 for (i = right; i.left != null; i = i.left)
928 i.total_length += left.total_length;
929 i.total_length += left.total_length;
938 int len = end - start;
940 for (MInterval i = this; i != null; i = i.parent)
941 i.total_length -= len;
945 public void Push (int start, int end, MTextProperty prop)
948 Console.Write ("push({0} {1}) at ", start, end); DumpOne (false, true);
953 left.Push (start, end, prop);
956 left.Push (start, from, prop);
963 right.Push (start, end, prop);
966 right.Push (to, end, prop);
974 stack.Push (prop.key, prop);
977 private void DumpOne (bool with_prop, bool newline)
979 Console.Write ("#{0}({1} {2} {3}", id, total_length, from, to);
981 foreach (MPlist p in stack)
982 Console.Write (" " + p.Val);
985 Console.WriteLine ();
996 DumpOne (true, false);
1002 private class MTextEnum : IEnumerator
1005 private int pos = -1;
1007 public MTextEnum (MText mt)
1012 public bool MoveNext ()
1015 return (pos < mt.nchars);
1018 public void Reset ()
1023 public object Current
1026 //if (pos < 0 || pos >= mt.nchars)
1027 //throw new InvalidOperationException ();