*** empty log message ***
[m17n/m17n-lib-cs.git] / MText.cs
index 986bad0..05e6fcf 100644 (file)
--- a/MText.cs
+++ b/MText.cs
@@ -25,28 +25,52 @@ namespace M17N.Core
     public enum Flags
     {
       None =           0x00,
-      /// A text inserted before a character that has this property
-      /// inherits this property.  If the text already has properties
-      /// of the same key, they are deleted.
-      FrontSticky =    0x01,
-      /// A text inserted after a character that has this property
-      /// inherits this property.  If the text already has properties
-      /// of the same key, they are deleted.
-      RearSticky =     0x02,
+
+      /// On inserting a text in between two characters, if the
+      /// preceding and following characters have Sticky properties of
+      /// the same key with same values, the inserted text inherits
+      /// those properties.  In that case, properties of the inserted
+      /// text are overriden.
+      Sticky =         0x01,   // 00000001
+
+      /// On inserting a text before a character, if the character has
+      /// FrontSticky properties, the inserted text inherits those
+      /// properties.
+      FrontSticky =    0x03,   // 00000011
+
+      /// On inserting a text after a character, if the character has
+      /// RearSticky properties, the inserted text inherits those
+      /// properties.
+      RearSticky =     0x05,   // 00000101
+
+      /// Like RearSticky, but if the inserted text inherits no
+      /// properties from the preceding character, it inherits
+      /// BothSticky properties from the following character if any.
+      BothSticky =     0x07,   // 00000111
+
       /// This property is deleted from a span of text if the span is
-      /// modified (i.e. a character is changed, a text is inserted,
-      /// some part is deleted).  This propery is also deleted if a
-      /// property of the same key is added, which means that this
-      /// property is not stackable.
-      Sensitive =      0x04,
+      /// modified (i.e. one of a character is changed, a text is
+      /// inserted, some part is deleted).  Here, "span" means a
+      /// sequence of characters that has this property with the same
+      /// value.  This property is also deleted if a property of the
+      /// same key is added, which means that this property is not
+      /// stackable.  In addition this property is never merged with
+      /// the same value of preceding or following property.  At last,
+      /// this property can't be sticky in any way.
+      Sensitive =      0x10,   // 00010000
+
       /// Like Sensitive but also this property is deleted from a span
-      /// of text if a text just before the span is modified,
+      /// of text if a characeter just before the span is modified,
       /// inserted, or deleted.
-      FrontSensitive = 0x08,
+      FrontSensitive = 0x30,   // 00110000
+
       /// Like Sensitive but also this property is deleted from a span
-      /// of text if a text just after the span is modified, inserted,
-      /// or deleted.
-      RearSensitive =  0x10
+      /// of text if a character just after the span is modified,
+      /// inserted, or deleted.
+      RearSensitive =  0x50,   // 01010000
+
+      /// Same as (FrontSensitive | RearSensitive).
+      BothSensitive =  0x70,   // 01110000
     };
 
     internal MSymbol key;
@@ -71,7 +95,7 @@ namespace M17N.Core
 
     public static bool HasFlags (MSymbol key, Flags flags)
     {
-      return ((key.flags & flags) != Flags.None);
+      return ((key.flags & flags) == flags);
     }
 
     public override string ToString ()
@@ -128,6 +152,13 @@ namespace M17N.Core
        intervals = new MPlist ();
       }
 
+    public MText (byte[] str, int offset, int length)
+      {
+       sb = new StringBuilder (utf8.GetString (str, offset, length));
+       nchars = count_chars (sb);
+       intervals = new MPlist ();
+      }
+
     public MText (String str)
       {
        sb = new StringBuilder (str);
@@ -494,10 +525,17 @@ namespace M17N.Core
              intervals.Push (prop.key, root);
            }
          else
-           root = (MInterval) p.Val;
-
-         if (root.isSensitive)
-           root.PopSensitive (from, to);
+           {
+             root = (MInterval) p.Val;
+             if (root.isSensitive)
+               {
+                 root.PopSensitive (from, to);
+                 root.MergeAfterChange (from, to);
+                 root = (MInterval) p.Val;
+                 if (M17n.debug)
+                   DumpPropNested ();
+               }
+           }
          root.Push (from, to, prop);
          root.MergeAfterChange (from, to);
          root.Balance ();
@@ -663,14 +701,6 @@ namespace M17N.Core
                                         MProperty.Flags.RearSensitive) ; }
       }
 
