fusionlanguage / fut

Fusion programming language. Transpiling to C, C++, C#, D, Java, JavaScript, Python, Swift, TypeScript and OpenCL C.
https://fusion-lang.org
GNU General Public License v3.0
1.77k stars 55 forks source link

String Comparisons #151

Open caesay opened 9 months ago

caesay commented 9 months ago

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:

C++ Win32

C++ ICU

JS / TS

Java

D

Python

Swift

OpenCL

Ignore / not supported

pfusik commented 9 months ago

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?