Ejemplo n.º 1
0
        public static bool Equals(Value a, Value b)
        {
            if (a.RefVal == b.RefVal)
            {
                return(a.RefVal != NumTypeTag || a.NumVal == b.NumVal);
            }

            var asStrA = a.RefVal as byte[];

            if (asStrA != null)
            {
                return(LString.InternalEquals(asStrA, b.RefVal as byte[]));
            }

            return(false);
        }
Ejemplo n.º 2
0
        public int IndexOf(LString str, int index, int count)
        {
            if (str.IsNil)
            {
                throw new ArgumentNullException("str");
            }

            if (index < 0 || count < 0 || index > Length - count)
            {
                throw new ArgumentOutOfRangeException("index");
            }

            var data    = InternalData;
            var strData = str.InternalData;

            if (strData.Length == BufferDataOffset)
            {
                //the empty string is always found at the beginning of the search
                return(index);
            }

            count -= strData.Length - BufferDataOffset;

            for (int i = index + BufferDataOffset; count-- >= 0; i++)
            {
                for (int ii = i, j = BufferDataOffset; j < strData.Length; ii++, j++)
                {
                    if (data[ii] != strData[j])
                    {
                        goto notFound;
                    }
                }

                return(i - BufferDataOffset);

notFound:
                ;
            }

            return(-1);
        }
Ejemplo n.º 3
0
        public override string ToString()
        {
            if (RefVal == null)
            {
                return("(nil)");
            }

            if (RefVal == TrueTag)
            {
                return("true");
            }

            if (RefVal == FalseTag)
            {
                return("false");
            }

            if (RefVal == NumTypeTag)
            {
                return(NumVal.ToString());
            }

            var asStr = RefVal as byte[];

            if (asStr != null)
            {
                return(LString.InternalFromData(asStr).ToString());
            }

            var val = RefVal;

            var asWrapper = val as UserDataWrapper;

            if (asWrapper != null)
            {
                val = asWrapper.Value;
            }

            return(val.ToString());
        }
