Skip to content

[API Proposal]: MemoryExtensions.CommonPrefixLength with StringComparison parameter #126756

Open

Description

@jnm2

Background and motivation

Presciently, the api review for MemoryExtensions.CommonPrefixLength<T> noted:

  • We might need to add an overload that is specific to char and takes StringComparison

There are two major reasons to add this overload:

  1. It enables vectorization when doing an ordinalignorecase comparison. (Performance)
  2. It no longer requires you to implement your own IEqualityComparer<char> to gain the desired behavior. (Usability)

Currently, if you are looking for a common prefix case-insensitively among strings, you have to entirely give up on vectorization by passing an IEqualityComparer<char> (which you also have to define yourself) which will then be called into for every char.

Prior art is the MemoryExtensions.Equals extension on spans of chars that takes a StringComparison parameter. The rationale that applied there, applies here.

API Proposal

namespace System;

public static class MemoryExtensions
{
    extension(ReadOnlySpan<char> span)
    {
        public int CommonPrefixLength(ReadOnlySpan<char> other, StringComparison comparisonType);
    }
}

ℹ️ An extension is not defined on Span<T>. This is due to the C# 14 language feature first-class spans which enables the ReadOnlySpan<T> extensions to appear when dotting off any Span<T> expression.

API Usage

Finding a common base folder among a bunch of file and folder paths, where paths are case-insensitive:

public static string GetCommonPath(params IEnumerable<string> paths)
{
    using var enumerator = paths.GetEnumerator();

    if (!enumerator.MoveNext())
        return string.Empty;

    var firstString = enumerator.Current;
    var commonLength = firstString.Length;
    if (commonLength == 0)
        return firstString;

    var first = firstString.AsSpan();

    while (enumerator.MoveNext())
    {
        var current = enumerator.Current;
        var newLength = first[..commonLength].CommonPrefixLength(current, StringComparison.OrdinalIgnoreCase);

        var atSegmentBoundary =
            (newLength == commonLength || first[newLength] is '/' or '\\') 
            && (newLength == current.Length || current[newLength] is '/' or '\\');

        commonLength = atSegmentBoundary
            ? newLength
            : first[..newLength].LastIndexOfAny('/', '\\');

        if (commonLength <= 0)
            return "";
    }

    return commonLength == first.Length ? firstString : first[..commonLength].ToString();
}

Alternative Designs

No response

Risks

No response

Activity

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

    @dotnet-policy-service
    Contributor

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

  4. grbell-ms commented on Apr 10, 2026

    @grbell-ms
    Member

    There's no guarantee that a common prefix between two strings is the same length in both strings. Which length would it return?

  5. jnm2 commented on Apr 11, 2026

    @jnm2
    ContributorAuthor

    Does ordinalignorecase specifically bring that guarantee?

    Perhaps the return could be ReadOnlySpan<char>.

  6. removed
    untriaged
    on Aug 16, 2026
  7. added this to the Future milestone on Aug 16, 2026
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-suggestionarea-System.Runtime

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions