Skip to content

String Comparisons #151

@caesay

Description

@caesay

Before I get started on this, I wanted your thoughts on the API.

There are many ways to compare strings. Some .net best practice guide is here: https://learn.microsoft.com/en-us/dotnet/standard/base-types/best-practices-strings?redirectedfrom=MSDN

The C# enum is:

    //     Specifies the culture, case, and sort rules to be used by certain overloads of
    //     the System.String.Compare(System.String,System.String) and System.String.Equals(System.Object)
    //     methods.
    public enum StringComparison
    {
        //     Compare strings using culture-sensitive sort rules and the current culture.
        CurrentCulture = 0,

        //     Compare strings using culture-sensitive sort rules, the current culture, and
        //     ignoring the case of the strings being compared.
        CurrentCultureIgnoreCase = 1,

        //     Compare strings using culture-sensitive sort rules and the invariant culture.
        InvariantCulture = 2,

        //     Compare strings using culture-sensitive sort rules, the invariant culture, and
        //     ignoring the case of the strings being compared.
        InvariantCultureIgnoreCase = 3,

        //     Compare strings using ordinal (binary) sort rules.
        Ordinal = 4,

        //     Compare strings using ordinal (binary) sort rules and ignoring the case of the
        //     strings being compared.
        OrdinalIgnoreCase = 5
    }

We could implement string.Compare(otherString, StringComparison), but let me know if you agree. The string.Equal() method can then just translate to Compare() == 0.

If we want to expose all of these options, then we will need to get a bit clever in some of the other languages. Particularly around InvariantCulture, which appears to be an artificial / unchanging culture designed by Microsoft to not be country / current locale specific. We could drop support for Invariant completely, or we could fall back to CurrentCulture implementation on languages which do not support it.

C

I'm not too sure about this one, but I think the following should work:

  • CurrentCulture: utf8_collate
  • CurrentCultureIgnoreCase: utf8_casefold followed by strcoll
  • Ordinal: strcmp
  • OrdinalIgnoreCase: utf8_casefold followed by strcmp
  • InvariantCulture / InvariantCultureIgnoreCase: no equivalent unless we use the win32 functions.

C++ Win32

  • Ordinal / OrdinalIgnoreCase: CompareStringOrdinal
  • InvariantCulture / InvariantCultureIgnoreCase / CurrentCulture / CurrentCultureIgnoreCase can all be done with CompareStringEx

C++ ICU

JS / TS

  • CurrentCulture: s1.localeCompare(s2, undefined, { sensitivity: "variant" })
  • CurrentCultureIgnoreCase: s1. localeCompare(s2, undefined, { sensitivity: "base" })
  • Ordinal: s1 < s2
  • OrdinalIgnoreCase: s1.toUpperCase() < s2.toUpperCase()
  • InvariantCulture / InvariantCultureIgnoreCase: no equivalent.

Java

D

Python

Swift

OpenCL

Ignore / not supported

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions