sq / JSIL

CIL to Javascript Compiler
http://jsil.org/
Other
1.73k stars 240 forks source link

StringComparer #953

Open dosh1974 opened 8 years ago

dosh1974 commented 8 years ago

Hi all!

I'm new to JSIL and I must say it looks great! I have a large cross platform code base that I'm trying to port over to HTML5 (for fun to start off with at least). I initially had some problems with my PCL's but I managed to solve those. Next hurdle seems to be that the StringComparer type isn't implemented? Is there a plan for adding this - or if you could advice how I would go about adding it myself?

Thanks in advance!

iskiselev commented 8 years ago

I use next implementation:

(function CultureAwareComparer$Members() {
    var $, $thisType;

    /*var $T02 = function () {
        return ($T02 = JSIL.Memoize($jsilcore.System.StringComparer))();
    };*/

    function CultureAwareComparer__ctor$00(culture, ignoreCase) {
        //$T02().prototype._ctor.call(this);
        this._compareInfo = culture.get_Name();
        this._ignoreCase = ignoreCase;
    }

    function CultureAwareComparer_Compare(x, y) {
        if (x === null && y === null) {
            return 0;
        }
        if (x === null) {
            return -1;
        }
        if (y === null) {
            return 1;
        }

        if (this._compareInfo === "") {
            if (this._ignoreCase) {
                x = x.toUpperCase();
                y = y.toUpperCase();
            }
            if (x < y) return -1;
            if (x > y) return 1;
            return 0;
        }

        if (this._ignoreCase) {
            x = x.toLocaleUpperCase();
            y = y.toLocaleUpperCase();
        }
        return x.localeCompare(y);
    }

    function CultureAwareComparer_Equals$02(x, y) {
        return this.Compare(x, y) === 0;
    }

    JSIL.ImplementExternals("System.CultureAwareComparer", function ($) {
        $.Method({ Static: false, Public: false }, ".ctor",
            new JSIL.MethodSignature(null, [$jsilcore.TypeRef("System.Globalization.CultureInfo"), $.Boolean], []),
            CultureAwareComparer__ctor$00
        );

        $.Method({ Static: false, Public: true, Virtual: true }, "Compare",
            new JSIL.MethodSignature($.Int32, [$.String, $.String], []),
            CultureAwareComparer_Compare
        );

        $.Method({ Static: false, Public: true, Virtual: true }, "Equals",
            new JSIL.MethodSignature($.Boolean, [$.String, $.String], []),
            CultureAwareComparer_Equals$02
        );

        // TODO: Call base methods?
        $.Method({ Static: false, Public: true, Virtual: true }, "GetHashCode",
            new JSIL.MethodSignature($.Int32, [$.Object], []),
            function StringComparer_GetHashCode_Obj(input) {
                return 0;
            }
        );

        $.Method({ Static: false, Public: true, Virtual: true }, "GetHashCode",
            new JSIL.MethodSignature($.Int32, [$.String], []),
            function StringComparer_GetHashCode_String(input) {
                return 0;
            }
        );

        //$.Field({ Static: false, Public: false }, "_culture", $jsilcore.TypeRef("System.String"));
        //$.Field({ Static: false, Public: false }, "_ignoreCase", $.Boolean);

        //return function (newThisType) { $thisType = newThisType; };
    });
})();

(function OrdinalComparer$Members() {
    var $, $thisType;

    /*var $T01 = function () {
        return ($T01 = JSIL.Memoize($jsilcore.System.StringComparer))();
    };*/

    function OrdinalComparer__ctor(ignoreCase) {
        //$T01().prototype._ctor.call(this);
        this._ignoreCase = ignoreCase;
    };

    function OrdinalComparer_Compare(x, y) {
        if (x === null && y === null) {
            return 0;
        }
        if (x === null) {
            return -1;
        }
        if (y === null) {
            return 1;
        }

        if (this._ignoreCase) {
            x = x.toUpperCase();
            y = y.toUpperCase();
        }
        if (x < y) return -1;
        if (x > y) return 1;
        return 0;
    }

    function OrdinalComparer_Equals$00(x, y) {
        return this.Compare(x, y) === 0;
    };

    JSIL.ImplementExternals("System.OrdinalComparer", function ($) {
        $.Method({ Static: false, Public: false }, ".ctor",
            new JSIL.MethodSignature(null, [$.Boolean], []),
            OrdinalComparer__ctor
        );

        $.Method({ Static: false, Public: true, Virtual: true }, "Compare",
          new JSIL.MethodSignature($.Int32, [$.String, $.String], []),
          OrdinalComparer_Compare
        );

        $.Method({ Static: false, Public: true, Virtual: true }, "Equals",
          new JSIL.MethodSignature($.Boolean, [$.String, $.String], []),
          OrdinalComparer_Equals$00
        );

        // TODO: Call base methods?
        $.Method({ Static: false, Public: true, Virtual: true }, "GetHashCode",
            new JSIL.MethodSignature($.Int32, [$.Object], []),
            function StringComparer_GetHashCode_Obj(input) {
                return 0;
            }
        );

        $.Method({ Static: false, Public: true, Virtual: true }, "GetHashCode",
            new JSIL.MethodSignature($.Int32, [$.String], []),
            function StringComparer_GetHashCode_String(input) {
                return 0;
            }
        );

        //$.Field({ Static: false, Public: false }, "_ignoreCase", $.Boolean);

        //return function (newThisType) { $thisType = newThisType; };
    });

})();

(function () {
    JSIL.ImplementExternals("System.StringComparer", function ($) {
        $.Method({ Static: true, Public: true }, "get_Ordinal",
            JSIL.MethodSignature.Return($jsilcore.TypeRef("System.StringComparer")),
            function Compile() {
                return new $jsilcore.System.OrdinalComparer(false);
            }
        );

        $.Method({ Static: true, Public: true }, "get_OrdinalIgnoreCase",
            JSIL.MethodSignature.Return($jsilcore.TypeRef("System.StringComparer")),
            function Compile() {
                return new $jsilcore.System.OrdinalComparer(true);
            }
        );

        $.Method({ Static: true, Public: true }, "Create",
            new JSIL.MethodSignature($jsilcore.TypeRef("System.StringComparer"), [$jsilcore.TypeRef("System.Globalization.CultureInfo"), $.Boolean], []),
            function Create(culture, ignoreCase) {
                return new $jsilcore.System.CultureAwareComparer(culture, ignoreCase);
            }
        );

        $.Method({ Static: false, Public: true, Virtual: true }, "GetHashCode",
            new JSIL.MethodSignature($.Int32, [$.Object], []),
            function StringComparer_GetHashCode_Obj(input) {
                return 0;
            }
        );

        $.Method({ Static: false, Public: true, Virtual: true }, "GetHashCode",
            new JSIL.MethodSignature($.Int32, [$.String], []),
            function StringComparer_GetHashCode_String(input) {
                return 0;
            }
        );
    });
})();

I attach it through record in assetToLoad: var assetsToLoad = [["Library", "StringCompare.js"]];