private void ReadNumber(FlexBuffer flx)
        {
            var builder = new StringBuilder();

            var isFloat = false;

            if (_scanner.Peek() == '-')
            {
                builder.Append(_scanner.Read());
            }

            if (_scanner.Peek() == '0')
            {
                builder.Append(_scanner.Read());
            }
            else
            {
                ReadDigits(builder);
            }

            if (_scanner.CanRead && _scanner.Peek() == '.')
            {
                builder.Append(_scanner.Read());
                ReadDigits(builder);
                isFloat = true;
            }

            if (_scanner.CanRead && char.ToLowerInvariant(_scanner.Peek()) == 'e')
            {
                builder.Append(_scanner.Read());

                var next = _scanner.Peek();

                switch (next)
                {
                case '+':
                case '-':
                    builder.Append(_scanner.Read());
                    break;
                }

                ReadDigits(builder);
            }

            if (isFloat)
            {
                var value = double.Parse(
                    builder.ToString(),
                    CultureInfo.InvariantCulture
                    );
                flx.Add(value);
            }
            else
            {
                flx.Add(long.Parse(builder.ToString()));
            }
        }
Esempio n. 2
0
        public static byte[] SingleValue(long x, long y)
        {
            var buffer = new FlexBuffer(20);
            var start  = buffer.StartVector();

            buffer.Add(x);
            buffer.Add(y);
            buffer.EndVector(start, true, true);
            return(buffer.Finish());
        }
Esempio n. 3
0
        public static byte[] SingleValue(double x, double y, double z)
        {
            var buffer = new FlexBuffer(28);
            var start  = buffer.StartVector();

            buffer.Add(x);
            buffer.Add(y);
            buffer.Add(z);
            buffer.EndVector(start, true, true);
            return(buffer.Finish());
        }
Esempio n. 4
0
        public static byte[] SingleValue(ulong x, ulong y, ulong z, ulong w)
        {
            var buffer = new FlexBuffer(36);
            var start  = buffer.StartVector();

            buffer.Add(x);
            buffer.Add(y);
            buffer.Add(z);
            buffer.Add(w);
            buffer.EndVector(start, true, true);
            return(buffer.Finish());
        }
Esempio n. 5
0
        public static byte[] SingleValue(string value)
        {
            var buffer = new FlexBuffer((ulong)value.Length + 2);

            buffer.Add(value);
            return(buffer.Finish());
        }
Esempio n. 6
0
        public static byte[] SingleValue(bool value)
        {
            var buffer = new FlexBuffer(3);

            buffer.Add(value);
            return(buffer.Finish());
        }
