Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Modified format logic so that braces are not allowed in custom format…
Browse files Browse the repository at this point in the history
… specifiers (#23062)

* Modified format logic so that braces are not allowed in custom format specifiers.

* Disabled failing test temporarily and fixed code style issue.

PR needed to pass test: dotnet/corefx#35820

* Slight changes to logic to better align the diff.

* Fixed counting error.

* Fix nullable

* Move exclusion near related test

* Add skip to new issue file

* Delete CoreFX.issues.json
  • Loading branch information
mikernet authored and JeremyKuhne committed May 22, 2019
1 parent 2b62361 commit 5d8fea0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 49 deletions.
70 changes: 21 additions & 49 deletions src/System.Private.CoreLib/shared/System/Text/StringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1538,7 +1538,6 @@ internal StringBuilder AppendFormatHelper(IFormatProvider? provider, string form
int pos = 0;
int len = format.Length;
char ch = '\x0';
StringBuilder? unescapedItemFormat = null;

ICustomFormatter? cf = null;
if (provider != null)
Expand Down Expand Up @@ -1665,7 +1664,7 @@ internal StringBuilder AppendFormatHelper(IFormatProvider? provider, string form
// Start of parsing of optional formatting parameter.
//
object? arg = args[index];
string? itemFormat = null;

ReadOnlySpan<char> itemFormatSpan = default; // used if itemFormat is null
// Is current character a colon? which indicates start of formatting parameter.
if (ch == ':')
Expand All @@ -1678,67 +1677,40 @@ internal StringBuilder AppendFormatHelper(IFormatProvider? provider, string form
// If reached end of text then error. (Unexpected end of text)
if (pos == len) FormatError();
ch = format[pos];
pos++;

// Is character a opening or closing brace?
if (ch == '}' || ch == '{')
if (ch == '}')
{
if (ch == '{')
{
// Yes, is next character also a opening brace, then treat as escaped. eg {{
if (pos < len && format[pos] == '{')
pos++;
else
// Error Argument Holes can not be nested.
FormatError();
}
else
{
// Yes, is next character also a closing brace, then treat as escaped. eg }}
if (pos < len && format[pos] == '}')
pos++;
else
{
// No, then treat it as the closing brace of an Arg Hole.
pos--;
break;
}
}

// Reaching here means the brace has been escaped
// so we need to build up the format string in segments
if (unescapedItemFormat == null)
{
unescapedItemFormat = new StringBuilder();
}
unescapedItemFormat.Append(format, startPos, pos - startPos - 1);
startPos = pos;
// Argument hole closed
break;
}
}

if (unescapedItemFormat == null || unescapedItemFormat.Length == 0)
{
if (startPos != pos)
else if (ch == '{')
{
// There was no brace escaping, extract the item format as a single string
itemFormatSpan = format.AsSpan(startPos, pos - startPos);
// Braces inside the argument hole are not supported
FormatError();
}

pos++;
}
else

if (pos > startPos)
{
unescapedItemFormat.Append(format, startPos, pos - startPos);
itemFormatSpan = itemFormat = unescapedItemFormat.ToString();
unescapedItemFormat.Clear();
itemFormatSpan = format.AsSpan(startPos, pos - startPos);
}
}
// If current character is not a closing brace then error. (Unexpected Character)
if (ch != '}') FormatError();
else if (ch != '}')
{
// Unexpected character
FormatError();
}

// Construct the output for this arg hole.
pos++;
string? s = null;
string? itemFormat = null;

if (cf != null)
{
if (itemFormatSpan.Length != 0 && itemFormat == null)
if (itemFormatSpan.Length != 0)
{
itemFormat = new string(itemFormatSpan);
}
Expand Down
1 change: 1 addition & 0 deletions tests/CoreFX/CoreFX.issues.rsp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@
-nomethod System.Numerics.Tests.Vector4Tests.Vector4ClampTest
-noclass System.Diagnostics.Tests.DebugTests
-nomethod System.Text.Tests.StringBuilderTests.Equals
-nomethod System.Text.Tests.StringBuilderTests.AppendFormat
-nomethod System.Tests.DecimalTests.Remainder
-nomethod System.Tests.DecimalTests.Remainder_Invalid
-nomethod System.Tests.DecimalTests+BigIntegerMod.Test
Expand Down

0 comments on commit 5d8fea0

Please sign in to comment.