-      public bool isAnySensitive
-      {
-       get { return MProperty.HasFlags (Key,
-                                        (MProperty.Flags.Sensitive
-                                         | MProperty.Flags.RearSensitive
-                                         | MProperty.Flags.FrontSensitive)) ; }
-      }
-
       private void update_from_to ()
       {
        if (Parent == null)
@@ -784,6 +814,10 @@ namespace M17N.Core
       {
        MPlist p1, p2;
 
+       if (Stack.IsEmpty && i.Stack.IsEmpty)
+         return true;
+       if (isSensitive)
+         return false;
        for (p1 = Stack, p2 = i.Stack; ! p1.IsEmpty && ! p2.IsEmpty;
             p1 = p1.Next, p2 = p2.Next)
          if (p1.Val != p2.Val)
@@ -942,21 +976,25 @@ namespace M17N.Core
        MInterval head = find_head (start);
        MInterval tail = find_tail (end);
 
+       M17n.DebugPrint ("Copying: {0}", copy);
+
        if (! head.Stack.IsEmpty
-           && (isAnySensitive && head.From < start
-               || isFrontSensitive && ! first))
+           && (isSensitive && head.From < start
+               || (isFrontSensitive && ! first)))
          {
+           M17n.DebugPrint (" clear head");
            head = copy.find_head (0);
            head.Stack.Clear ();
          }
        if (! tail.Stack.IsEmpty
-           && (isAnySensitive && end < head.To
-               || isRearSensitive && ! last))
+           && (isSensitive && end < tail.To
+               || (isRearSensitive && ! last)))
          {
+           M17n.DebugPrint (" clear tail");
            tail = copy.find_tail (copy.Length);
            tail.Stack.Clear ();
          }
-       M17n.DebugPrint ("Copied: {0}\n", copy);
+       M17n.DebugPrint ("\n");
        return copy;
       }
 
@@ -1039,10 +1077,9 @@ namespace M17N.Core
 
            len = 0;
            if (Stack.IsEmpty
-               && (isFrontSensitive
-                   || ((isSensitive || isRearSensitive) && i.From < start)))
+               && (isFrontSensitive || (isSensitive && i.From < start)))
              {
-               M17n.DebugPrint (" grafting {0}", i);
+               M17n.DebugPrint (" forward grafting {0}", i);
                if (i.To < end)
                  len = i.To - start;
                else
@@ -1051,7 +1088,7 @@ namespace M17N.Core
              }
            while (i != null && i.From < end && mergeable (i))
              {
-               M17n.DebugPrint (" grafting {0}", i);
+               M17n.DebugPrint (" forward grafting {0}", i);
                len += i.To - i.From;
                if (i.From < start)
                  len -= start - i.From;
@@ -1084,10 +1121,9 @@ namespace M17N.Core
 
            len = 0;
            if (Stack.IsEmpty
-               && (isRearSensitive
-                   || ((isSensitive || isFrontSensitive) && end < i.To)))
+               && (isRearSensitive || (isSensitive && end < i.To)))
              {
-               M17n.DebugPrint (" grafting {0}", i);
+               M17n.DebugPrint (" backward grafting {0}", i);
                if (i.From <= start)
                  len = end - start;
                else
@@ -1096,7 +1132,7 @@ namespace M17N.Core
              }
            while (i != null && i.To <= start && mergeable (i))
              {
-               M17n.DebugPrint (" grafting {0}", i);
+               M17n.DebugPrint (" backward grafting {0}", i);
                len += i.To - i.From;
                if (end < i.To)
                  len -= i.To - end;
@@ -1135,11 +1171,13 @@ namespace M17N.Core
            if (prev != null && isRearSticky && ! prev.Stack.IsEmpty)
              {
                prev.enlarge (end - start);
+               M17n.DebugPrint (" done\n");
                return;
              }
            if (isFrontSticky && ! Stack.IsEmpty)
              {
                enlarge (end - start);
+               M17n.DebugPrint (" done\n");
                return;
              }
            bool front_grafted = false, rear_grafted = false;
@@ -1149,14 +1187,20 @@ namespace M17N.Core
              {
                start += grafted;
                if (start == end)
-                 return;
+                 {
+                   M17n.DebugPrint (" done\n");
+                   return;
+                 }
                front_grafted = true;
              }
            if ((grafted = graft_backward (interval, start, end)) > 0)
              {
                end -= grafted;
                if (start == end)
-                 return;
+                 {
+                   M17n.DebugPrint (" done\n");
+                   return;
+                 }
                rear_grafted = true;
              }
 
@@ -1191,7 +1235,7 @@ namespace M17N.Core
          {
            if (! Stack.IsEmpty)
              {
-               if (isSensitive || isFrontSensitive || isRearSensitive)
+               if (isSensitive)
                  Stack.Clear ();
                else if (isFrontSticky || isRearSticky)
                  {
@@ -1205,7 +1249,10 @@ namespace M17N.Core
              {
                start += grafted;
                if (start == end)
-                 return;
+                 {
+                   M17n.DebugPrint (" done\n");
+                   return;
+                 }
                front_grafted = true;
                pos += grafted;
              }
@@ -1213,7 +1260,10 @@ namespace M17N.Core
              {
                end -= grafted;
                if (start == end)
-                 return;
+                 {
+                   M17n.DebugPrint (" done\n");
+                   return;
+                 }
                rear_grafted = true;
              }
            if (interval != null)
@@ -1239,11 +1289,14 @@ namespace M17N.Core
            if (isRearSticky && ! Stack.IsEmpty)
              {
                enlarge (end - start);
+               M17n.DebugPrint (" done by enlarging this\n");
                return;
              }
            if (next != null && isFrontSticky && ! next.Stack.IsEmpty)
              {
+               M17n.DebugPrint (" next is {0}\n", next);
                next.enlarge (end - start);
+               M17n.DebugPrint (" done by enlarging next\n");
                return;
              }
            bool front_grafted = false, rear_grafted = false;
@@ -1253,21 +1306,27 @@ namespace M17N.Core
              {
                end -= grafted;
                if (start == end)
-                 return;
+                 {
+                   M17n.DebugPrint (" done\n");
+                   return;
+                 }
                rear_grafted = true;
              }
            if ((grafted = graft_forward (interval, start, end)) > 0)
              {
                start += grafted;
                if (start == end)
-                 return;
+                 {
+                   M17n.DebugPrint (" done\n");
+                   return;
+                 }
                front_grafted = true;
              }
            if (interval != null)
              interval = interval.Copy (mtext, start, end,
                                        front_grafted,
                                        (rear_grafted
-                                        || (next == null && end < interval.mtext.Length)));
+                                        || (next == null && end == interval.mtext.Length)));
            else
              interval = new MInterval (Key, mtext, end - start, null);
 
@@ -1346,7 +1405,7 @@ namespace M17N.Core
                Left.Delete (start, end);
                return;
              }
-           if (isSensitive || isFrontSensitive)
+           if (isSensitive)
              Stack.Clear ();
            Left.Delete (start, From);
            To -= From - start;
@@ -1363,7 +1422,7 @@ namespace M17N.Core
                Right.Delete (start, end);
                return;
              }
