*** empty log message ***
[m17n/m17n-lib-cs.git] / MText.cs
index 4264023..fcf4a58 100644 (file)
--- a/MText.cs
+++ b/MText.cs
@@ -275,6 +275,30 @@ namespace M17N.Core
        }
     }
 
+    private void insert (int pos, int c)
+    {
+      check_pos (pos, true);
+
+      int pos_idx = pos_to_idx (this, pos);
+
+      if (c < 0x10000)
+       {
+         char ch = (char) c;
+         sb.Insert (pos_idx, ch);
+       }
+      else
+       {
+         char high = (char) (0xD800 + ((c - 0x10000) >> 10));
+         char low = (char) (0xDC00 + ((c - 0x10000) & 0x3FF));
+         sb.Insert (pos_idx, low);
+         sb.Insert (pos_idx, high);
+       }
+      nchars++;
+      foreach (MPlist plist in intervals)
+       ((MInterval) plist.Val).Insert (pos,
+                                       new MInterval (plist.Key, this, 1));
+    }
+
     public int this[int i]
     {
       set {
@@ -306,7 +330,17 @@ namespace M17N.Core
 
     public MText Dup ()
     {
-      return (new MText (sb.ToString ()));
+      MText mt = new MText (sb.ToString ());
+
+      foreach (MPlist p in intervals)
+       mt.intervals.Add (p.Key, ((MInterval) p.Val).Copy (0, Length));
+      return mt;
+    }
+
+    public MText Ins (int pos, int c)
+    {
+      insert (pos, c);
+      return this;
     }
 
     public MText Ins (int pos, MText mt)
@@ -321,6 +355,12 @@ namespace M17N.Core
       return this;
     }
 
+    public MText Cat (int c)
+    {
+      insert (nchars, c);
+      return this;
+    }
+
     public MText Del (int from, int to)
     {
       if (check_range (from, to, true))
@@ -341,8 +381,7 @@ namespace M17N.Core
     {
       check_pos (pos, false);
 
-      MInterval i = (MInterval) intervals.Find (key).Val;
-
+      MInterval i = (MInterval) intervals.Get (key);
       if (i == null)
        return null;
 
@@ -354,8 +393,7 @@ namespace M17N.Core
     {
       check_pos (pos, false);
 
-      MInterval i = (MInterval) intervals.Find (key).Val;
-
+      MInterval i = (MInterval) intervals.Get (key);
       if (i == null)
        return (prop = null);
       prop = i.Get (pos);
@@ -366,8 +404,7 @@ namespace M17N.Core
     {
       check_pos (pos, false);
 
-      MInterval i = (MInterval) intervals.Find (key).Val;
-
+      MInterval i = (MInterval) intervals.Get (key);
       if (i == null)
        return (array = null);
       MTextProperty prop = i.Get (pos, out array);
@@ -439,6 +476,12 @@ namespace M17N.Core
       Console.WriteLine (")");
     }
 
+    public void DumpPropNested ()
+    {
+      foreach (MPlist p in intervals)
+       ((MInterval) p.Val).DumpNested (true);
+    }
+
     private class MInterval
     {
       // position: 0   1   2   3   4   5   6   7
@@ -713,13 +756,14 @@ namespace M17N.Core
        MInterval copy, left_copy = null, right_copy = null;
 
        update_from_to ();
+
        if (start < From)
          {
            if (end <= From)
              return Left.Copy (start, end);
            left_copy = Left.Copy (start, From);
          }
-       else if (end > To)
+       if (end > To)
          {
            if (start >= To)
              return Right.Copy (start, end);
@@ -728,9 +772,16 @@ namespace M17N.Core
 
        copy = new MInterval (Key, null, end - start, Stack);
        remove_properties (MTextProperty.Flag.Sensitive);
-       copy.Left = left_copy;
-       copy.Right = right_copy;
-
+       if (left_copy != null)
+         {
+           copy.Left = left_copy;
+           left_copy.Parent = copy;
+         }
+       if (right_copy != null)
+         {
+           copy.Right = right_copy;
+           right_copy.Parent = copy;
+         }
        return copy;
       }
 
@@ -1164,11 +1215,15 @@ namespace M17N.Core
        if (! mergeable (prev))
          return false;
 
+       M17N.DebugPrint ("merging "); DumpOne (false, false);
+       M17N.DebugPrint (" with prev "); prev.DumpOne (false, true);
        int len = prev.Length - prev.LeftLength;
 
        // PREV is Left, Left.Right, ..., or Left....Right.
        if (prev != Left)
          {
+           if (prev.Left != null)
+             prev.Left.Parent = prev.Parent;
            prev.Parent.Right = prev.Left;
            while (prev.Parent != Left)
              {
@@ -1178,7 +1233,11 @@ namespace M17N.Core
          }
        Left.Length -= len;
        if (Left.Length == Left.LeftLength)
-         Left = Left.Left;
+         {
+           if (Left.Left != null)
+             Left.Left.Parent = this;
+           Left = Left.Left;
+         }
        return true;
       }
 
@@ -1189,11 +1248,16 @@ namespace M17N.Core
        if (! mergeable (next))
          return false;
 
-       int len = next.Length - next.LeftLength;
+       M17N.DebugPrint ("merging "); DumpOne (false, false);
+       M17N.DebugPrint (" with next "); next.DumpOne (false, true);
+
+       int len = next.Length - next.RightLength;
 
        // NEXT is Right, Right.Left, ..., or Right....Left.
        if (next != Right)
          {
+           if (next.Right != null)
+             next.Right.Parent = next.Parent;
            next.Parent.Left = next.Right;
            while (next.Parent != Right)
              {
@@ -1202,9 +1266,12 @@ namespace M17N.Core
              }
          }
        Right.Length -= len;
-       if (Right.Length == Right.LeftLength)
-         Right = Right.Left;
-
+       if (Right.Length == Right.RightLength)
+         {
+           if (Right.Right != null)
+             Right.Right.Parent = this;
+           Right = Right.Right;
+         }
        return true;
       }
 
@@ -1276,12 +1343,13 @@ namespace M17N.Core
            Console.Write (")");
            if (newline)
              Console.WriteLine ();
+           if (Length <= 0)
+             throw new Exception ("Invalid interval length");
          }
       }
 
       public void Dump () { Dump (false); }
 
-
       public void Dump (bool force)
       {
        if (force || M17N.debug)
@@ -1297,6 +1365,45 @@ namespace M17N.Core
              Right.Dump (force);
          }
       }
+
+      private int Depth {
+       get { return (Parent == null ? 0 : Parent.Depth + 1); }
+      }
+
+      public void DumpNested (bool force)
+      {
+       DumpNested ("", force);
+      }
+
+      public void DumpNested (string indent, bool force)
+      {
+       if (force || M17N.debug)
+         {
+           int indent_type = (Parent == null ? 1
+                              : Parent.Left == this ? 0 : 2);
+
+           update_from_to ();
+           if (Left != null)
+             {
+               if (indent_type <= 1)
+                 Left.DumpNested (indent + "  ", force);
+               else
+                 Left.DumpNested (indent + "| ", force);
+             }
+           if (indent_type == 0)
+             Console.Write (indent + ".-");
+           else if (indent_type == 2)
+             Console.Write (indent + "`-");
+           DumpOne (true, true);
+           if (Right != null)
+             {
+               if (indent_type >= 1)
+                 Right.DumpNested (indent + "  ", force);
+               else if (indent_type == 2)
+                 Right.DumpNested (indent + "| ", force);
+             }
+         }
+      }
     }
 
     private class MTextEnum : IEnumerator