Skip to content

Commit

Permalink
add Trim method
Browse files Browse the repository at this point in the history
  • Loading branch information
MeilCli committed Apr 16, 2017
1 parent 4a0b127 commit fb55bd5
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
91 changes: 91 additions & 0 deletions Source/Plugin.CrossFormattedText.Abstractions/FormattedString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,97 @@ public FormattedString ToUpperInvariant() {
return new FormattedString(sAr);
}

public FormattedString Trim() {
return trim(Text.Trim());
}

public FormattedString Trim(params char[] trimChars) {
return trim(Text.Trim(trimChars));
}

public FormattedString TrimEnd(params char[] trimChars) {
return trim(Text.TrimEnd(trimChars));
}

public FormattedString TrimStart(params char[] trimChars) {
return trim(Text.TrimStart(trimChars));
}

private FormattedString trim(string trimedText) {
if(trimedText.Length == 0) {
return Empty;
}

char startChar = trimedText[0];
char endChar = trimedText[trimedText.Length - 1];

int startIndex = IndexOf(startChar);
int endIndex = LastIndexOf(endChar);

CharSpan[] sAr = ToCharSpanArray();
CharSpan[] nAr = new CharSpan[endIndex - startIndex + 1];

Array.Copy(sAr,startIndex,nAr,0,endIndex - startIndex + 1);

return MergeCharSpan(nAr);
}

public FormattedString TrimSpan(params Span[] trimSpans) {
if(Length == 0) {
return this;
}
int startIndex = 0;
int endIndex = Length - 1;
trimEndSpan(trimSpans,startIndex,ref endIndex);
trimStartSpan(trimSpans,ref startIndex,endIndex);

return Subspan(startIndex,endIndex - startIndex + 1);
}

public FormattedString TrimEndSpan(params Span[] trimSpans) {
if(Length == 0) {
return this;
}
int startIndex = 0;
int endIndex = Length - 1;
trimEndSpan(trimSpans,startIndex,ref endIndex);

return Subspan(startIndex,endIndex - startIndex + 1);
}

public FormattedString TrimStartSpan(params Span[] trimSpans) {
if(Length == 0) {
return this;
}
int startIndex = 0;
int endIndex = Length - 1;
trimStartSpan(trimSpans,ref startIndex,endIndex);

return Subspan(startIndex,endIndex - startIndex + 1);
}

private void trimEndSpan(Span[] trimSpans,int startIndex,ref int endIndex) {
for(int i = Length - 1;i >= startIndex;i--) {
Span span = spans[i];
if(trimSpans.Any(x => x.Equals(span))) {
endIndex--;
}else {
break;
}
}
}

private void trimStartSpan(Span[] trimSpans,ref int startIndex,int endIndex) {
for(int i = startIndex;i <= endIndex;i++) {
Span span = spans[i];
if(trimSpans.Any(x => x.Equals(span))) {
startIndex++;
} else {
break;
}
}
}

public bool AnySpanReferenceEquals(FormattedString formattedString) {
foreach(var span1 in spans) {
foreach(var span2 in formattedString.spans) {
Expand Down
33 changes: 33 additions & 0 deletions Test/Test.CrossFormattedText.Unit/FormattedStringTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,5 +257,38 @@ public void ToUpperTest() {
Assert.AreEqual(newText2.Text,text.Text.ToUpperInvariant());
Assert.AreEqual(newText2.AnySpanReferenceEquals(text),false);
}

[TestMethod]
public void TrimTest() {
var newText1 = text.Trim('T','t','.');

Assert.AreEqual(newText1.Text,text.Text.Trim('T','t','.'));
Assert.AreEqual(newText1.AnySpanReferenceEquals(text),false);

var newText2 = text.TrimEnd('T','t','.');

Assert.AreEqual(newText2.Text,text.Text.TrimEnd('T','t','.'));
Assert.AreEqual(newText2.AnySpanReferenceEquals(text),false);

var newText3 = text.TrimStart('T','t','.');

Assert.AreEqual(newText3.Text,text.Text.TrimStart('T','t','.'));
Assert.AreEqual(newText3.AnySpanReferenceEquals(text),false);
}

[TestMethod]
public void TrimSpanTest() {
var startSpan = new Span() {
Text = "This is sample text."
};
var endSpan = new Span() {
Text = "This is text."
};

var newText1 = text.TrimSpan(startSpan,endSpan);

Assert.AreEqual(newText1.Text,text.Subspan(1,3).Text);
Assert.AreEqual(newText1.AnySpanReferenceEquals(text),false);
}
}
}

0 comments on commit fb55bd5

Please sign in to comment.