Skip to content

[API Proposal]: MemoryExtensions.CommonSuffixLength<T> #127630

Description

@hamarb123

Background and motivation

I've run into wanting this a few times over the years - most recently, it was for subtracting two BigInt's (my own custom ones, which done similarly to the normal BigInteger, but slightly differently to the BCL ones) - but this is not the only case I've run into wanting this for (I could try to find other cases if wanted, but might take me a while to actually find the relevant code, as I don't remember off the top of my head where it is or what it was for).

This to me doesn't seem any different to having both IndexOf and LastIndexOf, despite IndexOf being used much more overall.

API Proposal

namespace System
{
    public static class MemoryExtensions
    {
+        public static int CommonSuffixLength<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> other);
+        public static int CommonSuffixLength<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> other, IEqualityComparer<T>? comparer = null);
    }
}

API Usage

var bits1 = ...;
var bits2 = ...;

if (bits1.Length == bits2.Length)
{
    var commonSuffixLength = bits1.CommonSuffixLength(bits2);
    bits1 = bits1[..^commonSuffixLength];
    bits2 = bits2[..^commonSuffixLength];
}

...

Alternative Designs

We could also add the this Span<T> ones for consistency with CommonPrefixLength, but idk if that's necessary with first class span these days, although it probably helps with languages like VB.NET using it.

If #126756 gets approved, then we probably want a public static int CommonSuffixLength(this ReadOnlySpan<char> other, StringComparison comparisonType); also to match it.

Risks

No response

Activity

  1. added
    api-suggestion
    on May 1, 2026
  2. added
    untriaged
    on May 1, 2026
  3. dotnet-policy-service commented on May 1, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-memory
    See info in area-owners.md if you want to be subscribed.

  4. removed
    api-suggestion
    untriaged
    on Aug 17, 2026
  5. added this to the Future milestone on Aug 17, 2026
  6. added
    api-ready-for-review
    on Aug 17, 2026
  7. bartonjs commented on Sep 8, 2026

    @bartonjs
    Member

    Video

    A good parallel to CommonPrefixLength, but the longer overload shouldn't have a defaulted comparer.

    namespace System
    {
        public static class MemoryExtensions
        {
            public static int CommonSuffixLength<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> other);
            public static int CommonSuffixLength<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> other, IEqualityComparer<T>? comparer);
        }
    }
  8. added
    api-approved
    and removed
    api-ready-for-review
    on Sep 8, 2026
  9. shubmittal commented on Sep 8, 2026

    @shubmittal

    I’d like to implement the two approved ReadOnlySpan<T>.CommonSuffixLength overloads, with tests for default and custom comparers and unequal span lengths. I’ll keep the scope to the approved API and its tests. Please let me know if anyone is already working on it. I’ll use GitHub Copilot for assistance and review the resulting changes.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedarea-System.Memory

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions