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 MText mt = new MText (sb.ToString ());
335 foreach (MPlist p in intervals)
336 mt.intervals.Add (p.Key, ((MInterval) p.Val).Copy (0, Length));
340 public MText Ins (int pos, int c)
346 public MText Ins (int pos, MText mt)
348 insert (pos, mt, 0, mt.nchars);
352 public MText Ins (int pos, MText mt, int from, int to)
354 insert (pos, mt, from, to);
358 public MText Cat (int c)
364 public MText Del (int from, int to)
366 if (check_range (from, to, true))
369 sb.Remove (from, pos_to_idx (this, to) - pos_to_idx (this, from));
373 foreach (MPlist plist in intervals)
374 ((MInterval) plist.Val).Delete (from, to);
376 intervals = new MPlist ();
380 public object GetProp (int pos, MSymbol key)
382 check_pos (pos, false);
384 MInterval i = (MInterval) intervals.Get (key);
388 MTextProperty prop = i.Get (pos);
389 return (prop != null ? prop.Val : null);
392 public object GetProp (int pos, MSymbol key, out MTextProperty prop)
394 check_pos (pos, false);
396 MInterval i = (MInterval) intervals.Get (key);
398 return (prop = null);
400 return (prop != null ? prop.Val : null);
403 public object GetProp (int pos, MSymbol key, out MTextProperty[] array)
405 check_pos (pos, false);
407 MInterval i = (MInterval) intervals.Get (key);
409 return (array = null);
410 MTextProperty prop = i.Get (pos, out array);
411 return (prop != null ? prop.Val : null);
414 public void PushProp (int from, int to, MSymbol key, object val)
416 if (! check_range (from, to, true))
417 PushProp (from, to, new MTextProperty (key, val));
420 public void PushProp (int from, int to, MTextProperty prop)
424 if (default_property == null)
425 default_property = new MPlist ();
426 default_property.Push (prop.key, prop.val);
430 if (check_range (from, to, true))
433 MPlist p = intervals.Find (prop.key);
438 root = new MInterval (prop.key, this);
439 intervals.Push (prop.key, root);
442 root = (MInterval) p.Val;
444 root.Push (from, to, prop);
448 public void PopProp (int from, int to, MSymbol key)
452 if (default_property == null)
454 MPlist p = default_property.Find (key);
461 if (check_range (from, to, true))
464 MPlist p = intervals.Find (key);
468 MInterval root = (MInterval) p.Val;
471 root.MergeAfterChange (from, to);
476 public void DumpProp ()
479 foreach (MPlist p in intervals)
480 ((MInterval) p.Val).Dump (true);
481 Console.WriteLine (")");
484 public void DumpPropNested ()
486 foreach (MPlist p in intervals)
487 ((MInterval) p.Val).DumpNested (true);
490 private class MInterval
492 // position: 0 1 2 3 4 5 6 7
493 // | A | B | C | D | E F | G |
494 // interval |---|---|---|<->|-------|---|
495 // |---|<->|---| |<----->|---|
499 // [3 (1 2)] [3 (4 6)]
500 // [1 (0 1)] [2 (2 3)] [1 (6 7)]
502 private static int count = 0;
505 private int From, To;
507 private MPlist Stack;
508 private MInterval Left, Right, Parent;
511 public MInterval (MSymbol key, MText mt, int length)
514 throw new Exception ("Invalid interval length");
518 Stack = new MPlist ();
522 public MInterval (MSymbol key, MText mt)
526 Length = mt.sb.Length;
529 Stack = new MPlist ();
533 public MTextProperty Get (int pos)
535 MInterval i = find (pos);
537 return (i.Stack.IsEmpty ? null : (MTextProperty) i.Stack.Val);
540 public MTextProperty Get (int pos, out MTextProperty[] array)
542 MInterval i = find (pos);
549 array = new MTextProperty[i.Stack.Count];
553 for (idx = 0, p = i.Stack; ! p.IsEmpty; idx++, p = p.Next)
554 array[idx] = (MTextProperty) p.Val;
558 private MInterval (MSymbol key, MText mt, int length, MPlist stack)
565 Stack = stack.Clone ();
569 private void update_from_to ()
574 To = Length - RightLength;
576 else if (Parent.Left == this)
578 From = Parent.From - Length + LeftLength;
579 To = Parent.From - RightLength;
583 From = Parent.To + LeftLength;
584 To = Parent.To + Length - RightLength;
588 private int LeftLength
590 get { return (Left == null ? 0 : Left.Length); }
593 private int RightLength
595 get { return (Right == null ? 0 : Right.Length); }
598 private MInterval LeftMost
600 get { return (Left == null ? this : Left.LeftMost); }
603 private MInterval RightMost
605 get { return (Right == null ? this : Right.RightMost); }
608 private MInterval Prev {
613 for (i = Left; i.Right != null; i = i.Right);
616 MInterval child = this;
617 for (i = Parent; i != null && i.Left == child;
618 child = i, i = i.Parent);
624 private MInterval Next {
629 for (i = Right; i.Left != null; i = i.Left);
632 MInterval child = this;
633 for (i = Parent; i != null && i.Right == child;
634 child = i, i = i.Parent);
640 private MInterval find (int pos)
644 return Left.find (pos);
646 return Right.find (pos);
650 private bool mergeable (MInterval i)
654 for (p1 = Stack, p2 = i.Stack; ! p1.IsEmpty && ! p2.IsEmpty;
655 p1 = p1.Next, p2 = p2.Next)
656 if (p1.Val != p2.Val)
658 return (p1.IsEmpty && p2.IsEmpty);
661 // p-. or .-p p-. or .-p
662 // .-this-. .-right-.
663 // left .-right-. -> .-this-. c2
665 private MInterval promote_right ()
667 int right_length = Right.Length;
671 mtext.intervals.Put (Key, Right);
672 else if (Parent.Left == this)
675 Parent.Right = Right;
676 Right.Parent = Parent;
682 Parent.Length += Length;
683 Length -= right_length;
687 Parent.Length -= c1.Length;
693 // p-. or .-p p-. or .-p
695 // .-left-. .-right-. -> c1 .-this-.
697 private MInterval promote_left ()
699 int left_length = Left.Length;
703 mtext.intervals.Put (Key, Left);
704 else if (Parent.Left == this)
708 Left.Parent = Parent;
714 Parent.Length += Length;
715 Length -= left_length;
719 Parent.Length -= c1.Length;
725 private MInterval balance ()
732 // .-left-. .-right-.
734 int diff = i.LeftLength - i.RightLength;
739 new_diff = (i.Length - i.LeftLength
740 + i.Left.RightLength - i.Left.LeftLength);
741 if (Math.Abs (new_diff) >= diff)
743 i = i.promote_left ();
748 new_diff = (i.Length - i.RightLength
749 + i.Right.LeftLength - i.Right.RightLength);
750 if (Math.Abs (new_diff) >= diff)
752 i = i.promote_right ();
759 public MInterval Copy (int start, int end)
761 MInterval copy, left_copy = null, right_copy = null;
768 return Left.Copy (start, end);
769 left_copy = Left.Copy (start, From);
774 return Right.Copy (start, end);
775 right_copy = Right.Copy (To, end);
778 copy = new MInterval (Key, null, end - start, Stack);
779 remove_properties (MTextProperty.Flag.Sensitive);
780 if (left_copy != null)
782 copy.Left = left_copy;
783 left_copy.Parent = copy;
785 if (right_copy != null)
787 copy.Right = right_copy;
788 right_copy.Parent = copy;
796 private MInterval divide_right (int pos)
798 MInterval interval = new MInterval (Key, mtext, To - pos, Stack);
800 M17N.DebugPrint ("divide-right({0}) at ", pos); DumpOne (false, true);
804 interval.Right = Right;
805 Right.Parent = interval;
806 interval.Length += Right.Length;
808 interval.Parent = this;
816 private MInterval divide_left (int pos)
818 MInterval interval = new MInterval (Key, mtext, pos - From, Stack);
820 M17N.DebugPrint ("divide-left({0}) at ", pos); DumpOne (false, true);
824 interval.Left = Left;
825 Left.Parent = interval;
826 interval.Length += Left.Length;
828 interval.Parent = this;
833 private void remove_properties (MTextProperty.Flag flags)
835 for (MPlist p = Stack; ! p.IsEmpty;)
837 MTextProperty prop = (MTextProperty) p.Val;
839 if ((prop.flags & flags) == flags)
846 private void inherit_front_properties (MPlist plist)
848 for (MInterval i = LeftMost; i != null; i = i.Next)
852 for (MPlist p = plist; ! p.IsEmpty; p = p.Next)
854 MTextProperty prop = (MTextProperty) p.Val;
856 if ((prop.flags & MTextProperty.Flag.RearSticky)
857 == MTextProperty.Flag.RearSticky)
858 i.Stack.Add (prop.key, prop);
863 private void inherit_rear_properties (MPlist plist)
865 for (MInterval i = RightMost; i != null; i = i.Prev)
869 for (MPlist p = plist; ! p.IsEmpty; p = p.Next)
871 MTextProperty prop = (MTextProperty) p.Val;
873 if ((prop.flags & MTextProperty.Flag.FrontSticky)
874 == MTextProperty.Flag.FrontSticky)
875 i.Stack.Add (prop.key, prop);
880 private MInterval delete_node_forward ()
884 int len = Length - RightLength;
886 for (MInterval i = Parent; i != null; i = i.Parent)
888 if (Parent.Left == this)
891 Parent.Right = Right;
896 Right.Parent = Parent;
897 return Right.LeftMost;
902 private MInterval delete_node_backward ()
906 int len = Length - RightLength;
908 for (MInterval i = Parent; i != null; i = i.Parent)
910 if (Parent.Left == this)
918 Left.Parent = Parent;
919 return Left.RightMost;
924 private void set_mtext (MText mt)
930 Right.set_mtext (mt);
933 private MInterval graft (MInterval interval, bool forward, out int len)
940 i = interval.LeftMost;
945 len += i.Length - i.RightLength;
946 i = i.delete_node_forward ();
951 i = interval.RightMost;
956 len += i.Length - i.LeftLength;
957 i = i.delete_node_backward ();
963 for (MInterval prev = this, ii = this.Parent; ii != null;
964 prev = ii, ii = ii.Parent)
974 while (i.Parent != null) i = i.Parent;
978 public void Insert (int pos, MInterval interval)
981 M17N.DebugPrint ("insert({0}) at {1} in ", interval.Length, pos);
982 DumpOne (false, true);
984 interval.set_mtext (mtext);
987 Prev.Insert (pos, interval);
988 else if (pos == From)
990 MInterval prev = Prev;
996 prev.Insert (pos, interval);
999 prev.remove_properties
1000 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.RearSticky);
1001 interval.inherit_front_properties (prev.Stack);
1004 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.FrontSticky);
1005 interval.inherit_rear_properties (Stack);
1008 interval = graft (interval, false, out len);
1009 if (interval != null && prev != null)
1010 interval = prev.graft (interval, true, out len);
1011 if (interval != null)
1017 // .-this-. ==> .-this-.
1029 interval.Parent = i;
1030 for (; i != null; i = i.Parent)
1031 i.Length += interval.Length;
1036 remove_properties (MTextProperty.Flag.Sensitive);
1039 interval = graft (interval, true, out len);
1041 if (interval != null)
1042 interval = graft (interval, false, out len);
1043 if (interval != null)
1046 Right.Left = interval;
1047 interval.Parent = Right;
1048 for (MInterval i = Right; i != null; i = i.Parent)
1049 i.Length += interval.Length;
1054 MInterval next = Next;
1060 next.Insert (pos, interval);
1063 next.remove_properties
1064 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.FrontSticky);
1065 interval.inherit_rear_properties (next.Stack);
1068 (MTextProperty.Flag.Sensitive|MTextProperty.Flag.RearSticky);
1069 interval.inherit_front_properties (Stack);
1072 interval = graft (interval, true, out len);
1073 if (interval != null && next != null)
1074 interval = next.graft (interval, false, out len);
1075 if (interval != null)
1081 // .-this-. ==> .-this-.
1094 interval.Parent = i;
1095 for (; i != null; i = i.Parent)
1096 i.Length += interval.Length;
1100 Next.Insert (pos, interval);
1103 private void vacate_node (MInterval interval)
1105 vacate_node (interval, null);
1108 private void vacate_node (MInterval interval, MInterval stop)
1110 if (interval != null)
1111 M17N.DebugPrint ("vacate #{0} to #{1}\n", ID, interval.ID);
1113 M17N.DebugPrint ("vacate #{0} to null\n", ID);
1114 if (interval != null)
1115 interval.Parent = Parent;
1119 mtext.intervals.Put (Key, interval);
1123 if (this == Parent.Right)
1124 Parent.Right = interval;
1126 Parent.Left = interval;
1129 if (interval != null)
1130 diff -= interval.Length;
1131 for (MInterval i = Parent; i != stop; i = i.Parent)
1136 public void Delete (int start, int end)
1139 M17N.DebugPrint ("delete({0} {1}) from ", start, end); DumpOne (false, true);
1144 Left.Delete (start, end);
1147 Left.Delete (start, From);
1149 end -= From - start;
1156 Right.Delete (start, end);
1159 Right.Delete (To, end);
1162 if (start == From && end == To)
1174 for (i = Right; i.Left != null; i = i.Left)
1175 i.Length += Left.Length;
1176 i.Length += Left.Length;
1180 vacate_node (Right);
1185 int len = end - start;
1187 for (MInterval i = this; i != null; i = i.Parent)
1192 public void Push (int start, int end, MTextProperty prop)
1195 M17N.DebugPrint ("push({0} {1}) at ", start, end); DumpOne (false, true);
1200 Left.Push (start, end, prop);
1203 Left.Push (start, From, prop);
1210 Right.Push (start, end, prop);
1213 Right.Push (To, end, prop);
1218 divide_left (start);
1221 Stack.Push (prop.key, prop);
1224 private static void merge_nodes (MInterval head, MInterval tail)
1226 M17N.DebugPrint ("merging "); head.DumpOne (true, false);
1227 M17N.DebugPrint (" through "); tail.DumpOne (true, false);
1229 int from = head.From;
1233 for (root = head; root.To + head.RightLength < to;
1234 root = root.Parent);
1236 M17N.DebugPrint (" common root is "); root.DumpOne (false, true);
1238 if (from < root.From)
1240 MInterval prev = root.Prev;
1244 M17N.DebugPrint ("merging "); prev.DumpOne (false, true);
1245 prev.vacate_node (prev.Left, root);
1248 if (prev.Left != null)
1249 prev = prev.Left.RightMost;
1256 MInterval next = root.Next;
1260 M17N.DebugPrint ("merging "); next.DumpOne (false, true);
1261 next.vacate_node (next.Right, root);
1264 if (next.Right != null)
1265 next = next.Right.LeftMost;
1272 public void MergeAfterChange (int start, int end)
1277 Prev.MergeAfterChange (start, end);
1281 MInterval head = this, tail = this, i;
1283 if (start == From && start > 0)
1289 while (tail.To < end)
1292 if (! tail.mergeable (i))
1295 merge_nodes (head, tail);
1303 if (i == null || ! tail.mergeable (i))
1308 merge_nodes (head, tail);
1311 public void Pop (int start, int end)
1314 M17N.DebugPrint ("pop({0} {1}) at ", start, end); DumpOne (false, true);
1319 Left.Pop (start, end);
1322 Left.Pop (start, From);
1329 Right.Pop (start, end);
1332 Right.Pop (To, end);
1336 if (! Stack.IsEmpty)
1339 divide_left (start);
1346 private void DumpOne (bool with_prop, bool newline)
1348 DumpOne (with_prop, newline, false);
1351 private void DumpOne (bool with_prop, bool newline, bool force)
1353 if (force || M17N.debug)
1355 Console.Write ("#{0}({1} {2} {3}", ID, Length, From, To);
1357 foreach (MPlist p in Stack)
1358 Console.Write (" " + p.Val);
1359 Console.Write (")");
1361 Console.WriteLine ();
1363 throw new Exception ("Invalid interval length");
1367 public void Dump () { Dump (false); }
1369 public void Dump (bool force)
1371 if (force || M17N.debug)
1378 Console.Write (" ");
1379 DumpOne (true, false, force);
1386 get { return (Parent == null ? 0 : Parent.Depth + 1); }
1389 public void DumpNested (bool force)
1391 DumpNested ("", force);
1394 public void DumpNested (string indent, bool force)
1396 if (force || M17N.debug)
1398 int indent_type = (Parent == null ? 1
1399 : Parent.Left == this ? 0 : 2);
1404 if (indent_type <= 1)
1405 Left.DumpNested (indent + " ", force);
1407 Left.DumpNested (indent + "| ", force);
1409 if (indent_type == 0)
1410 Console.Write (indent + ".-");
1411 else if (indent_type == 2)
1412 Console.Write (indent + "`-");
1413 DumpOne (true, true, true);
1416 if (indent_type >= 1)
1417 Right.DumpNested (indent + " ", force);
1419 Right.DumpNested (indent + "| ", force);
1425 private class MTextEnum : IEnumerator
1428 private int pos = -1;
1430 public MTextEnum (MText mt)
1435 public bool MoveNext ()
1438 return (pos < mt.nchars);
1441 public void Reset ()
1446 public object Current
1449 //if (pos < 0 || pos >= mt.nchars)
1450 //throw new InvalidOperationException ();