/// <summary> /// Replaces all line breaks in the text with the specied value. /// </summary> public EditString ReplaceLineBreaks(string newValue) { var text = this.CurrentText; List <StringEdit> edits = null; for (int i = 0; i < text.Length; i++) { if (TextFacts.IsLineBreakStart(text[i])) { var len = TextFacts.GetLineBreakLength(text, i); // only make the edit if the newValue is different than the existing line break if (len != newValue.Length || string.Compare(text, i, newValue, 0, len) != 0) { if (edits == null) { edits = new List <StringEdit>(); } edits.Add(StringEdit.Replacement(i, len, newValue)); } // add one less because for-loop will add one back i += len - 1; } } return(this.ApplyAll(edits)); }
/// <summary> /// Removes the blank lines from the text. /// </summary> public EditString RemoveBlankLines() { var pos = 0; var text = this.CurrentText; List <StringEdit> edits = null; while (pos < text.Length) { var len = TextFacts.GetLineLength(text, pos, includeLineBreak: true); if (TextFacts.IsBlankLine(text, pos)) { if (edits == null) { edits = new List <StringEdit>(); } edits.Add(StringEdit.Deletion(pos, len)); } pos += len; } return(this.ApplyAll(edits)); }
/// <summary> /// Replaces all occurrances of <see cref="p:oldValue"/> with <see cref="p:newValue"/> /// </summary> public EditString Replace(string oldValue, string newValue) { int startIndex = 0; List <StringEdit> edits = null; while (true) { int oldValueStart = this.CurrentText.IndexOf(oldValue, startIndex); if (oldValueStart < startIndex) { break; } if (edits == null) { edits = new List <StringEdit>(); } edits.Add(StringEdit.Replacement(oldValueStart, oldValue.Length, newValue)); startIndex = oldValueStart + oldValue.Length; } return(this.ApplyAll(edits)); }
/// <summary> /// Applies the edit. /// </summary> public EditString Apply(StringEdit edit) { return(ReplaceAt(edit.Start, edit.DeleteLength, edit.InsertText)); }