-
Notifications
You must be signed in to change notification settings - Fork 594
fix: add ReadOnlySpan<byte> overload for long BE decode #9826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
fix: add ReadOnlySpan<byte> overload for long BE decode #9826
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR introduces a ReadOnlySpan overload for ToLongFromBigEndianByteArrayWithoutLeadingZeros to provide zero-allocation decoding and unify semantics with UInt64Extensions. The existing byte[] overload now delegates to the span-based implementation.
Key changes:
- Adds span-based overload that delegates to UInt64Extensions' well-tested forward accumulation algorithm
- Refactors byte[] overload to call the span version, maintaining backward compatibility
- Fixes potential shift count overflow issues for inputs longer than 8 bytes
| public static long ToLongFromBigEndianByteArrayWithoutLeadingZeros(this ReadOnlySpan<byte> bytes) | ||
| { | ||
| if (bytes is null) | ||
| if (bytes.IsEmpty) | ||
| { | ||
| return 0L; | ||
| } | ||
|
|
||
| long value = 0; | ||
| int length = bytes.Length; | ||
| ulong u = UInt64Extensions.ToULongFromBigEndianByteArrayWithoutLeadingZeros(bytes); | ||
| return unchecked((long)u); | ||
| } |
Copilot
AI
Nov 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new ReadOnlySpan overload lacks test coverage. Consider adding test cases similar to those in UInt64Tests.cs (which has comprehensive test cases for various edge cases like "7fffffffffffffff", "ffffffffffffffff", "0000", etc.) to ensure the span-based implementation handles all scenarios correctly, especially since it now delegates to UInt64Extensions and performs an unchecked cast.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
Introduced a ReadOnlySpan overload for ToLongFromBigEndianByteArrayWithoutLeadingZeros in Int64Extensions and refactored the existing byte[] overload to delegate to it, unifying the decoding semantics with UInt64Extensions by delegating to the ulong span-based implementation and casting to long. The update removes a subtle correctness risk in the previous reverse-index add/shift algorithm where shift counts could wrap for inputs longer than eight bytes, and provides a zero-allocation path that matches the project’s preference for span-based APIs. Existing call sites continue working without modification, behavior for typical ≤8-byte inputs remains consistent, and decoding of oversized inputs now matches the well-defined forward accumulation semantics already used for ulong.