-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move EOL management to its own utility class, add test for just that
- Loading branch information
1 parent
78b259c
commit 80b92cd
Showing
5 changed files
with
135 additions
and
108 deletions.
There are no files selected for viewing
This file contains 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 characters
6 changes: 0 additions & 6 deletions
6
nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Run/StringExtensions.cs
This file was deleted.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Utilities/EOLHandlingTests.cs
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.Text.RegularExpressions; | ||
|
||
using Xunit; | ||
|
||
using static NuGetUpdater.Core.Utilities.EOLHandling; | ||
|
||
namespace NuGetUpdater.Core.Test.Utilities | ||
{ | ||
public class EOLHandlingTests | ||
{ | ||
[Theory] | ||
[InlineData(EOLType.LF, "\n")] | ||
[InlineData(EOLType.CR, "\r")] | ||
[InlineData(EOLType.CRLF, "\r\n")] | ||
public void ValidateEOLNormalizesFromLF(EOLType eolType, string literal) | ||
{ | ||
var teststring = "this\ris\na\r\nstring\rwith\nmixed\r\nline\rendings\n."; | ||
var changed = teststring.SetEOL(eolType); | ||
var lineEndings = Regex.Split(changed, "\\S+"); | ||
Assert.All(lineEndings, lineEnding => lineEnding.Equals(literal)); | ||
} | ||
} | ||
} |
This file contains 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 characters
78 changes: 78 additions & 0 deletions
78
nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Utilities/EOLHandling.cs
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using System.Text.RegularExpressions; | ||
|
||
namespace NuGetUpdater.Core.Utilities; | ||
|
||
public static class EOLHandling | ||
{ | ||
/// <summary> | ||
/// Used to save (and then restore) which line endings are predominant in a file. | ||
/// </summary> | ||
public enum EOLType | ||
{ | ||
/// <summary> | ||
/// Line feed - \n | ||
/// Typical on most systems. | ||
/// </summary> | ||
LF, | ||
/// <summary> | ||
/// Carriage return - \r | ||
/// Typical on older MacOS, unlikely (but possible) to come up here | ||
/// </summary> | ||
CR, | ||
/// <summary> | ||
/// Carriage return and line feed - \r\n. | ||
/// Typical on Windows | ||
/// </summary> | ||
CRLF | ||
}; | ||
|
||
/// <summary> | ||
/// Analyze the input string and find the most common line ending type. | ||
/// </summary> | ||
/// <param name="content">The string to analyze</param> | ||
/// <returns>The most common type of line ending in the input string.</returns> | ||
public static EOLType GetPredominantEOL(this string content) | ||
{ | ||
// Get stats on EOL characters/character sequences, if one predominates choose that for writing later. | ||
var lfcount = content.Count(c => c == '\n'); | ||
var crcount = content.Count(c => c == '\r'); | ||
var crlfcount = Regex.Matches(content, "\r\n").Count(); | ||
|
||
// Since CRLF contains both a CR and a LF, subtract it from those counts | ||
lfcount -= crlfcount; | ||
crcount -= crlfcount; | ||
if (crcount > lfcount && crcount > crlfcount) | ||
{ | ||
return EOLType.CR; | ||
} | ||
else if (crlfcount > lfcount) | ||
{ | ||
return EOLType.CRLF; | ||
} | ||
else | ||
{ | ||
return EOLType.LF; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Given a line ending, modify the input string to uniformly use that line ending. | ||
/// </summary> | ||
/// <param name="content">The input string, which may have any combination of line endings.</param> | ||
/// <param name="desiredEOL">The line ending type to use across the result.</param> | ||
/// <returns>The string with any line endings swapped to the desired type.</returns> | ||
/// <exception cref="ArgumentOutOfRangeException">If EOLType is an unexpected value.</exception> | ||
public static string SetEOL(this string content, EOLType desiredEOL) | ||
{ | ||
switch (desiredEOL) | ||
{ | ||
case EOLType.LF: | ||
return Regex.Replace(content, "(\r\n|\r)", "\n"); | ||
case EOLType.CR: | ||
return Regex.Replace(content, "(\r\n|\n)", "\r"); | ||
case EOLType.CRLF: | ||
return Regex.Replace(content, "(\r\n|\r|\n)", "\r\n"); | ||
} | ||
throw new ArgumentOutOfRangeException(nameof(desiredEOL)); | ||
} | ||
} |