Add string.Truncate(int charCount) and string.TruncateBytes(int byteCount, Encoding?)#82
Conversation
…ault and tests Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/558e71dc-baee-4a84-b68b-9e7d1f8c3800 Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
…ogate check Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/558e71dc-baee-4a84-b68b-9e7d1f8c3800 Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
…ccordingly Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/82caa6e5-eb8d-419b-9d8d-a0d7fc27789d Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
There was a problem hiding this comment.
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.
…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>
|
Addressed all review comments in 3d55ce9: renamed |
| "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"); |
There was a problem hiding this comment.
Can we add assertions with Encoding.ASCII for other strings in this test (one each, similar to this one) @copilot
There was a problem hiding this comment.
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>
…n assertions Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/482543fb-7466-4743-a68f-80c540547a74 Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
Adds two distinct truncation methods on
stringwith clear, non-ambiguous names.Truncate(int charCount)— truncates to at mostcharCountUTF-16 characters (simple slice)TruncateBytes(int byteCount, Encoding? encoding = null)— truncates so the encoded byte length doesn't exceedbyteCount; defaults to UTF-8. Uses binary search over character count and guards against splitting surrogate pairs.