예제 #1
0
        private int CompareByteArray(ByteArrayDatum x, ByteArrayDatum y)
        {
            int pos = 0;

            while (true)
            {
                if (x.Length == pos && y.Length == pos)
                {
                    return(0);
                }
                if (x.Length == pos)
                {
                    return(-1);
                }
                if (y.Length == pos)
                {
                    return(1);
                }
                int itemResult = Comparer <byte> .Default.Compare(x[pos], y[pos]);

                if (itemResult != 0)
                {
                    return(itemResult);
                }
                ++pos;
            }
        }
예제 #2
0
 public bool VisitByteArray(ByteArrayDatum d)
 {
     bw.Write(B_BYTE_ARRAY);
     bw.Write(d.Bytes.Length);
     byte[] temp = new byte[d.Bytes.Length];
     d.Bytes.CopyTo(temp);
     bw.Write(temp);
     return(true);
 }
 private bool EqualByteArray(ByteArrayDatum x, ByteArrayDatum y)
 {
     if (x.Length != y.Length)
     {
         return(false);
     }
     foreach (int i in Enumerable.Range(0, x.Length))
     {
         if (x[i] != y[i])
         {
             return(false);
         }
     }
     return(true);
 }
예제 #4
0
        public static Datum ReadDatum(this BinaryReader r)
        {
            int       uninternedSymbolCount = r.ReadInt32();
            ListDatum uninternedSymbols     = ListDatum.Empty;

            for (int i = 0; i < uninternedSymbolCount; ++i)
            {
                uninternedSymbols = uninternedSymbols.Add(new SymbolDatum());
            }
            int       internedSymbolCount = r.ReadInt32();
            ListDatum internedSymbols     = ListDatum.Empty;

            for (int i = 0; i < internedSymbolCount; ++i)
            {
                string name = r.ReadString();
                internedSymbols = internedSymbols.Add(new SymbolDatum(name));
            }
            int       mutableBoxCount = r.ReadInt32();
            ListDatum mutableBoxes    = ListDatum.Empty;

            for (int i = 0; i < mutableBoxCount; ++i)
            {
                mutableBoxes = mutableBoxes.Add(new MutableBoxDatum(NullDatum.Value));
            }

            Func <Datum> read = null;

            read = delegate()
            {
                byte typeId = r.ReadByte();
                switch (typeId)
                {
                case B_NULL:
                    return(NullDatum.Value);

                case B_BOOLEAN_FALSE:
                    return(BooleanDatum.False);

                case B_BOOLEAN_TRUE:
                    return(BooleanDatum.True);

                case B_CHAR:
                {
                    char ch = r.ReadChar();
                    return(new CharDatum(ch));
                }

                case B_STRING:
                {
                    string str = r.ReadString();
                    return(new StringDatum(str));
                }

                case B_INT:
                {
                    BigInteger b = r.ReadBigInteger();
                    return(new IntDatum(b));
                }

                case B_FLOAT:
                {
                    double f = r.ReadDouble();
                    return(new FloatDatum(f));
                }

                case B_BYTE_ARRAY:
                {
                    int    nBytes    = r.ReadInt32();
                    byte[] buf       = new byte[nBytes];
                    int    bytesRead = r.Read(buf, 0, nBytes);
                    if (bytesRead != nBytes)
                    {
                        throw new EndOfStreamException();
                    }
                    return(ByteArrayDatum.FromByteArray(buf));
                }

                case B_SYMBOL:
                {
                    int index = r.ReadInt32();
                    if (index < 0)
                    {
                        return(uninternedSymbols[~index]);
                    }
                    else
                    {
                        return(internedSymbols[index]);
                    }
                }

                case B_LIST:
                {
                    int       count = r.ReadInt32();
                    ListDatum l     = ListDatum.Empty;
                    foreach (int i in Enumerable.Range(0, count))
                    {
                        l = l.Add(read());
                    }
                    return(l);
                }

                case B_SET:
                {
                    int      count = r.ReadInt32();
                    SetDatum s     = SetDatum.Empty;
                    foreach (int i in Enumerable.Range(0, count))
                    {
                        s = s.Add(read());
                    }
                    return(s);
                }

                case B_DICTIONARY:
                {
                    int             count = r.ReadInt32();
                    DictionaryDatum d     = DictionaryDatum.Empty;
                    foreach (int i in Enumerable.Range(0, count))
                    {
                        Datum k = read();
                        Datum v = read();
                        d = d.Add(k, v);
                    }
                    return(d);
                }

                case B_MUTABLE_BOX:
                {
                    int index = r.ReadInt32();
                    return(mutableBoxes[index]);
                }

                case B_RATIONAL:
                {
                    BigInteger numerator   = r.ReadBigInteger();
                    BigInteger denominator = r.ReadBigInteger();
                    return(new RationalDatum(new BigRational(numerator, denominator)));
                }

                case B_GUID:
                {
                    byte[] buf       = new byte[16];
                    int    bytesRead = r.Read(buf, 0, 16);
                    if (bytesRead < 16)
                    {
                        throw new EndOfStreamException();
                    }
                    return(new GuidDatum(new Guid(buf)));
                }

                default:
                    throw new FormatException("Unknown type id");
                }
            };

            for (int j = 0; j < mutableBoxCount; ++j)
            {
                ((MutableBoxDatum)mutableBoxes[j]).Content = read();
            }

            return(read());
        }
 public State VisitByteArray(State state, ByteArrayDatum d) => state;
 public string VisitByteArray(ByteArrayDatum d)
 {
     return("#y(" + string.Join("", d.Bytes.Select(b => b.ToString("X2"))) + ")");
 }
 public SetDatum VisitByteArray(ByteArrayDatum d) => SetDatum.Empty;
예제 #8
0
 public string VisitByteArray(ByteArrayDatum d)
 {
     return("a" + Convert.ToBase64String(d.Bytes.ToArray()));
 }