Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
204 changes: 105 additions & 99 deletions src/Compilers/CSharp/Portable/SymbolDisplay/ObjectDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,88 +133,11 @@ internal static string FormatLiteral(bool value)
return value ? "true" : "false";
}

private static void FormatStringChar(
ref PooledStringBuilder pooledBuilder,
string str,
int index,
char c,
char quote,
bool useLanguageSpecificEscapes,
bool useUnicodeEscapes)
private static void FormatStringChar(StringBuilder builder, char c, char quote)
{
Debug.Assert(quote == '\0' || quote == '"' || quote == '\'');

string replaceWith = null;
if (useLanguageSpecificEscapes)
{
switch (c)
{
case '\\':
replaceWith = "\\\\";
break;
case '"':
if (quote == c)
{
replaceWith = "\\\"";
}
break;
case '\'':
if (quote == c)
{
replaceWith = "\\'";
}
break;
case '\0':
replaceWith = "\\0";
break;
case '\a':
replaceWith = "\\a";
break;
case '\b':
replaceWith = "\\b";
break;
case '\f':
replaceWith = "\\f";
break;
case '\n':
replaceWith = "\\n";
break;
case '\r':
replaceWith = "\\r";
break;
case '\t':
replaceWith = "\\t";
break;
case '\v':
replaceWith = "\\v";
break;
}
}

bool unicodeEscape = false;
if ((replaceWith == null) && useUnicodeEscapes)
string replaceWith;
if (ReplaceChar(c, quote, out replaceWith))
{
switch (CharUnicodeInfo.GetUnicodeCategory(c))
{
case UnicodeCategory.Control:
case UnicodeCategory.OtherNotAssigned:
case UnicodeCategory.ParagraphSeparator:
unicodeEscape = true;
break;
}
}

if ((replaceWith != null) || unicodeEscape)
{
if (pooledBuilder == null)
{
pooledBuilder = PooledStringBuilder.GetInstance();
if (index > 0)
{
pooledBuilder.Builder.Append(str, 0, index);
}
}
var builder = pooledBuilder.Builder;
if (replaceWith != null)
{
builder.Append(replaceWith);
Expand All @@ -225,13 +148,94 @@ private static void FormatStringChar(
builder.Append(((int)c).ToString("x4"));
}
}
else if (pooledBuilder != null)
else
{
var builder = pooledBuilder.Builder;
builder.Append(c);
}
}

/// <summary>
/// Returns true if the character should be replaced and sets
/// replaceWith to the replacement text if the character is

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

paramref

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

/// replaced with text other than the Unicode escape sequence.
/// </summary>
private static bool ReplaceChar(char c, char quote, out string replaceWith)
{
Debug.Assert(quote == '\0' || quote == '"' || quote == '\'');

replaceWith = null;
switch (c)
{
case '\\':
replaceWith = "\\\\";
break;
case '"':
if (quote == c)
{
replaceWith = "\\\"";
}
break;
case '\'':
if (quote == c)
{
replaceWith = "\\'";
}
break;
case '\0':
replaceWith = "\\0";
break;
case '\a':
replaceWith = "\\a";
break;
case '\b':
replaceWith = "\\b";
break;
case '\f':
replaceWith = "\\f";
break;
case '\n':
replaceWith = "\\n";
break;
case '\r':
replaceWith = "\\r";
break;
case '\t':
replaceWith = "\\t";
break;
case '\v':
replaceWith = "\\v";
break;
}

if (replaceWith != null)
{
return true;
}

switch (CharUnicodeInfo.GetUnicodeCategory(c))
{
case UnicodeCategory.Control:
case UnicodeCategory.OtherNotAssigned:
case UnicodeCategory.ParagraphSeparator:
return true;
default:
return false;
}
}

private static bool ReplaceAny(string s, char quote)
{
foreach (var c in s)
{
string replaceWith;
if (ReplaceChar(c, quote, out replaceWith))
{
return true;
}
}
return false;
}

/// <summary>
/// Returns a C# string literal with the given value.
/// </summary>
Expand All @@ -252,23 +256,26 @@ public static string FormatLiteral(string value, ObjectDisplayOptions options)

var useQuotes = options.IncludesOption(ObjectDisplayOptions.UseQuotes);
var quote = useQuotes ? '"' : '\0';
PooledStringBuilder pooledBuilder = null;
StringBuilder builder = null;
if (!useQuotes && !ReplaceAny(value, quote))
{
return value;
}

var pooledBuilder = PooledStringBuilder.GetInstance();
var builder = pooledBuilder.Builder;
if (useQuotes)
{
pooledBuilder = PooledStringBuilder.GetInstance();
builder = pooledBuilder.Builder;
builder.Append(quote);
}
for (int i = 0; i < value.Length; i++)
foreach (var c in value)
{
FormatStringChar(ref pooledBuilder, value, i, value[i], quote, useLanguageSpecificEscapes: true, useUnicodeEscapes: true);
FormatStringChar(builder, c, quote);
}
if (useQuotes)
{
builder.Append(quote);
}
return (pooledBuilder == null) ? value : pooledBuilder.ToStringAndFree();
return pooledBuilder.ToStringAndFree();
}