Esempio n. 7
0
        public static byte[] SingleValue(byte[] blob)
        {
            var buffer = new FlexBuffer((ulong)blob.Length + 10);

            buffer.Add(blob);
            return(buffer.Finish());
        }
        private void ReadBoolean(FlexBuffer flx)
        {
            switch (_scanner.Peek())
            {
            case 't':
                _scanner.Assert("true");
                flx.Add(true);
                return;

            case 'f':
                _scanner.Assert("false");
                flx.Add(false);
                return;

            default:
                throw new Exception($"Unexpected character {_scanner.Position}");
            }
        }
        private static void Process(FlexBuffer flx, XmlNode element)
        {
            var node = flx.StartVector();

            flx.AddKey("tagName");
            flx.Add(element.Name);
            var attributes = element.Attributes;

            if (attributes != null)
            {
                for (var i = 0; i < attributes.Count; i++)
                {
                    var att = attributes.Item(i);
                    flx.AddKey(att.Name);
                    flx.Add(att.Value);
                }
            }

            var children = element.ChildNodes;

            if (children.Count > 0)
            {
                flx.AddKey("children");
                var childVector = flx.StartVector();
                for (var i = 0; i < children.Count; i++)
                {
                    var child = children[i];
                    if (child.NodeType == XmlNodeType.Text || child.NodeType == XmlNodeType.CDATA)
                    {
                        flx.Add(child.Value);
                    }
                    else if (child.NodeType == XmlNodeType.Comment)
                    {
                    }
                    else
                    {
                        Process(flx, child);
                    }
                }

                flx.EndVector(childVector, false, false);
            }
            flx.SortAndEndMap(node);
        }
        private void ReadString(FlexBuffer flx, bool asKey = false)
        {
            var builder = new StringBuilder();

            _scanner.Assert('"');

            while (true)
            {
                var c = _scanner.Read();

                if (c == '\\')
                {
                    c = _scanner.Read();

                    switch (char.ToLower(c))
                    {
                    case '"':                              // "
                    case '\\':                             // \
                    case '/':                              // /
                        builder.Append(c);
                        break;

                    case 'b':
                        builder.Append('\b');
                        break;

                    case 'f':
                        builder.Append('\f');
                        break;

                    case 'n':
                        builder.Append('\n');
                        break;

                    case 'r':
                        builder.Append('\r');
                        break;

                    case 't':
                        builder.Append('\t');
                        break;

                    case 'u':
                        builder.Append(ReadUnicodeLiteral());
                        break;

                    default:
                        throw new Exception($"Unexpected character {_scanner.Position}");
                    }
                }
                else if (c == '"')
                {
                    break;
                }
                else
                {
                    if (char.IsControl(c))
                    {
                        throw new Exception($"Unexpected character {_scanner.Position}");
                    }
                    else
                    {
                        builder.Append(c);
                    }
                }
            }

            if (asKey)
            {
                flx.AddKey(builder.ToString());
            }
            else
            {
                flx.Add(builder.ToString());
            }
        }
        public static byte[] Convert(string csv, char separator = ',')
        {
            var flx              = new FlexBuffer();
            var sb               = new StringBuilder();
            var outerVec         = flx.StartVector();
            var innerVec         = -1;
            var isInDoubleQuotes = false;

            for (var offset = 0; offset < csv.Length;)
            {
                if (innerVec == -1)
                {
                    innerVec = flx.StartVector();
                }

                if (csv[offset] == '"')
                {
                    if (isInDoubleQuotes == false)
                    {
                        isInDoubleQuotes = true;
                        offset++;
                    }
                    else
                    {
                        if (csv.Length > offset + 1 && csv[offset + 1] == '"')
                        {
                            sb.Append('"');
                            offset += 2;
                        }
                        else
                        {
                            isInDoubleQuotes = false;
                            offset++;
                        }
                    }
                }
                else if (csv[offset] == separator && isInDoubleQuotes == false)
                {
                    flx.Add(sb.ToString());
                    sb.Clear();
                    offset++;
                }
                else if (csv[offset] == '\n' && isInDoubleQuotes == false)
                {
                    flx.Add(sb.ToString());
                    flx.EndVector(innerVec, false, false);
                    innerVec = -1;
                    sb.Clear();
                    offset++;
                }
                else if (csv[offset] == '\r' && csv.Length > offset + 1 && csv[offset + 1] == '\n' && isInDoubleQuotes == false)
                {
                    flx.Add(sb.ToString());
                    flx.EndVector(innerVec, false, false);
                    innerVec = -1;
                    sb.Clear();
                    offset += 2;
                }
                else
                {
                    sb.Append(csv[offset]);
                    offset++;
                }
            }

            if (innerVec != -1)
            {
                flx.Add(sb.ToString());
                flx.EndVector(innerVec, false, false);
            }
            flx.EndVector(outerVec, false, false);
            return(flx.Finish());
        }
Esempio n. 12
0
 public void Add(string key, long value, bool indirect = false)
 {
     _buffer.AddKey(key);
     if (indirect)
     {
         _buffer.AddIndirect(value);
     }
     else
     {
         _buffer.Add(value);
     }
 }
Esempio n. 13
0
 public void Add(long value, bool indirect = false)
 {
     if (indirect)
     {
         _buffer.AddIndirect(value);
     }
     else
     {
         _buffer.Add(value);
     }
 }