X-Git-Url: http://git.chise.org/gitweb/?a=blobdiff_plain;f=MText.cs;h=fcf4a58bbd196c83bec27393fdec53f10fc30f58;hb=98e63be36c264959e53e41e88bb48b77349d244e;hp=db9962def64fe8f36c019571d02dd811950ecb81;hpb=a583a94ebfd8bbd3fa3353afa204059e1c90c941;p=m17n%2Fm17n-lib-cs.git diff --git a/MText.cs b/MText.cs index db9962d..fcf4a58 100644 --- a/MText.cs +++ b/MText.cs @@ -330,7 +330,11 @@ 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) @@ -377,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; @@ -390,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); @@ -402,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); @@ -475,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 @@ -749,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); @@ -764,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; } @@ -1200,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) { @@ -1214,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; } @@ -1225,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) { @@ -1238,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; } @@ -1312,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) @@ -1333,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