Skip to content

[API] Add ASCIIEncoding.IsEquivalentOrdinal(ROS<char>, ROS<byte>) #29134

@benaadams

Description

@benaadams

From dotnet/aspnetcore#8374 (comment)

Cross encoding compare

namespace System.Text
{
    public partial class ASCIIEncoding : Encoding
    {
        public static bool IsEquivalentOrdinal(
            ReadOnlySpan<char> chars, 
            ReadOnlySpan<byte> asciiBytes);
    }
}

This is method to test whether a sequence of ascii bytes is the same as a string; without having to first convert the bytes to a sting and allocate and then use string equality. (e.g. compare ascii bytes to a UTF-16 string)

Used in dotnet/aspnetcore#8374 to drop steady state string allocations to zero (as previous materialized strings can be reused when they match the on-wire bytes)

e.g.

image

Could extend with an bool ignoreCase = false if desired, however that is not required for this particular use case.

/cc @GrabYourPitchforks @davidfowl @halter73

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions