Skip to content

Use range indexer in SubstringAfterLast#62

Merged
Tyrrrz merged 3 commits into
masterfrom
copilot/update-stringextensions-method
Feb 27, 2026
Merged

Use range indexer in SubstringAfterLast#62
Tyrrrz merged 3 commits into
masterfrom
copilot/update-stringextensions-method

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 27, 2026

  • Replace str.Substring(index + sub.Length, str.Length - index - sub.Length) with str[(index + sub.Length)..] in SubstringAfterLast
  • Fix formatting: use single-line ternary consistent with SubstringUntil and SubstringUntilLast
Original prompt

Problem

In GitHubActionsTestLogger/Utils/Extensions/StringExtensions.cs, the SubstringAfterLast method uses the old string.Substring(startIndex, length) overload, which requires manually computing the length:

public string SubstringAfterLast(
    string sub,
    StringComparison comparison = StringComparison.Ordinal
)
{
    var index = str.LastIndexOf(sub, comparison);
    return index >= 0
        ? str.Substring(index + sub.Length, str.Length - index - sub.Length)
        : "";
}

This can be simplified to use the range indexer str[(index + sub.Length)..], consistent with how SubstringUntil and SubstringUntilLast are already written in the same file (str[..index]).

Fix

Replace the Substring(startIndex, length) call with a range slice:

public string SubstringAfterLast(
    string sub,
    StringComparison comparison = StringComparison.Ordinal
)
{
    var index = str.LastIndexOf(sub, comparison);
    return index >= 0
        ? str[(index + sub.Length)..]
        : "";
}

No functional behavior changes.

This pull request was created from Copilot chat.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor SubstringAfterLast method to use range indexer Use range indexer in SubstringAfterLast Feb 27, 2026
@Tyrrrz Tyrrrz added the enhancement New feature or request label Feb 27, 2026
@Tyrrrz Tyrrrz marked this pull request as ready for review February 27, 2026 11:44
Copilot AI review requested due to automatic review settings February 27, 2026 11:44
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.46%. Comparing base (1d89cf4) to head (05fec3c).

Additional details and impacted files
@@           Coverage Diff           @@
##           master      #62   +/-   ##
=======================================
  Coverage   84.46%   84.46%           
=======================================
  Files          23       23           
  Lines         721      721           
  Branches       85       85           
=======================================
  Hits          609      609           
  Misses         58       58           
  Partials       54       54           

☔ 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.

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 pull request refactors the SubstringAfterLast method in StringExtensions.cs to use modern C# range indexer syntax for improved code consistency and readability. The change replaces verbose Substring(startIndex, length) arithmetic with the cleaner range indexer syntax str[(index + sub.Length)..], aligning with the existing implementation patterns of SubstringUntil and SubstringUntilLast in the same file.

Changes:

  • Simplified string slicing in SubstringAfterLast by replacing Substring method call with range indexer syntax

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

@Tyrrrz
Copy link
Copy Markdown
Owner

Tyrrrz commented Feb 27, 2026

@copilot fix formatting

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

Copilot AI commented Feb 27, 2026

@copilot fix formatting

Fixed in db08693 — collapsed the multi-line ternary to a single line and flipped the condition to index < 0, matching the style of SubstringUntil and SubstringUntilLast.

github-actions Bot pushed a commit to saan800/github that referenced this pull request May 12, 2026
…#255)

Updated
[GitHubActionsTestLogger](https://github.com/Tyrrrz/GitHubActionsTestLogger)
from 3.0.0 to 3.0.4.

<details>
<summary>Release notes</summary>

_Sourced from [GitHubActionsTestLogger's
releases](https://github.com/Tyrrrz/GitHubActionsTestLogger/releases)._

## 3.0.4

<!-- Release notes generated using configuration in .github/release.yml
at 3.0.4 -->

## What's Changed
### Enhancements
* Add PowerKit (prerelease), update PolyShim and CSharpier, remove
duplicate utilities by @​Copilot in
Tyrrrz/GitHubActionsTestLogger#72
### Bugs
* Explicitly dispose `summaryWriter` before reading file in truncation
tests by @​Copilot in
Tyrrrz/GitHubActionsTestLogger#76
* Fix GITHUB_STEP_SUMMARY size limit exceeded with non-ASCII characters
by @​Copilot in
Tyrrrz/GitHubActionsTestLogger#75


**Full Changelog**:
Tyrrrz/GitHubActionsTestLogger@3.0.3...3.0.4

## 3.0.3

## What's Changed
* Truncate or omit step summary when approaching GitHub's 1 MiB limit by
@​Copilot in Tyrrrz/GitHubActionsTestLogger#68
* Pin Microsoft.Testing.Platform reference to 2.0.0 by @​Copilot in
Tyrrrz/GitHubActionsTestLogger#70


**Full Changelog**:
Tyrrrz/GitHubActionsTestLogger@3.0.2...3.0.3

## 3.0.2

## What's Changed
* Bump the nuget group with 4 updates by @​dependabot[bot] in
Tyrrrz/GitHubActionsTestLogger#59
* Bump the nuget group with 1 update by @​dependabot[bot] in
Tyrrrz/GitHubActionsTestLogger#60
* Migrate to Centralized NuGet Package Management (CPM) by @​Copilot in
Tyrrrz/GitHubActionsTestLogger#61
* Use range indexer in `SubstringAfterLast` by @​Copilot in
Tyrrrz/GitHubActionsTestLogger#62
* Bump the nuget group with 7 updates by @​dependabot[bot] in
Tyrrrz/GitHubActionsTestLogger#63
* Fix build errors from obsolete MTP API usage by @​Copilot in
Tyrrrz/GitHubActionsTestLogger#64
* Remove explicit Microsoft.SourceLink.GitHub package reference by
@​Copilot in Tyrrrz/GitHubActionsTestLogger#65
* Add strong naming by @​mgravell in
Tyrrrz/GitHubActionsTestLogger#67

## New Contributors
* @​Copilot made their first contribution in
Tyrrrz/GitHubActionsTestLogger#61
* @​mgravell made their first contribution in
Tyrrrz/GitHubActionsTestLogger#67

**Full Changelog**:
Tyrrrz/GitHubActionsTestLogger@3.0.1...3.0.2

## 3.0.1

**Full Changelog**:
Tyrrrz/GitHubActionsTestLogger@3.0...3.0.1

Commits viewable in [compare
view](Tyrrrz/GitHubActionsTestLogger@3.0...3.0.4).
</details>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
This was referenced May 12, 2026
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