public RCSymbolScalar(RCSymbolScalar previous, object key) { if (key == null) { throw new ArgumentNullException("key"); } if (key.GetType() == typeof(RCSymbolScalar)) { throw new Exception("Symbols may not contain other symbols."); } if (previous == null) { Previous = RCSymbolScalar.Empty; } else { Previous = previous; } if (key.GetType() == typeof(string)) { string str = (string)key; if (str.Length > 0) { if (str[0] == '\'') { if (str[str.Length - 1] == '\'') { key = str.Substring(1, str.Length - 2); } else { throw new Exception("Invalid symbol part: " + str); } } } } if (key.GetType() == typeof(int)) { key = (long)(int)key; } Key = key; Type = RCVectorBase.EmptyOf(Key.GetType()); if (Previous == RCSymbolScalar.Empty) { Length = 1; string part = Type.IdShorthand(Key); string prefix = part.Length > 0 && part[0] == '#' ? "" : "#"; _string = prefix + part + Type.Suffix; } else { Length = previous.Length + 1; _string = previous.ToString() + "," + Type.IdShorthand(Key) + Type.Suffix; } if (Previous.Key.Equals("*")) { _leadingStar = true; } }
public static void WriteScalarSymbol(RCArray <byte> result, RCSymbolScalar scalar) { // This is not going to be a triumph of efficiency. // I am considering rewriting RCSymbolScalar so that it stores all of its // data as an array of bytes. Then the ToByte operation would just // return the internal representation. for (int i = 0; i < scalar.Length; ++i) { object part = scalar.Part(i); char type = RCVectorBase.EmptyOf(part.GetType()).TypeCode; result.Write((byte)type); switch (type) { case 'l': result.Write(BitConverter.GetBytes((long)part)); break; case 'd': result.Write(BitConverter.GetBytes((double)part)); break; case 'm': Binary.WriteScalarDecimal((decimal)part, result); break; case 'b': result.Write(BitConverter.GetBytes((bool)part)); break; case 's': Binary.WriteScalarString((string)part, result); break; default: throw new Exception("Unknown type:" + type + " found in symbol"); } } }