Skip to content

Conversation

@Galoretka
Copy link
Contributor

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.

@benaadams benaadams requested a review from Copilot November 28, 2025 23:18
Copilot finished reviewing on behalf of benaadams November 28, 2025 23:20
Copy link
Contributor

Copilot AI left a 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

Comment on lines +94 to +103
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);
}
Copy link

Copilot AI Nov 28, 2025

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.

Copilot uses AI. Check for mistakes.
@benaadams benaadams requested a review from Copilot November 30, 2025 20:57
Copilot finished reviewing on behalf of benaadams November 30, 2025 20:58
Copy link
Contributor

Copilot AI left a 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant