Skip to content

Add polyfills for BigInteger.ToByteArray overloads and related .NET Core 2.1 members - #152

Merged
Tyrrrz merged 1 commit into
primefrom
copilot/add-polyfills-biginteger-tobytearray
Oct 3, 2026
Merged

Tyrrrz merged 1 commit into
primefrom
copilot/add-polyfills-biginteger-tobytearray

Conversation

Copilot AI commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

BigInteger gained a byte-array overload family in .NET Core 2.1 / .NET Standard 2.1 (ToByteArray(bool, bool), GetByteCount(bool), TryWriteBytes(Span<byte>, out int, bool, bool)) that wasn't available as a polyfill for older target frameworks.

Polyfill implementation

  • Added PolyShim/NetCore21/BigInteger.cs with extension members for:
    • ToByteArray(bool isUnsigned, bool isBigEndian)
    • GetByteCount(bool isUnsigned)
    • TryWriteBytes(Span<byte> destination, out int bytesWritten, bool isUnsigned, bool isBigEndian)
  • Built on top of the native parameterless ToByteArray(), handling sign-byte trimming for unsigned output, endianness reversal, and throwing OverflowException for negative values when isUnsigned: true, matching documented BCL behavior.
  • Guarded with a nested #if to exclude frameworks where BigInteger itself doesn't exist (net35, netstandard1.0), in addition to the standard upper-bound guard for frameworks that already have these members natively.
BigInteger value = 33022;

value.ToByteArray(isUnsigned: true, isBigEndian: true); // { 0x80, 0xFE }
value.GetByteCount(isUnsigned: true);                   // 2
value.TryWriteBytes(destination, out var written, isUnsigned: true, isBigEndian: true);

Not polyfilled

  • The BigInteger(ReadOnlySpan<byte>, bool, bool) constructor overload (also added in .NET Core 2.1) can't be polyfilled — C# extension members can't add constructors to an existing type.

Tests & docs

  • Added PolyShim.Tests/NetCore21/BigIntegerTests.cs covering signed/unsigned and little/big-endian combinations, zero value, negative overflow, and TryWriteBytes success/insufficient-buffer paths.
  • Regenerated Signatures.md.

…ore21)

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
@Tyrrrz Tyrrrz added the enhancement New feature or request label Oct 3, 2026
@Tyrrrz
Tyrrrz marked this pull request as ready for review October 3, 2026 20:11
Copilot AI balanced review requested due to automatic review settings October 3, 2026 20:11
@codecov

codecov Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.63014% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 88.20%. Comparing base (87ebebc) to head (9178814).

Files with missing lines Patch % Lines
PolyShim/NetCore21/BigInteger.cs 95.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            prime     #152      +/-   ##
==========================================
+ Coverage   88.17%   88.20%   +0.03%     
==========================================
  Files         349      351       +2     
  Lines        7821     7894      +73     
  Branches      558      562       +4     
==========================================
+ Hits         6896     6963      +67     
- Misses        755      759       +4     
- Partials      170      172       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

No blocking issues were identified in the implementation, compatibility guards, tests, or signature updates.

Review effort: Balanced
Findings: None

What changed in this PR

Adds three BigInteger byte-conversion polyfills for older target frameworks, extending .NET Core 2.1 API compatibility.

Changes:

  • Implements byte conversion, byte counting, and span writing with signedness and endianness support.
  • Adds tests for conversion options, overflow, and buffer handling.
  • Updates the signature catalog.
File Description
Signatures.md Lists the three new members and updates totals.
PolyShim/​NetCore21/​BigInteger.cs Implements the polyfills with framework guards.
PolyShim.Tests/​NetCore21/​BigIntegerTests.cs Tests conversion behavior and buffer handling.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@Tyrrrz
Tyrrrz merged commit 4e5ec5e into prime Oct 3, 2026
8 checks passed
@Tyrrrz
Tyrrrz deleted the copilot/add-polyfills-biginteger-tobytearray branch October 3, 2026 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants