Exemple #1
0
        internal static string /*!*/ ConvertToHostString(BigInteger /*!*/ address)
        {
            Assert.NotNull(address);
            ulong u;

            if (address.AsUInt64(out u))
            {
                if (u <= UInt32.MaxValue)
                {
                    // IPv4:
                    return(ConvertToHostString((uint)u));
                }
                else
                {
                    // IPv6:
                    byte[] bytes = new byte[8];
                    for (int i = bytes.Length - 1; i >= 0; --i)
                    {
                        bytes[i] = (byte)(u & 0xff);
                        u      >>= 8;
                    }
                    return(new IPAddress(bytes).ToString());
                }
            }
            else
            {
                throw RubyExceptions.CreateRangeError("bignum too big to convert into `quad long'");
            }
        }
Exemple #2
0
        internal static UInt64 ToUInt64(BigInteger value)
        {
            UInt64 result;

            if (value.AsUInt64(out result))
            {
                return(result);
            }
            throw RubyExceptions.CreateRangeError("number too big to convert into System::UInt64");
        }
Exemple #3
0
        public static object EvalExpression(string code, Dictionary <string, object> localScope)
        {
            var scope = Prepare(localScope);

            try
            {
                ScriptSource source = scope.Engine.CreateScriptSourceFromString(code, SourceCodeKind.Expression);
                object       obj    = source.Execute(scope);

                if (obj != null && obj.GetType() == typeof(BigInteger))
                {
                    BigInteger bint = (BigInteger)obj;

                    int   i32;
                    uint  ui32;
                    long  i64;
                    ulong ui64;

                    if (bint.AsInt32(out i32))
                    {
                        return(i32);
                    }

                    if (bint.AsInt64(out i64))
                    {
                        return(i64);
                    }

                    if (bint.AsUInt32(out ui32))
                    {
                        return(ui32);
                    }

                    if (bint.AsUInt64(out ui64))
                    {
                        return(ui64);
                    }
                }

                return(obj);
            }
            catch (Exception ex)
            {
                throw new PeachException("Error executing expression [" + code + "]: " + ex.ToString(), ex);
            }
            finally
            {
                var names = scope.GetVariableNames().ToList();
                foreach (var name in names)
                {
                    scope.RemoveVariable(name);
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// Try to convert to an UInt64.
        /// </summary>
        /// <param name="ret">Set to the converted value</param>
        /// <returns><value>true</value> if successful; <value>false</value> if the value cannot be represented.</returns>
        public bool AsUInt64(out ulong ret)
        {
            ret = 0;
            if (_bipart != null)
            {
                return(_bipart.AsUInt64(out ret));
            }
            if (_lpart < 0)
            {
                return(false);
            }

            ret = (ulong)_lpart;
            return(true);
        }
Exemple #5
0
        public static int __hash__(BigInteger self)
        {
            // TODO: we might need our own hash code implementation. This avoids assertion failure.
            if (self == -2147483648)
            {
                return(-2147483648);
            }

            // check if it's in the Int64 or UInt64 range, and use the built-in hashcode for that instead
            // this ensures that objects added to dictionaries as (U)Int64 can be looked up with Python longs
            Int64 i64;

            if (self.AsInt64(out i64))
            {
                return(Int64Ops.__hash__(i64));
            }
            else
            {
                UInt64 u64;
                if (self.AsUInt64(out u64))
                {
                    return(UInt64Ops.__hash__(u64));
                }
            }

            // Call the DLR's BigInteger hash function, which will return an int32 representation of
            // b if b is within the int32 range. We use that as an optimization for hashing, and
            // assert the assumption below.
            int hash = self.GetHashCode();

#if DEBUG
            int i;
            if (self.AsInt32(out i))
            {
                Debug.Assert(i == hash, String.Format("hash({0}) == {1}", i, hash));
            }
#endif
            return(hash);
        }
Exemple #6
0
 public static bool AsUInt64(BigInteger self, out ulong res)
 {
     return(self.AsUInt64(out res));
 }
 private void AsUInt64Test(BigInteger i, bool expRet, ulong expInt)
 {
     ulong v;
     bool b = i.AsUInt64(out v);
     Expect(b, EqualTo(expRet));
     Expect(v, EqualTo(expInt));
 }
Exemple #8
0
 public static Variable MakeFixRat(BigInteger num, BigInteger den)
 {
     ulong sden;
     SimplifyFrac(ref num, ref den);
     if (den.AsUInt64(out sden) && sden != 0)
         return Kernel.BoxAnyMO<Rat>(new Rat(num, sden), Kernel.RatMO);
     return MakeFloat(RatToFloat(num, den));
 }
Exemple #9
0
        public static object EvalExpression(string code, Dictionary <string, object> localScope)
        {
            var missing = Imports.Except(Engine.Modules.Keys).ToList();

            foreach (string import in missing)
            {
                Engine.Modules.Add(import, Engine.Instance.ImportModule(import));
            }

            ScriptScope scope = Engine.Instance.CreateScope();

            foreach (var kv in Engine.Modules)
            {
                scope.SetVariable(kv.Key, kv.Value);
            }

            foreach (var kv in GlobalScope)
            {
                scope.SetVariable(kv.Key, kv.Value);
            }

            foreach (var kv in localScope)
            {
                scope.SetVariable(kv.Key, kv.Value);
            }

            try
            {
                ScriptSource source = scope.Engine.CreateScriptSourceFromString(code, SourceCodeKind.Expression);
                object       obj    = source.Execute(scope);

                if (obj != null && obj.GetType() == typeof(BigInteger))
                {
                    BigInteger bint = (BigInteger)obj;

                    int   i32;
                    uint  ui32;
                    long  i64;
                    ulong ui64;

                    if (bint.AsInt32(out i32))
                    {
                        return(i32);
                    }

                    if (bint.AsInt64(out i64))
                    {
                        return(i64);
                    }

                    if (bint.AsUInt32(out ui32))
                    {
                        return(ui32);
                    }

                    if (bint.AsUInt64(out ui64))
                    {
                        return(ui64);
                    }
                }

                return(obj);
            }
            catch (Exception ex)
            {
                throw new PeachException("Error executing expression [" + code + "]: " + ex.ToString(), ex);
            }
            finally
            {
                var names = scope.GetVariableNames().ToList();
                foreach (var name in names)
                {
                    scope.RemoveVariable(name);
                }
            }
        }