Conversation
Replace pointer-based parsing with safe indexing and for loops in ParseNextInt32, ParseNextInt64, ParseNextUInt32, ParseNextUInt64. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR removes unsafe pointer-based parsing from System.IO.StringParser numeric parsing helpers by switching to safe string indexing/for loops, with a small consistency fix around empty-component handling.
Changes:
- Replaced
fixed/pointer loops with safe indexed loops inParseNextInt32/Int64/UInt32/UInt64. - Added/standardized empty-component validation (notably for
ParseNextUInt64).
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…d add empty-component tests - Use AsSpan slice in all parse methods so JIT can eliminate bounds checks - Add Theory test covering empty component -> InvalidDataException for all numeric parse methods Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR removes unsafe pointer-based parsing from System.IO.StringParser numeric parsing helpers, replacing it with safe ReadOnlySpan<char> indexing and for loops while keeping the same overflow/invalid-data behavior.
Changes:
- Rewrote
ParseNextInt32,ParseNextInt64,ParseNextUInt32, andParseNextUInt64to avoidfixed/pointers and instead parse via spans and indexing. - Added a new unit test intended to validate that empty components throw
InvalidDataExceptionfor numeric parsing.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/libraries/Common/src/System/IO/StringParser.cs | Replaces pointer-based numeric parsing loops with span/index-based parsing and explicit empty-component checks. |
| src/libraries/Common/tests/Tests/System/IO/StringParserTests.cs | Adds a theory to validate numeric parsers throw InvalidDataException on empty components. |
src/libraries/Common/tests/Tests/System/IO/StringParserTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/Common/tests/Tests/System/IO/StringParserTests.cs
Outdated
Show resolved
Hide resolved
…anch Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR removes unsafe pointer-based parsing from StringParser’s numeric parsing APIs by switching to ReadOnlySpan<char> indexing/loops, and adds a regression test to ensure empty components throw InvalidDataException.
Changes:
- Replaced pointer iteration with
ReadOnlySpan<char>+forloops inParseNextInt32/Int64/UInt32/UInt64. - Added a new xUnit theory covering empty components for all four numeric parse methods.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/libraries/Common/src/System/IO/StringParser.cs |
Converts numeric parsing implementations from unsafe pointer-based logic to safe span-based loops. |
src/libraries/Common/tests/Tests/System/IO/StringParserTests.cs |
Adds a theory to validate empty-component behavior for numeric parsers. |
src/libraries/Common/tests/Tests/System/IO/StringParserTests.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Pull request overview
This PR removes unsafe pointer-based parsing from StringParser numeric parsing methods by switching to ReadOnlySpan<char> indexing and for loops.
Changes:
- Replaced pointer iteration with span indexing in
ParseNextInt32andParseNextInt64. - Replaced pointer iteration with span indexing in
ParseNextUInt32andParseNextUInt64. - Added explicit empty-span validation before parsing.
| int startIndex = _startIndex; | ||
| int endIndex = _endIndex; | ||
| ReadOnlySpan<char> span = _buffer.AsSpan(startIndex, endIndex - startIndex); | ||
|
|
||
| if (span.IsEmpty) | ||
| { | ||
| ThrowForInvalidData(); | ||
| } |
| MoveNextOrFail(); | ||
| if (_startIndex == _endIndex) | ||
|
|
||
| ReadOnlySpan<char> span = _buffer.AsSpan(_startIndex, _endIndex - _startIndex); |
|
|
||
| /// <summary>Moves to the next component and parses it as an Int64.</summary> | ||
| public unsafe long ParseNextInt64() | ||
| public long ParseNextInt64() |
There was a problem hiding this comment.
Do we know why this is its own custom handler and not just reusing the shared number parsing code used by int/long?
There was a problem hiding this comment.
When this was written, span didn't exist yet, and we didn't want to allocate strings for each component just to be able to parse them. Now that span exists and you can int/long.Parse from them, that should be doable.
|
Tagging subscribers to this area: @dotnet/area-system-io |
Replace pointer-based parsing with safe indexing and for loops in ParseNextInt32, ParseNextInt64, ParseNextUInt32, ParseNextUInt64.