Skip to content

Add string.Truncate(int charCount) and string.TruncateBytes(int byteCount, Encoding?)#82

Merged
Tyrrrz merged 7 commits into
primefrom
copilot/add-string-truncate-extension
May 2, 2026
Merged

Add string.Truncate(int charCount) and string.TruncateBytes(int byteCount, Encoding?)#82
Tyrrrz merged 7 commits into
primefrom
copilot/add-string-truncate-extension

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 2, 2026

Adds two distinct truncation methods on string with clear, non-ambiguous names.

  • Truncate(int charCount) — truncates to at most charCount UTF-16 characters (simple slice)
  • TruncateBytes(int byteCount, Encoding? encoding = null) — truncates so the encoded byte length doesn't exceed byteCount; defaults to UTF-8. Uses binary search over character count and guards against splitting surrogate pairs.
"héllo".Truncate(3)                        // "hél"
"héllo".TruncateBytes(4)                   // "hél"  (h=1, é=2, l=1 bytes)
"héllo".TruncateBytes(3, Encoding.UTF8)    // "hé"   (h=1, é=2 bytes)
"a𝄞b".TruncateBytes(5)                    // "a𝄞"  (a=1, 𝄞=4 bytes; surrogate pair kept intact)

Copilot AI and others added 3 commits May 2, 2026 09:22
@Tyrrrz Tyrrrz added the enhancement New feature or request label May 2, 2026
@Tyrrrz Tyrrrz marked this pull request as ready for review May 2, 2026 09:29
Copilot AI review requested due to automatic review settings May 2, 2026 09:29
Copy link
Copy Markdown
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 adds two new string extension APIs to PowerKit for truncating text either by UTF-16 character count or by encoded byte count, extending the library’s existing collection of convenience extensions for core BCL types.

Changes:

  • Adds string.TruncateBytes(int byteCount, Encoding? encoding = null) with UTF-8 as the default encoding.
  • Keeps the existing Truncate(int charCount) API alongside the new byte-based overload to make the truncation mode explicit.
  • Adds unit tests covering basic ASCII, multi-byte UTF-8, surrogate-pair, default-encoding, and ASCII scenarios.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
PowerKit/Extensions/StringExtensions.cs Adds the new byte-based truncation implementation and adjacent string truncation API docs.
PowerKit.Tests/Extensions/StringExtensionsTests.cs Adds tests for the new TruncateBytes behavior across several common encoding/character scenarios.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread PowerKit/Extensions/StringExtensions.cs Outdated
Comment thread PowerKit/Extensions/StringExtensions.cs
Comment thread PowerKit.Tests/Extensions/StringExtensionsTests.cs
Copy link
Copy Markdown
Owner

@Tyrrrz Tyrrrz left a comment

Choose a reason for hiding this comment

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

Comment thread PowerKit/Extensions/StringExtensions.cs Outdated
Comment thread PowerKit.Tests/Extensions/StringExtensionsTests.cs Outdated
Comment thread PowerKit.Tests/Extensions/StringExtensionsTests.cs Outdated
Comment thread PowerKit.Tests/Extensions/StringExtensionsTests.cs Outdated
Comment thread PowerKit.Tests/Extensions/StringExtensionsTests.cs Outdated
Comment thread PowerKit/Extensions/StringExtensions.cs Outdated
…fix surrogate-safe probe, clean up tests

Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/c0e9ec28-c1d7-4884-983b-50847e26373c

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 2, 2026

@copilot

Addressed all review comments in 3d55ce9: renamed encactualEncoding and lo/hicharLo/charHi, added ArgumentOutOfRangeException guard for negative byteCount, fixed surrogate-safe probing via try/catch (EncoderFallbackException) in the binary search, and cleaned up tests (added // Act & assert, removed the two low-value assertions, fixed the "Emoji" label to "Supplementary character", dropped the explicit null arg, added a test for the negative byteCount case).

Copilot finished work on behalf of Tyrrrz May 2, 2026 11:11
Copilot AI requested a review from Tyrrrz May 2, 2026 11:11
"hello".TruncateBytes(10).Should().Be("hello");
"hello".TruncateBytes(3).Should().Be("hel");
"hello".TruncateBytes(0).Should().Be("");
"hello".TruncateBytes(3, Encoding.ASCII).Should().Be("hel");
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Can we add assertions with Encoding.ASCII for other strings in this test (one each, similar to this one) @copilot

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in b5f363a — added "héllo".TruncateBytes(3, Encoding.ASCII).Should().Be("hél") and "a𝄞b".TruncateBytes(1, Encoding.ASCII).Should().Be("a"). Also fixed a pre-existing incorrect assertion: "a𝄞b".TruncateBytes(10) should return "a𝄞b" (the full string, since it's only 6 UTF-8 bytes).

…eBytes(10) expected value for "a𝄞b"

Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/0cb01706-821f-4688-bc65-1f87cc6d06b9

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Copilot finished work on behalf of Tyrrrz May 2, 2026 11:28
Copilot AI requested a review from Tyrrrz May 2, 2026 11:28
Comment thread PowerKit.Tests/Extensions/StringExtensionsTests.cs Outdated
Copilot finished work on behalf of Tyrrrz May 2, 2026 11:34
Copilot AI requested a review from Tyrrrz May 2, 2026 11:34
@codecov
Copy link
Copy Markdown

codecov Bot commented May 2, 2026

Codecov Report

❌ Patch coverage is 92.10526% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.82%. Comparing base (9e825ee) to head (14cde3e).
⚠️ Report is 1 commits behind head on prime.

Files with missing lines Patch % Lines
PowerKit/Extensions/StringExtensions.cs 85.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            prime      #82      +/-   ##
==========================================
- Coverage   93.85%   93.82%   -0.03%     
==========================================
  Files         136      136              
  Lines        2717     2755      +38     
  Branches      217      223       +6     
==========================================
+ Hits         2550     2585      +35     
- Misses        123      125       +2     
- Partials       44       45       +1     

☔ View full report in Codecov by Sentry.
📢 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.

@Tyrrrz Tyrrrz merged commit 87605c3 into prime May 2, 2026
7 checks passed
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