internal static string FormatString(string str, bool useQuotes)
Expand All @@ -282,9 +289,9 @@ internal static string FormatString(string str, bool useQuotes)
var builder = pooledBuilder.Builder;
const char quote = '"';
builder.Append(quote);
for (int i = 0; i < str.Length; i++)
foreach (var c in str)
{
FormatStringChar(ref pooledBuilder, str, i, str[i], quote, useLanguageSpecificEscapes: useQuotes, useUnicodeEscapes: false);
FormatStringChar(builder, c, quote);
}
builder.Append(quote);
return pooledBuilder.ToStringAndFree();
Expand All @@ -298,24 +305,23 @@ internal static string FormatString(string str, bool useQuotes)
/// <returns>A character literal with the given value.</returns>
internal static string FormatLiteral(char c, ObjectDisplayOptions options)
{
var useQuotes = options.IncludesOption(ObjectDisplayOptions.UseQuotes);
var quote = useQuotes ? '\'' : '\0';
var includeCodePoints = options.IncludesOption(ObjectDisplayOptions.IncludeCodePoints);
var pooledBuilder = PooledStringBuilder.GetInstance();
var builder = pooledBuilder.Builder;
if (includeCodePoints)
if (options.IncludesOption(ObjectDisplayOptions.IncludeCodePoints))
{
builder.Append(options.IncludesOption(ObjectDisplayOptions.UseHexadecimalNumbers) ? "0x" + ((int)c).ToString("x4") : ((int)c).ToString());
builder.Append(" ");
}
if (useQuotes)
if (options.IncludesOption(ObjectDisplayOptions.UseQuotes))
{
const char quote = '\'';
builder.Append(quote);
FormatStringChar(builder, c, quote);
builder.Append(quote);
}
FormatStringChar(ref pooledBuilder, str: null, index: 0, c: c, quote: quote, useLanguageSpecificEscapes: useQuotes, useUnicodeEscapes: !includeCodePoints);
if (useQuotes)
else
{
builder.Append(quote);
builder.Append(c);
}
return pooledBuilder.ToStringAndFree();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void Characters()

Assert.Equal("39 '\\''", ObjectDisplay.FormatLiteral('\'', ObjectDisplayOptions.UseQuotes | ObjectDisplayOptions.IncludeCodePoints));
Assert.Equal("39 '", ObjectDisplay.FormatLiteral('\'', ObjectDisplayOptions.IncludeCodePoints));
Assert.Equal("0x001e '\u001e'", ObjectDisplay.FormatLiteral('\u001e', ObjectDisplayOptions.UseQuotes | ObjectDisplayOptions.IncludeCodePoints | ObjectDisplayOptions.UseHexadecimalNumbers));
Assert.Equal("0x001e '\\u001e'", ObjectDisplay.FormatLiteral('\u001e', ObjectDisplayOptions.UseQuotes | ObjectDisplayOptions.IncludeCodePoints | ObjectDisplayOptions.UseHexadecimalNumbers));
Assert.Equal("0x001e \u001e", ObjectDisplay.FormatLiteral('\u001e', ObjectDisplayOptions.IncludeCodePoints | ObjectDisplayOptions.UseHexadecimalNumbers));

Assert.Equal("0x0008 '\\b'", ObjectDisplay.FormatLiteral('\b', ObjectDisplayOptions.UseQuotes | ObjectDisplayOptions.IncludeCodePoints | ObjectDisplayOptions.UseHexadecimalNumbers));
Expand Down Expand Up @@ -157,12 +157,12 @@ public void Strings()

// Formatting with quotes should escape specific control characters.
var expected =
"\"\\0\u0001\u0002\u0003\u0004\u0005\u0006\\a\\b\\t\\n\\v\\f\\r\u000e\u000f\u0010" +
"\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d" +
"\u001e\u001f !\\\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[" +
"\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u007f\u0080\u0081\u0082\u0083\u0084\u0085" +
"\u0086\u0087\u0088\u0089\u008a\u008b\u008c\u008d\u008e\u008f\u0090\u0091\u0092" +
"\u0093\u0094\u0095\u0096\u0097\u0098\u0099\u009a\u009b\u009c\u009d\u009e\u009f" +
"\"\\0\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\a\\b\\t\\n\\v\\f\\r\\u000e\\u000f\\u0010" +
"\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d" +
"\\u001e\\u001f !\\\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[" +
"\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\u007f\\u0080\\u0081\\u0082\\u0083\\u0084\\u0085" +
"\\u0086\\u0087\\u0088\\u0089\\u008a\\u008b\\u008c\\u008d\\u008e\\u008f\\u0090\\u0091\\u0092" +
"\\u0093\\u0094\\u0095\\u0096\\u0097\\u0098\\u0099\\u009a\\u009b\\u009c\\u009d\\u009e\\u009f" +
" ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèé" +
"êëìíîïðñòóôõö÷øùúûüýþ\"";
Assert.Equal(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1679,7 +1679,7 @@ public void UnicodeChar()
value = CreateDkmClrValue('\u001f');
evalResult = FormatResult("c", value, inspectionContext: CreateDkmInspectionContext(radix: 16));
Verify(evalResult,
EvalResult("c", "0x001f '\u001f'", "char", "c", editableValue: "'\\u001f'"));
EvalResult("c", "0x001f '\\u001f'", "char", "c", editableValue: "'\\u001f'"));

// This char is not printable, but there is a specific escape character.
value = CreateDkmClrValue('\u0007');
Expand All @@ -1688,6 +1688,16 @@ public void UnicodeChar()
EvalResult("c", "0x0007 '\\a'", "char", "c", editableValue: "'\\a'"));
}

[WorkItem(1138095)]
[Fact]
public void UnicodeString()
{
var value = CreateDkmClrValue("\u1234\u001f\u0007");
var evalResult = FormatResult("s", value);
Verify(evalResult,
EvalResult("s", $"\"{'\u1234'}\\u001f\\a\"", "string", "s", editableValue: $"\"{'\u1234'}\\u001f\\a\"", flags: DkmEvaluationResultFlags.RawString));
}

[WorkItem(1002381)]
[Fact]
public void BaseTypeEditableValue()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,20 @@ public void NoQuotes_String()
value = CreateDkmClrValue("a\r\n\tb\v\b\u001ec", type: stringType);
evalResult = FormatResult("s", value, inspectionContext: inspectionContext);
Verify(evalResult,
EvalResult("s", "a\r\n\tb\v\b\u001ec", "string", "s", editableValue: "\"a\\r\\n\\tb\\v\\b\u001ec\"", flags: DkmEvaluationResultFlags.RawString));
EvalResult("s", "a\r\n\tb\v\b\u001ec", "string", "s", editableValue: "\"a\\r\\n\\tb\\v\\b\\u001ec\"", flags: DkmEvaluationResultFlags.RawString));

// "a\0b"
value = CreateDkmClrValue("a\0b", type: stringType);
evalResult = FormatResult("s", value, inspectionContext: inspectionContext);
Verify(evalResult,
EvalResult("s", "a\0b", "string", "s", editableValue: "\"a\\0b\"", flags: DkmEvaluationResultFlags.RawString));

// "\u007f\u009f"
value = CreateDkmClrValue("\u007f\u009f", type: stringType);
evalResult = FormatResult("s", value, inspectionContext: inspectionContext);
Verify(evalResult,
EvalResult("s", "\u007f\u009f", "string", "s", editableValue: "\"\\u007f\\u009f\"", flags: DkmEvaluationResultFlags.RawString));

// " " with alias
value = CreateDkmClrValue(" ", type: stringType, alias: "1", evalFlags: DkmEvaluationResultFlags.HasObjectId);
evalResult = FormatResult("s", value, inspectionContext: inspectionContext);
Expand Down Expand Up @@ -118,6 +124,12 @@ public void NoQuotes_Char()
Verify(evalResult,
EvalResult("c", "30 \u001e", "char", "c", editableValue: "'\\u001e'", flags: DkmEvaluationResultFlags.None));

// '\u007f'
value = CreateDkmClrValue('\u007f', type: charType);
evalResult = FormatResult("c", value, inspectionContext: inspectionContext);
Verify(evalResult,
EvalResult("c", "127 \u007f", "char", "c", editableValue: "'\\u007f'", flags: DkmEvaluationResultFlags.None));

// array
value = CreateDkmClrValue(new char[] { '1' }, type: charType.MakeArrayType());
evalResult = FormatResult("a", value, inspectionContext: inspectionContext);
Expand Down
Loading