예제 #1
0
        private void WriteStringValue(BionToken markerType, String8 value)
        {
            if (_compressor == null)
            {
                // Write marker and length
                WriteStringLength(markerType, value.Length);

                // Write value
                _writer.EnsureSpace(value.Length);
                value.CopyTo(_writer.Buffer, _writer.Index);
                _writer.Index += value.Length;
            }
            else
            {
                // Write marker for compressed, terminated value
                Write((byte)(markerType == BionToken.String ? BionMarker.StringCompressedTerminated : BionMarker.PropertyNameCompressedTerminated));

                // Compress and write value
                using (BufferedReader reader = BufferedReader.FromString(value))
                {
                    _compressor.Compress(reader, _writer);
                }

                // Write end token
                Write(BionMarker.EndValue);
            }
        }
예제 #2
0
        private void WriteStringValue(BionToken markerType, string value)
        {
            if (value == null)
            {
                WriteNull();
                return;
            }

            if (_compressor == null)
            {
                int length = Encoding.UTF8.GetByteCount(value);

                // Write marker and length
                WriteStringLength(markerType, length);

                // Encode and writer value
                _writer.EnsureSpace(length);
                Encoding.UTF8.GetBytes(value, 0, value.Length, _writer.Buffer, _writer.Index);
                _writer.Index += length;
            }
            else
            {
                // Write marker for compressed, terminated value
                Write((byte)(markerType == BionToken.String ? BionMarker.StringCompressedTerminated : BionMarker.PropertyNameCompressedTerminated));

                // Compress and write value
                using (BufferedReader reader = BufferedReader.FromString(value, ref _stringConvertBuffer))
                {
                    _compressor.Compress(reader, _writer);
                }

                // Write end token
                Write(BionMarker.EndValue);
            }
        }
예제 #3
0
        private static string SplitAndJoin(string content)
        {
            StringBuilder result = new StringBuilder();

            byte[] buffer = new byte[content.Length];
            using (BufferedReader reader = BufferedReader.FromString(content, ref buffer))
            {
                bool isWord = WordSplitter.IsLetterOrDigit(reader.Buffer[reader.Index]);
                int  length = 0;
                while (!reader.EndOfStream)
                {
                    // Read the next word
                    length = WordSplitter.NextWordLength(reader, isWord);
                    String8 word = String8.Reference(reader.Buffer, reader.Index, length);

                    // Set state to read next word
                    reader.Index += length;
                    isWord        = !isWord;

                    if (reader.Index < reader.Length || reader.EndOfStream)
                    {
                        // If this is word is definitely complete, write it
                        if (result.Length > 0)
                        {
                            result.Append("|");
                        }
                        result.Append(word.ToString());
                    }
                    else if (!reader.EndOfStream)
                    {
                        // Reset state to re-read this word
                        reader.Index -= length;
                        isWord        = !isWord;

                        // If end of buffer but not stream, request more
                        reader.EnsureSpace(length * 2);
                    }
                }
            }

            return(result.ToString());
        }