Skip to content

Commit

Permalink
fix Insert method
Browse files Browse the repository at this point in the history
  • Loading branch information
MeilCli committed Apr 2, 2017
1 parent f0f67dc commit 7e4a69e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Source/Plugin.CrossFormattedText.Abstractions/FormattedString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
Span currentSpan = null;
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions Test/Test.CrossFormattedText.Unit/FormattedStringTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 7e4a69e

Please sign in to comment.