private static void SaveFloat(Pickler /*!*/ pickler, object obj) { Debug.Assert(obj.GetType() == typeof(double), "arg must be float"); pickler.Write(Opcode.BinFloat); pickler.WriteFloat64(obj); }
private static void SaveList(Pickler /*!*/ pickler, object obj) { Debug.Assert(obj.GetType() == typeof(List <object>), "arg must be list"); Debug.Assert(!pickler.MemoContains(obj)); int index = pickler.MemoizeNew(obj); pickler.Write(Opcode.EmptyList); pickler.WritePut(index); pickler.BatchAppends(((IEnumerable)obj).GetEnumerator()); }
private static void SaveBoolean(Pickler /*!*/ pickler, object obj) { Debug.Assert(obj.GetType() == typeof(bool), "arg must be bool"); if ((bool)obj) { pickler.Write(Opcode.NewTrue); } else { pickler.Write(Opcode.NewFalse); } }
private static void SaveLong(Pickler /*!*/ pickler, object obj) { Debug.Assert(obj.GetType() == typeof(BigInteger), "arg must be long"); BigInteger bi = (BigInteger)obj; if (bi.IsZero) { pickler.Write(Opcode.Long1); pickler.WriteUInt8(0); } else if (bi <= MaxInt && bi >= MinInt) { pickler.Write(Opcode.Long1); int value = (int)bi; if (IsInt8(value)) { pickler.WriteUInt8(1); pickler._file.Write((char)(byte)value); } else if (IsInt16(value)) { pickler.WriteUInt8(2); pickler.WriteUInt8(value & 0xff); pickler.WriteUInt8((value >> 8) & 0xff); } else { pickler.WriteUInt8(4); pickler.WriteInt32(value); } } else { byte[] dataBytes = bi.ToByteArray(); if (dataBytes.Length < 256) { pickler.Write(Opcode.Long1); pickler.WriteUInt8(dataBytes.Length); } else { pickler.Write(Opcode.Long4); pickler.WriteInt32(dataBytes.Length); } foreach (byte b in dataBytes) { pickler.WriteUInt8(b); } } }
private static void SaveUnicode(Pickler /*!*/ pickler, object obj) { Debug.Assert(obj.GetType() == typeof(string), "arg must be unicode"); Debug.Assert(!pickler.MemoContains(obj)); var memo = pickler._privMemo[obj] = pickler._privMemo.Count; pickler.Write(Opcode.BinUnicode); pickler.WriteUnicodeStringUtf8(obj); pickler.WriteGetOrPut(false, memo); }
private static void SaveDict(Pickler /*!*/ pickler, object obj) { Debug.Assert(obj.GetType() == typeof(Dictionary <string, object>), "arg must be dict"); Debug.Assert(!pickler.MemoContains(obj)); int index = pickler.MemoizeNew(obj); pickler.Write(Opcode.EmptyDict); pickler.WritePut(index); pickler.BatchSetItems((Dictionary <string, object>)obj); }
private static void SaveInteger(Pickler /*!*/ pickler, object obj) { Debug.Assert(obj.GetType() == typeof(int), "arg must be int"); if (IsUInt8(obj)) { pickler.Write(Opcode.BinInt1); pickler.WriteUInt8(obj); } else if (IsUInt16(obj)) { pickler.Write(Opcode.BinInt2); pickler.WriteUInt16(obj); } else { pickler.Write(Opcode.BinInt); pickler.WriteInt32(obj); } }
private static void SaveTuple(Pickler /*!*/ pickler, object obj) { Debug.Assert(obj.GetType() == typeof(object[]), "arg must be array"); Debug.Assert(!pickler.MemoContains(obj)); object[] t = (object[])obj; byte opcode; bool needMark = false; int len = t.Length; if (len == 0) { opcode = Opcode.EmptyTuple; } else if (len == 1) { opcode = Opcode.Tuple1; } else if (len == 2) { opcode = Opcode.Tuple2; } else if (len == 3) { opcode = Opcode.Tuple3; } else { opcode = Opcode.Tuple; needMark = true; } if (needMark) { pickler.Write(Opcode.Mark); } var data = t; for (int i = 0; i < data.Length; i++) { pickler.Save(data[i]); } if (len > 0) { if (pickler.MemoContains(obj)) { // recursive tuple for (int i = 0; i < len; i++) { pickler.Write(Opcode.Pop); } pickler.WriteGet(obj); return; } pickler.Write(opcode); pickler.Memoize(t); pickler.WritePut(t); } else { pickler.Write(opcode); } }
private static void SaveNone(Pickler /*!*/ pickler, object obj) { Debug.Assert(obj == null, "arg must be None"); pickler.Write(Opcode.NoneValue); }