-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only un-escape unicode character encoding color codes #1124
- Loading branch information
Showing
5 changed files
with
58 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,9 @@ namespace NUnit.VisualStudio.TestAdapter.NUnitEngine; | |
|
||
internal static class UnicodeEscapeHelper | ||
{ | ||
public static string UnEscapeUnicodeCharacters(this string text) | ||
private const int EscapeAsciiValue = 0x1B; | ||
|
||
public static string UnEscapeUnicodeColorCodesCharacters(this string text) | ||
{ | ||
if (text == null) | ||
return null; | ||
|
@@ -43,13 +45,51 @@ private static bool TryUnEscapeOneCharacter(string text, int position, out char | |
if (position + unicodeEscapeSample.Length >= text.Length) | ||
return false; | ||
|
||
|
||
extraCharacterRead = unicodeEscapeSample.Length; | ||
if (!int.TryParse(text.Substring(position + 2, unicodeEscapeSample.Length - 1), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var escapeValue)) | ||
return false; | ||
|
||
// Here we only want to escape color escape character when used in a context of a ANSI color code | ||
// See https://github.com/nunit/nunit3-vs-adapter/issues/1124 for more information. | ||
if (escapeValue != EscapeAsciiValue) | ||
return false; | ||
|
||
if (!IsAnsiColorCodeSequence(text, position + extraCharacterRead + 1)) | ||
return false; | ||
|
||
escapedChar = (char)escapeValue; | ||
|
||
return true; | ||
} | ||
|
||
private static bool IsAnsiColorCodeSequence(string text, int position) | ||
Check failure on line 65 in src/NUnitTestAdapter/NUnitEngine/UnicodeEscapeHelper.cs GitHub Actions / build
Check failure on line 65 in src/NUnitTestAdapter/NUnitEngine/UnicodeEscapeHelper.cs GitHub Actions / build
|
||
{ | ||
var start = false; | ||
while (position < text.Length) | ||
{ | ||
var c = text[position++]; | ||
// Look for the begining [ | ||
if (c == '[' && !start) | ||
{ | ||
start = true; | ||
continue; | ||
} | ||
|
||
// Found the 'm' at the end | ||
if (c == 'm' && start) | ||
return true; | ||
|
||
// [ was not found | ||
if (!start) | ||
return false; | ||
|
||
// Ignore all number and ; | ||
var isDigit = c is >= '0' and <= '9'; | ||
if (!isDigit && c != ';') | ||
return false; | ||
} | ||
|
||
// At the end without the ending 'm' | ||
return false; | ||
} | ||
} |
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