Skip to content

Utf8Parser.TryParse ignores the provided format for bool #100659

@joegoldman2

Description

@joegoldman2

Description

The current implementation of Utf8Parser.TryParse for bool ignores the format:

public static bool TryParse(ReadOnlySpan<byte> source, out bool value, out int bytesConsumed, char standardFormat = default)
{
if (!(standardFormat == default(char) || standardFormat == 'G' || standardFormat == 'l'))
ThrowHelper.ThrowFormatException_BadFormatSpecifier();
if (source.Length >= 4)
{
int dw = BinaryPrimitives.ReadInt32LittleEndian(source) & ~0x20202020;
if (dw == 0x45555254 /* 'EURT' */)
{
bytesConsumed = 4;
value = true;
return true;
}
if (source.Length > 4)
{
if (dw == 0x534c4146 /* 'SLAF' */ && (source[4] & ~0x20) == 'E')
{
bytesConsumed = 5;
value = false;
return true;
}
}
}
bytesConsumed = 0;
value = default;
return false;
}

Reproduction Steps

byte[] bytes = Encoding.UTF8.GetBytes("true");
var result = Utf8Parser.TryParse(bytes, out bool value, out int byteConsumed, standardFormat: 'G');
Console.WriteLine(result);

Expected behavior

result should be false because for the format G, True or False beginning with uppercase are expected.

Actual behavior

result is true.

Regression?

I don't think so. In dotnet/corefx, the implementation already didn't take the format into account.

Known Workarounds

No response

Configuration

9.0.0-preview.2.24128.5

Other information

No response

Metadata

Metadata

Assignees

Labels

area-System.MemorydocumentationDocumentation bug or enhancement, does not impact product or test codein-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions