-
-
Save pavelhodek/8d1e553685c4696e59c4bb25dea944e7 to your computer and use it in GitHub Desktop.
Revisions
-
jonlabelle revised this gist
Nov 20, 2020 . No changes.There are no files selected for viewing
-
jonlabelle created this gist
Nov 20, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ /// <summary> /// Normalize line endings. /// </summary> /// <param name="lines">Lines to normalize.</param> /// <param name="targetLineEnding">If targetLineEnding is null, Environment.NewLine is used.</param> /// <exception cref="ArgumentOutOfRangeException">Unknown target line ending character(s).</exception> /// <returns>Lines normalized.</returns> /// <remarks> /// https://jonlabelle.com/snippets/view/csharp/normalize-line-endings /// </remarks> public static string NormalizeLineEndings(string lines, string targetLineEnding = null) { if (string.IsNullOrEmpty(lines)) { return lines; } targetLineEnding ??= Environment.NewLine; const string unixLineEnding = "\n"; const string windowsLineEnding = "\r\n"; const string macLineEnding = "\r"; if (targetLineEnding != unixLineEnding && targetLineEnding != windowsLineEnding && targetLineEnding != macLineEnding) { throw new ArgumentOutOfRangeException(nameof(targetLineEnding), "Unknown target line ending character(s)."); } lines = lines .Replace(windowsLineEnding, unixLineEnding) .Replace(macLineEnding, unixLineEnding); if (targetLineEnding != unixLineEnding) { lines = lines.Replace(unixLineEnding, targetLineEnding); } return lines; }