Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String Comparisons #151

Open
caesay opened this issue Feb 22, 2024 · 1 comment
Open

String Comparisons #151

caesay opened this issue Feb 22, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@caesay
Copy link
Contributor

caesay commented Feb 22, 2024

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

@pfusik pfusik added the enhancement New feature or request label Feb 22, 2024
@pfusik
Copy link
Collaborator

pfusik commented Feb 29, 2024

I think that starting with .NET's StringComparison is way too ambitious. I would start with just string.EqualsIgnoreCase (not the ordering comparisons) and list what the target languages provide and only then decide what to implement. Does "culture" affect that? I understand different human languages have different alphabets, with possibly overlapping letters, but does equality depend on the culture?

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

No branches or pull requests

2 participants