Пример #1
0
        public static bool Ceq(string sx, PhpValue y)
        {
            switch (y.TypeCode)
            {
            case PhpTypeCode.Boolean: return(Convert.ToBoolean(sx) == y.Boolean);

            case PhpTypeCode.Long: return(Compare(sx, y.Long) == 0);

            case PhpTypeCode.Double: return(Compare(sx, y.Double) == 0);

            case PhpTypeCode.PhpArray: return(false);

            case PhpTypeCode.String: return(Ceq(sx, y.String));

            case PhpTypeCode.MutableString: return(Ceq(sx, y.MutableString.ToString()));

            case PhpTypeCode.Object: return(CompareStringToObject(sx, y.Object) == 0);

            case PhpTypeCode.Alias: return(Ceq(sx, y.Alias.Value));

            case PhpTypeCode.Null: return(sx.Length == 0);
            }

            throw InvalidTypeCodeException("ceq", PhpVariable.TypeNameString, PhpVariable.GetDebugType(y));
        }
Пример #2
0
        public static int Compare(PhpString.Blob sx, PhpValue y)
        {
            switch (y.TypeCode)
            {
            case PhpTypeCode.Null: return((sx.Length == 0) ? 0 : 1);

            case PhpTypeCode.Boolean: return(Compare(sx.ToBoolean(), y.Boolean));

            case PhpTypeCode.Long: return(Compare(sx.ToString(), y.Long));

            case PhpTypeCode.Double: return(Compare(sx.ToString(), y.Double));

            case PhpTypeCode.PhpArray: return(-1);      // - 1 * (array.CompareTo(string))

            case PhpTypeCode.String: return(Compare(sx.ToString(), y.String));

            case PhpTypeCode.MutableString: return(Compare(sx, y.MutableStringBlob));

            case PhpTypeCode.Object: return(CompareStringToObject(sx.ToString(), y.Object));

            case PhpTypeCode.Alias: return(Compare(sx, y.Alias.Value));
            }

            throw InvalidTypeCodeException("compare", PhpVariable.TypeNameString, PhpVariable.GetDebugType(y));
        }
Пример #3
0
        public static int Compare(double dx, PhpValue y)
        {
            switch (y.TypeCode)
            {
            case PhpTypeCode.Null: return((dx == 0.0) ? 0 : 1);

            case PhpTypeCode.Boolean: return(Compare(dx != 0.0, y.Boolean));

            case PhpTypeCode.Long: return(Compare(dx, (double)y.Long));

            case PhpTypeCode.Double: return(Compare(dx, y.Double));

            case PhpTypeCode.PhpArray: return(-1);

            case PhpTypeCode.String: return(-Compare(y.String, dx));

            case PhpTypeCode.MutableString: return(-Compare(y.MutableString.ToString(), dx));

            case PhpTypeCode.Object:
                // we cannot use ToNumber(object) because it treats some builtin classes as numbers
                if (y.Object is decimal yd)
                {
                    return(Compare(dx, (double)yd));
                }
                // Notice: Object of class {0} could not be converted to int
                PhpException.Throw(PhpError.Notice, string.Format(Resources.ErrResources.object_could_not_be_converted, PhpVariable.GetDebugType(y), PhpVariable.TypeNameDouble));
                return(Compare(dx, 1.0));    // object is treated as '1'

            case PhpTypeCode.Alias: return(Compare(dx, y.Alias.Value));
            }

            throw InvalidTypeCodeException("compare", PhpVariable.TypeNameDouble, PhpVariable.GetDebugType(y));
        }
Пример #4
0
        public static int Compare(object x, PhpValue y)
        {
            Debug.Assert(x != null);

            if (x.Equals(y.Object))
            {
                return(0);
            }
            if (x is IPhpComparable)
            {
                return(((IPhpComparable)x).Compare(y));
            }
            if (y.Object is IPhpComparable)
            {
                return(-((IPhpComparable)y.Object).Compare(PhpValue.FromClass(x)));
            }
            if (x is decimal xd)
            {
                return(Compare((double)xd, y));
            }

            switch (y.TypeCode)
            {
            case PhpTypeCode.Null:
                return(1);

            case PhpTypeCode.Boolean:
                return(y.Boolean ? 0 : 1);

            case PhpTypeCode.Long:
                // Notice: Object of class {0} could not be converted to int
                PhpException.Throw(PhpError.Notice, string.Format(Resources.ErrResources.object_could_not_be_converted, PhpVariable.GetDebugType(y), PhpVariable.TypeNameInt));
                return(Compare(1L, y.Long));

            case PhpTypeCode.Double:
                // Notice: Object of class {0} could not be converted to float
                PhpException.Throw(PhpError.Notice, string.Format(Resources.ErrResources.object_could_not_be_converted, PhpVariable.GetDebugType(y), PhpVariable.TypeNameFloat));
                return(Compare(1L, y.Long));

            case PhpTypeCode.String:
                return(-CompareStringToObject(y.String, x));

            case PhpTypeCode.Object:
                Debug.Assert(y.Object != null);
                var result = CompareObjects(x, y.Object, PhpComparer.Default, out var incomparable);
                if (incomparable)
                {
                    PhpException.Throw(PhpError.Warning,
                                       Resources.ErrResources.incomparable_objects_compared_exception,
                                       x.GetPhpTypeInfo().Name,
                                       y.Object.GetPhpTypeInfo().Name);
                    return(1);
                }
                return(result);

            case PhpTypeCode.Alias:
                return(Compare(x, y.Alias.Value));

            default:
                PhpException.Throw(PhpError.Notice,
                                   string.Format(Resources.ErrResources.incomparable_objects_compared_exception,
                                                 x.GetPhpTypeInfo().Name,
                                                 PhpVariable.GetDebugType(y)));

                return(0);
            }
        }