-           if (isSensitive || isRearSensitive)
+           if (isSensitive)
              Stack.Clear ();
            Right.Delete (To, end);
            end = To;
@@ -1404,7 +1463,7 @@ namespace M17N.Core
          {
            int len = end - start;
 
-           if (isAnySensitive)
+           if (isSensitive)
              Stack.Clear ();
            for (MInterval i = this; i != null; i = i.Parent)
              i.Length -= len;
@@ -1436,7 +1495,7 @@ namespace M17N.Core
            end = To;
          }
 
-       if (! Stack.IsEmpty && isAnySensitive)
+       if (! Stack.IsEmpty && isSensitive)
          Stack.Clear ();
        if (start > From)
          divide_left (start);
@@ -1560,7 +1619,7 @@ namespace M17N.Core
 
        if (! Stack.IsEmpty)
          {
-           if (isAnySensitive)
+           if (isSensitive)
              Stack.Clear ();
            else
              {
@@ -1578,22 +1637,6 @@ namespace M17N.Core
        update_from_to ();
        MInterval head = find_head (start);
        MInterval tail = find_tail (end);
-       while (! head.Stack.IsEmpty && head.From > 0)
-         {
-           MInterval prev = head.Prev;
-
-           if (prev.Stack.IsEmpty || head.Stack.Val != prev.Stack.Val)
-             break;
-           head = head.Prev;
-         }
-       while (! tail.Stack.IsEmpty && tail.To < mtext.Length)
-         {
-           MInterval next = tail.Next;
-
-           if (next.Stack.IsEmpty || tail.Stack.Val != next.Stack.Val)
-             break;
-           tail = tail.Next;
-         }
        Pop (head.From, tail.To);
       }
 
@@ -1661,7 +1704,7 @@ namespace M17N.Core
 
       public void DumpNested (bool force)
       {
-       DumpNested (Key.ToString () + ":", force);
+       DumpNested ("  " + Key.ToString () + ":", force);
       }
 
       public void DumpNested (string indent, bool force)