Ejemplo n.º 4
0
        public bool Equals(Value value)
        {
            if (Val == value.RefVal)
            {
                return(true);
            }

            if (value.RefVal == Value.NumTypeTag)
            {
                var asNum = Val as NumBox;
                return(asNum != null && asNum.Value == value.NumVal);
            }

            var asStr = Val as byte[];

            if (asStr != null)
            {
                return(LString.InternalEquals(asStr, value.RefVal as byte[]));
            }

            return(false);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Finds the key's location in the table (returns 0 if not found).
        /// Note that the value at that key may be nil.
        /// </summary>
        internal int FindValue(LString key)
        {
            if (key.InternalData == null)
            {
                throw new ArgumentNullException("key");
            }

            int i = GetMainPosition(key);

            while (i != -1)
            {
                var node = nodes[i];
                if (node.Key.Equals(key))
                {
                    return(-(i + 1));
                }

                i = node.Next;
            }

            return(0);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Gets or sets a value in the table.
        /// This is a raw operation, it does
        /// not invoke metatable methods.
        /// </summary>
        public Value this[LString key]
        {
            get
            {
                Value ret;

                int loc = FindValue(key);
                ReadValue(loc, out ret);

                return(ret);
            }

            set
            {
                int loc = FindValue(key);
                if (loc == 0)
                {
                    loc = InsertNewKey(new CompactValue(key));
                }
                WriteValue(loc, ref value);
            }
        }
Ejemplo n.º 7
0
        public override int GetHashCode()
        {
            if (Val == null || Val == Value.FalseTag)
            {
                return(0);
            }

            if (Val == Value.TrueTag)
            {
                return(1);
            }

            var asNum = Val as NumBox;

            if (asNum != null)
            {
                return(Value.GetHashCode(asNum.Value));
            }

            var asStr = Val as byte[];

            if (asStr != null)
            {
                return(LString.InternalGetHashCode(asStr));
            }

            var val = Val;

            var asWrapper = val as Value.UserDataWrapper;

            if (asWrapper != null)
            {
                val = asWrapper.Value;
            }

            return(Value.GetHashCode(val));
        }
Ejemplo n.º 8
0
        public static int CheckOpt( Value value, LString defaultArg, LString[] opts )
        {
            if( opts == null )
                throw new ArgumentNullException( "opts" );

            if( value.IsNil )
            {
                if( defaultArg.IsNil )
                    throw new ArgumentNullException( "A required argument was nil." );

                value = defaultArg;
            }

            if( value.ValueType != LValueType.String )
                throw new ArgumentException( "A string argument was expected." );

            for( int i = 0; i < opts.Length; i++ )
            {
                if( value == opts[i] )
                    return i;
            }

            throw new ArgumentException( "Invalid option." );
        }
Ejemplo n.º 9
0
        public static bool Equals(CompactValue a, CompactValue b)
        {
            if (a.Val == b.Val)
            {
                return(true);
            }

            var asNumA = a.Val as NumBox;

            if (asNumA != null)
            {
                var asNumB = b.Val as NumBox;
                return(asNumB != null && asNumA.Value == asNumB.Value);
            }

            var asStrA = a.Val as byte[];

            if (asStrA != null)
            {
                return(LString.InternalEquals(asStrA, b.Val as byte[]));
            }

            return(false);
        }
Ejemplo n.º 10
0
 public LoadState(Stream byteCode, LString name)
 {
     this.byteCode = byteCode;
     this.Name     = name;
 }
Ejemplo n.º 11
0
 internal static LString InternalFinishBuffer( byte[] buffer )
 {
     Debug.Assert( buffer != null );
     var ret = new LString { InternalData = buffer };
     ret.UpdateHashCode();
     return ret;
 }
Ejemplo n.º 12
0
 public int IndexOf( LString str )
 {
     return IndexOf( str, 0, Length );
 }
Ejemplo n.º 13
0
        public bool Equals(LString str)
        {
            var asStr = RefVal as byte[];

            return(LString.InternalEquals(asStr, str.InternalData));
        }
Ejemplo n.º 14
0
 public void Append( LString str )
 {
     if( str.Length != 0 )
         Append( str.InternalData, LString.BufferDataOffset, str.Length );
 }
Ejemplo n.º 15
0
 private Value GetMetamethod( ref Value val, LString tmName )
 {
     var mt = GetMetatable( ref val );
     return mt != null ? mt[tmName] : Value.Nil;
 }
Ejemplo n.º 16
0
 public static int CheckOpt( Value value, LString[] opts )
 {
     return CheckOpt( value, (int?)null, opts );
 }
Ejemplo n.º 17
0
 public CompactValue(LString value)
 {
     Val = value.InternalData;
 }
Ejemplo n.º 18
0
 public Value(LString value)
 {
     RefVal = value.InternalData;
     NumVal = 0;
 }
Ejemplo n.º 19
0
 public LoadState( Stream byteCode, LString name )
 {
     this.byteCode = byteCode;
     this.Name = name;
 }
Ejemplo n.º 20
0
 public bool ContainsKey(LString key)
 {
     return(!IsLocNilOrEmpty(FindValue(key)));
 }
Ejemplo n.º 21
0
 public int IndexOf(LString str)
 {
     return(IndexOf(str, 0, Length));
 }
Ejemplo n.º 22
0
        private int GetMainPosition(LString key)
        {
            int hash = key.GetHashCode();

            return((hash & 0x7FFFFFFF) % nodes.Length);
        }
Ejemplo n.º 23
0
 public bool Equals(LString value)
 {
     Debug.Assert(value.InternalData != null);
     return(LString.InternalEquals(value.InternalData, Val as byte[]));
 }
Ejemplo n.º 24
0
        private bool ValToStr( ref Value val )
        {
            if( val.RefVal is byte[] )
                return true;

            if( val.RefVal != Value.NumTypeTag )
                return false;

            int len = Helpers.NumToStr( fmtBuf, 0, val.NumVal, 14 );
            val = new LString( fmtBuf, 0, len );

            return true;
        }
Ejemplo n.º 25
0
        public int IndexOf( LString str, int index )
        {
            if( index < 0 )
                throw new ArgumentOutOfRangeException( "index" );

            return IndexOf( str, index, Length - index );
        }
Ejemplo n.º 26
0
 /// <summary>
 /// Compares this string to another for equality.
 /// </summary>
 public bool Equals( LString other )
 {
     return InternalEquals( InternalData, other.InternalData );
 }
Ejemplo n.º 27
0
 public void Set(LString value)
 {
     RefVal = value.InternalData;
 }
Ejemplo n.º 28
0
 /// <summary>
 /// Compares this string to another for equality.
 /// </summary>
 public bool Equals(LString other)
 {
     return(InternalEquals(InternalData, other.InternalData));
 }
Ejemplo n.º 29
0
        public int IndexOf( LString str, int index, int count )
        {
            if( str.IsNil )
                throw new ArgumentNullException( "str" );

            if( index < 0 || count < 0 || index > Length - count )
                throw new ArgumentOutOfRangeException( "index" );

            var data = InternalData;
            var strData = str.InternalData;

            if( strData.Length == BufferDataOffset )
                //the empty string is always found at the beginning of the search
                return index;

            count -= strData.Length - BufferDataOffset;

            for( int i = index + BufferDataOffset; count-- >= 0; i++ )
            {
                for( int ii = i, j = BufferDataOffset; j < strData.Length; ii++, j++ )
                {
                    if( data[ii] != strData[j] )
                        goto notFound;
                }

                return i - BufferDataOffset;

            notFound:
                ;
            }

            return -1;
        }