public static int ConvertToXRangeIndex(object value)
        {
            Conversion conversion;

            int val = Converter.TryConvertToInt32(value, out conversion);

            if (conversion != Conversion.None)
            {
                return(val);
            }

            IronMath.BigInteger bigval = Converter.TryConvertToBigInteger(value, out conversion);
            if (conversion != Conversion.None)
            {
                if (bigval <= Int32.MinValue)
                {
                    throw Ops.OverflowError("xrange() result has too many items");
                }
                if (bigval > Int32.MaxValue)
                {
                    throw Ops.OverflowError("long int too large to convert to int");
                }

                return(bigval.ToInt32());
            }

            Converter.TryConvertToDouble(value, out conversion);
            if (conversion != Conversion.None)
            {
                throw Ops.OverflowError("long int too large to convert to int");
            }
            else
            {
                throw Ops.TypeError("an integer is required");
            }
        }
Exemple #2
0
            public StatResult(ISequence statResult, [DefaultParameterValue(null)]object dict)
            {
                // dict is allowed by CPython's stat_result, but doesn't seem to do anything, so we ignore it here.

                if (statResult.GetLength() != 10) {
                    throw Ops.TypeError("stat_result() takes a 10-sequence ({0}-sequence given)", statResult.GetLength());
                }

                this.mode = Converter.ConvertToBigInteger(statResult[0]);
                this.ino = Converter.ConvertToBigInteger(statResult[1]);
                this.dev = Converter.ConvertToBigInteger(statResult[2]);
                this.nlink = Converter.ConvertToBigInteger(statResult[3]);
                this.uid = Converter.ConvertToBigInteger(statResult[4]);
                this.gid = Converter.ConvertToBigInteger(statResult[5]);
                this.size = Converter.ConvertToBigInteger(statResult[6]);
                this.atime = Converter.ConvertToBigInteger(statResult[7]);
                this.mtime = Converter.ConvertToBigInteger(statResult[8]);
                this.ctime = Converter.ConvertToBigInteger(statResult[9]);
            }
 /// <summary>
 /// Conversion routine TryConvertToBigInteger - converts object to BigInteger
 /// Try to avoid using this method, the goal is to ultimately remove it!
 /// </summary>
 internal static bool TryConvertToBigInteger(object value, out BigInteger result)
 {
     try {
         result = ConvertToBigInteger(value);
         return true;
     } catch {
         result = default(BigInteger);
         return false;
     }
 }
 private static object Power(long x, BigInteger y)
 {
     return LongOps.Power(BigInteger.Create(x), y);
 }
 private static object TrueDivide(double x, BigInteger y)
 {
     if (y == BigInteger.Zero) throw Ops.ZeroDivisionError();
     return x / y;
 }
            void WriteInteger(BigInteger val)
            {
                if (val == BigInteger.Zero) {
                    bytes.Add((byte)'l');
                    for(int i = 0; i<4; i++) bytes.Add(0);
                    return;
                }

                BigInteger mask = BigInteger.Create(short.MaxValue);
                uint startLen = (uint)val.Length;
                val = new BigInteger(val);

                bytes.Add((byte)'l');
                uint byteLen = ((startLen * 32) + 14) / 15; // len is in 32-bit multiples, we want 15-bit multiples
                bool fNeg = false;
                if (val < 0) {
                    fNeg = true;
                    val *= -1;
                }

                if (val <= short.MaxValue)
                    byteLen = 1;
                else if (val < (1 << 30)) {
                    byteLen = 2;
                }

                // write out length
                if (fNeg) {
                    WriteUInt32(uint.MaxValue - byteLen + 1);
                } else {
                    WriteUInt32(byteLen);
                }

                // write out value (15 bits at a time)
                while (val != 0) {
                    BigInteger res = (val & mask);
                    uint writeVal = res.ToUInt32();
                    bytes.Add((byte)((writeVal) & 0xff));
                    bytes.Add((byte)((writeVal >> 8) & 0xff));
                    val = val >> 15;
                }
            }
 private static object TrueDivide(BigInteger x, double y)
 {
     if (y == 0.0) {
         throw new DivideByZeroException();
     }
     return x.ToFloat64() / y;
 }
        private static object PowerMod(BigInteger x, BigInteger y, BigInteger z)
        {
            if (y < BigInteger.Zero) {
                throw Ops.TypeError("power", y, "power must be >= 0");
            }
            if (z == BigInteger.Zero) {
                throw Ops.ZeroDivisionError();
            }

            BigInteger result = x.ModPow(y, z);

            if (result >= BigInteger.Zero) {
                if (z < BigInteger.Zero) return result + z;
            } else {
                if (z > BigInteger.Zero) return result + z;
            }
            return result;
        }
        public static object RightShift(BigInteger x, object other)
        {
            ExtensibleLong el;
            ExtensibleInt ei;

            if (other is int) {
                return RightShift(x, (int)other);
            } else if (other is BigInteger) {
                BigInteger y = (BigInteger)other;
                if (y < BigInteger.Zero) {
                    throw Ops.ValueError("negative shift count");
                }
                int yint;
                if (y.AsInt32(out yint)) {
                    return RightShift(x, yint);
                }
            } else if (other is long) {
                long y = (long)other;
                if (y < 0) {
                    throw Ops.ValueError("negative shift count");
                }
                if (y <= Int32.MaxValue) {
                    return RightShift(x, (int)y);
                }
            } else if (other is bool) {
                return RightShift(x, (bool)other ? 1 : 0);
            } else if ((ei = other as ExtensibleInt) != null) {
                return ei.ReverseRightShift(x);
            } else if ((el = other as ExtensibleLong) != null) {
                return el.ReverseRightShift(x);
            } else if (other is byte) {
                return RightShift(x, (int)((byte)other));
            }
            return Ops.NotImplemented;
        }
 public static object ReverseXor(BigInteger x, object other)
 {
     return Xor(x, other);
 }
 public static object ReverseSubtract(BigInteger x, object other)
 {
     if (other is int) return ((int)other) - x;
     if (other is Complex64) return ((Complex64)other) - x;
     if (other is double) return ((double)other) - x;
     if (other is BigInteger) return ((BigInteger)other) - x;
     if (other is bool) return ((bool)other ? 1 : 0) - x;
     if (other is long) return ((long)other) - x;
     if (other is ExtensibleInt) return (((ExtensibleInt)other).value) - x;
     if (other is ExtensibleFloat) return (((ExtensibleFloat)other).value) - x;
     if (other is ExtensibleComplex) return ((ExtensibleComplex)other).value - x;
     return Ops.NotImplemented;
 }
        public static object ReverseRightShift(BigInteger x, object other)
        {
            ExtensibleLong el;
            ExtensibleInt ei;

            if (other is int) {
                return IntOps.RightShift((int)other, x);
            } else if (other is BigInteger) {
                return LongOps.RightShift((BigInteger)other, x);
            } else if (other is long) {
                return Int64Ops.RightShift((long)other, x);
            } else if (other is bool) {
                return IntOps.RightShift((bool)other ? 1 : 0, x);
            } else if ((ei = other as ExtensibleInt) != null) {
                return ei.RightShift(x);
            } else if ((el = other as ExtensibleLong) != null) {
                return el.RightShift(x);
            } else if (other is byte) {
                return IntOps.RightShift((int)(byte)other, x);
            }
            return Ops.NotImplemented;
        }
 public static object ReverseMultiply(BigInteger x, object other)
 {
     return Multiply(x, other);
 }
 public static object ReverseBitwiseOr(BigInteger x, object other)
 {
     return BitwiseOr(x, other);
 }
 public static object ReverseAdd(BigInteger x, object other)
 {
     return Add(x, other);
 }
 private static object PowerMod(BigInteger x, BigInteger y, object z)
 {
     if (z is int) {
         return PowerMod(x, y, BigInteger.Create((int)z));
     } else if (z is long) {
         return PowerMod(x, y, BigInteger.Create((long)z));
     } else if (z is BigInteger) {
         return PowerMod(x, y, (BigInteger)z);
     } else if (z == null) {
         return Power(x, y);
     }
     return Ops.NotImplemented;
 }
 public static object ToFloat(BigInteger self)
 {
     return self.ToFloat64();
 }
 private static object RightShift(BigInteger x, int y)
 {
     BigInteger q;
     if (y < 0) {
         throw Ops.ValueError("negative shift count");
     }
     if (x < BigInteger.Zero) {
         q = x >> y;
         BigInteger r = x - (q << y);
         if (r != BigInteger.Zero) q -= BigInteger.One; ;
     } else {
         q = x >> y;
     }
     return q;
 }
        public static object __lt__(BigInteger x, object y)
        {
            if (y == null) return false;

            BigInteger bi;
            if (Converter.TryConvertToBigInteger(y, out bi)) return x - bi < 0;
            return Ops.NotImplemented;
        }
 private static object TrueDivide(BigInteger x, BigInteger y)
 {
     if (y == BigInteger.Zero) {
         throw new DivideByZeroException();
     }
     return x.ToFloat64() / y.ToFloat64();
 }
 internal static object DivMod(BigInteger x, BigInteger y)
 {
     BigInteger div, mod;
     div = DivMod(x, y, out mod);
     return Tuple.MakeTuple(div, mod);
 }
 private static object ReverseTrueDivide(double x, BigInteger y)
 {
     return TrueDivide((double)y, x);
 }
 internal static object LeftShift(BigInteger x, ulong y)
 {
     if (y <= Int32.MaxValue) {
         return LeftShift(x, (int)y);
     }
     throw Ops.OverflowError("number too big");
 }
        /// <summary>
        /// BigInteger version of AppendBase.  Should be kept in sync w/ AppendBase
        /// </summary>
        private void AppendBaseBigInt(BigInteger origVal, char format, int radix)
        {
            BigInteger val = origVal;
            if (val < 0) val *= -1;

            // convert value to octal
            StringBuilder str = new StringBuilder();
            if (val == 0) str.Append('0');
            while (val != 0) {
                int digit = (int)(val % radix);
                if (digit < 10) str.Append((char)((digit) + '0'));
                else if (Char.IsLower(format)) str.Append((char)((digit - 10) + 'a'));
                else str.Append((char)((digit - 10) + 'A'));

                val /= radix;
            }

            // pad out for additional precision
            if (str.Length < opts.Precision) {
                int len = opts.Precision - str.Length;
                str.Append('0', len);
            }

            // pad result to minimum field width
            if (opts.FieldWidth != 0) {
                int signLen = (origVal < 0 || opts.SignChar) ? 1 : 0;
                int len = opts.FieldWidth - (str.Length + signLen);
                if (len > 0) {
                    // we account for the size of the alternate form, if we'll end up adding it.
                    if (opts.AltForm && NeedsAltForm(format, (!opts.LeftAdj && opts.ZeroPad) ? '0' : str[str.Length - 1])) {
                        len -= GetAltFormPrefixForRadix(format, radix).Length;
                    }

                    if (len > 0) {
                        // and finally append the right form
                        if (opts.LeftAdj) {
                            str.Insert(0, " ", len);
                        } else {
                            if (opts.ZeroPad) {
                                str.Append('0', len);
                            } else {
                                buf.Append(' ', len);
                            }
                        }
                    }
                }
            }

            // append the alternate form
            if (opts.AltForm && NeedsAltForm(format, str[str.Length - 1]))
                str.Append(GetAltFormPrefixForRadix(format, radix));

            // add any sign if necessary
            if (origVal < 0) {
                buf.Append('-');
            } else if (opts.SignChar) {
                buf.Append('+');
            } else if (opts.Space) {
                buf.Append(' ');
            }

            // append the final value
            for (int i = str.Length - 1; i >= 0; i--) {
                buf.Append(str[i]);
            }
        }
 internal static object ReverseDivMod(BigInteger x, BigInteger y)
 {
     return DivMod(y, x);
 }
 private static object TrueDivide(long x, BigInteger y)
 {
     if (y == BigInteger.Zero) {
         throw new DivideByZeroException();
     }
     return (double)x / y.ToFloat64();
 }
        private static BigInteger DivMod(BigInteger x, BigInteger y, out BigInteger r)
        {
            BigInteger rr;
            BigInteger qq;

            if (Object.ReferenceEquals(x, null)) throw Ops.TypeError("unsupported operands for div/mod: NoneType and long");
            if (Object.ReferenceEquals(y, null)) throw Ops.TypeError("unsupported operands for div/mod: long and NoneType");

            qq = BigInteger.DivRem(x, y, out rr);

            if (x >= BigInteger.Zero) {
                if (y > BigInteger.Zero) {
                    r = rr;
                    return qq;
                } else {
                    if (rr == BigInteger.Zero) {
                        r = rr;
                        return qq;
                    } else {
                        r = rr + y;
                        return qq - BigInteger.One;
                    }
                }
            } else {
                if (y > BigInteger.Zero) {
                    if (rr == BigInteger.Zero) {
                        r = rr;
                        return qq;
                    } else {
                        r = rr + y;
                        return qq - BigInteger.One;
                    }
                } else {
                    r = rr;
                    return qq;
                }
            }
        }
 /// <summary>
 /// ConvertToBigIntegerImpl Conversion Routine. If no conversion exists, returns false. Can throw OverflowException.
 /// </summary>
 private static bool ConvertToBigIntegerImpl(object value, out BigInteger result)
 {
     if (value is BigInteger) {
         result = (BigInteger)value; return true;
     } else if (value is Int32) {
         result = (BigInteger)(Int32)value; return true;
     } else if (value is Boolean) {
         result = (Boolean)value ? BigInteger.One : BigInteger.Zero; return true;
     } else if (value is ExtensibleInt) {
         result = (BigInteger)(Int32)((ExtensibleInt)value).value; return true;
     } else if (value is ExtensibleLong) {
         result = (BigInteger)((ExtensibleLong)value).Value; return true;
     } else if (value is Int64) {
         result = (BigInteger)(Int64)value; return true;
     } else if (value is Byte) {
         result = (BigInteger)(Byte)value; return true;
     } else if (value is SByte) {
         result = (BigInteger)(SByte)value; return true;
     } else if (value is Int16) {
         result = (BigInteger)(Int16)value; return true;
     } else if (value is UInt16) {
         result = (BigInteger)(UInt16)value; return true;
     } else if (value is UInt32) {
         result = (BigInteger)(UInt32)value; return true;
     } else if (value is UInt64) {
         result = (BigInteger)(UInt64)value; return true;
     } else if (value is Decimal) {
         result = (BigInteger)(Decimal)value; return true;
     } else if (value == null) {
         result = null; return true;
     }
     result = default(BigInteger);
     return false;
 }
 private static object LeftShift(BigInteger x, int y)
 {
     if (y < 0) {
         throw Ops.ValueError("negative shift count");
     }
     return x << y;
 }
 private static object LeftShift(BigInteger x, BigInteger y)
 {
     if (y < BigInteger.Zero) {
         throw Ops.ValueError("negative shift count");
     }
     int yint;
     if (y.AsInt32(out yint)) {
         return x << yint;
     }
     if (x == BigInteger.Zero) {
         return BigInteger.Zero;
     } else {
         throw Ops.OverflowError("number too big");
     }
 }
 public static object PowerMod(BigInteger x, object y, object z)
 {
     if (y is int) {
         return PowerMod(x, (int)y, z);
     } else if (y is long) {
         return PowerMod(x, BigInteger.Create((long)y), z);
     } else if (y is BigInteger) {
         return PowerMod(x, (BigInteger)y, z);
     }
     return Ops.NotImplemented;
 }