/// <summary>
/// Normalizes all new lines in <paramref name="input" /> to be <paramref name="newLine" />.
/// </summary>
public static string NormalizeNewLines(string input, string newLine)
{
if (input == null)
{
return(null);
}
if (!IsNewLine(newLine))
{
throw new ArgumentException("newLine must be one of the known newline sequences");
}
var ds = NewLineFinder.NextNewLine(input, 0);
if (ds == SimpleSegment.Invalid) // text does not contain any new lines
{
return(input);
}
var b = new StringBuilder(input.Length);
var lastEndOffset = 0;
do
{
b.Append(input, lastEndOffset, ds.Offset - lastEndOffset);
b.Append(newLine);
lastEndOffset = ds.EndOffset;
ds = NewLineFinder.NextNewLine(input, lastEndOffset);
} while (ds != SimpleSegment.Invalid);
// remaining string (after last newline)
b.Append(input, lastEndOffset, input.Length - lastEndOffset);
return(b.ToString());
}