-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Normalize text new line to Unix-style (#998)
Make text more consistent across Unix and Windows environments, avoiding the `\r` character in favor of the Unix new line ending. See #975
- Loading branch information
Showing
19 changed files
with
244 additions
and
41 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
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
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
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
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
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
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
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,43 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Text; | ||
|
||
namespace Microsoft.KernelMemory.Text; | ||
|
||
public static class StringBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Append line using Unix line ending "\n" | ||
/// </summary> | ||
public static void AppendLineNix(this StringBuilder sb) | ||
{ | ||
sb.Append('\n'); | ||
} | ||
|
||
/// <summary> | ||
/// Append line using Unix line ending "\n" | ||
/// </summary> | ||
public static void AppendLineNix(this StringBuilder sb, string value) | ||
{ | ||
sb.Append(value); | ||
sb.Append('\n'); | ||
} | ||
|
||
/// <summary> | ||
/// Append line using Unix line ending "\n" | ||
/// </summary> | ||
public static void AppendLineNix(this StringBuilder sb, char value) | ||
{ | ||
sb.Append(value); | ||
sb.Append('\n'); | ||
} | ||
|
||
/// <summary> | ||
/// Append line using Unix line ending "\n" | ||
/// </summary> | ||
public static void AppendLineNix(this StringBuilder sb, StringBuilder value) | ||
{ | ||
sb.Append(value); | ||
sb.Append('\n'); | ||
} | ||
} |
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,67 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
namespace Microsoft.KernelMemory.Text; | ||
|
||
public static class StringExtensions | ||
{ | ||
public static string NormalizeNewlines(this string text, bool trim = false) | ||
{ | ||
if (string.IsNullOrEmpty(text)) | ||
{ | ||
return text; | ||
} | ||
|
||
// We won't need more than the original length | ||
char[] buffer = new char[text.Length]; | ||
int bufferPos = 0; | ||
|
||
// Skip leading whitespace if trimming | ||
int i = 0; | ||
if (trim) | ||
{ | ||
while (i < text.Length && char.IsWhiteSpace(text[i])) { i++; } | ||
} | ||
|
||
// Tracks the last non-whitespace position written into buffer | ||
int lastNonWhitespacePos = -1; | ||
|
||
// Single pass: replace \r\n or \r with \n, record last non-whitespace | ||
for (; i < text.Length; i++) | ||
{ | ||
char c = text[i]; | ||
|
||
if (c == '\r') | ||
{ | ||
// If \r\n then skip the \n | ||
if (i + 1 < text.Length && text[i + 1] == '\n') { i++; } | ||
|
||
// Write a single \n | ||
buffer[bufferPos] = '\n'; | ||
} | ||
else | ||
{ | ||
buffer[bufferPos] = c; | ||
} | ||
|
||
// If trimming, update lastNonWhitespacePos only when char isn't whitespace | ||
// If not trimming, always update because we keep everything | ||
if (!trim || !char.IsWhiteSpace(buffer[bufferPos])) | ||
{ | ||
lastNonWhitespacePos = bufferPos; | ||
} | ||
|
||
bufferPos++; | ||
} | ||
|
||
// Cut off trailing whitespace if trimming | ||
// If every char was whitespace, lastNonWhitespacePos stays -1 and the result is an empty string | ||
int finalLength = (trim && lastNonWhitespacePos >= 0) | ||
? lastNonWhitespacePos + 1 | ||
: bufferPos; | ||
|
||
// Safety check if everything was trimmed away | ||
if (finalLength < 0) { finalLength = 0; } | ||
|
||
return new string(buffer, 0, finalLength); | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.