Пример #1
0
        public static void WriteString(string value, MsgPackStream stream)
        {
            if (value == null)
            {
                stream.WriteUInt8(FormatCode.Nil);
                return;
            }

            var length = Encoding.UTF8.GetByteCount(value);

            if (length <= FormatRange.MaxFixStringLength)
            {
                stream.WriteUInt8(unchecked ((byte)(FormatCode.MinFixStr | length)));
            }
            else if (length <= byte.MaxValue)
            {
                stream.WriteUInt8(FormatCode.Str8);
                stream.WriteUInt8(unchecked ((byte)length));
            }
            else if (length <= ushort.MaxValue)
            {
                stream.WriteUInt8(FormatCode.Str16);
                stream.WriteUInt16(unchecked ((ushort)length));
            }
            else
            {
                stream.WriteUInt8(FormatCode.Str32);
                stream.WriteInt32(length);
            }

            stream.WriteString(value);
        }