diff --git a/Source/Plugin.CrossFormattedText.Abstractions/FormattedString.cs b/Source/Plugin.CrossFormattedText.Abstractions/FormattedString.cs index 6a64f32..d4827bf 100644 --- a/Source/Plugin.CrossFormattedText.Abstractions/FormattedString.cs +++ b/Source/Plugin.CrossFormattedText.Abstractions/FormattedString.cs @@ -63,6 +63,14 @@ internal CharSpan[] ToCharSpanArray() { return ar; } + internal Span[] ToClonedSpanArray() { + var ar = new Span[spans.Length]; + for(int i = 0;i < spans.Length;i++) { + ar[i] = spans[i].Clone(); + } + return ar; + } + internal FormattedString MergeCharSpan(CharSpan[] newSpans) { var list = new List(); Span currentSpan = null; @@ -209,11 +217,12 @@ public FormattedString Insert(int insertIndex,Span value) { throw new ArgumentNullException(nameof(value)); } + Span[] sAr = ToClonedSpanArray(); Span[] nAr = new Span[spans.Length + 1]; Span newSpan = value.Clone(); - Array.Copy(spans,nAr,insertIndex); - Array.Copy(spans,insertIndex,nAr,insertIndex + 1,spans.Length - insertIndex); + Array.Copy(sAr,nAr,insertIndex); + Array.Copy(sAr,insertIndex,nAr,insertIndex + 1,sAr.Length - insertIndex); nAr[insertIndex] = newSpan; return new FormattedString(nAr); diff --git a/Test/Test.CrossFormattedText.Unit/FormattedStringTest.cs b/Test/Test.CrossFormattedText.Unit/FormattedStringTest.cs index f8697e7..2028f3e 100644 --- a/Test/Test.CrossFormattedText.Unit/FormattedStringTest.cs +++ b/Test/Test.CrossFormattedText.Unit/FormattedStringTest.cs @@ -92,6 +92,7 @@ public void InsertTest() { var newText3 = Text.Insert(1,span); Assert.AreEqual(newText3[1].Equals(span),true); Assert.AreEqual(newText3.Length,Text.Length + 1); + Assert.AreEqual(Text.AnySpanReferenceEquals(newText3),false); } [TestMethod]