public void WriteString(LazyCompressedStringValue str) { var strBuffer = str.DecompressToTempBuffer(out AllocatedMemoryData allocated, _context); try { var strSrcBuffer = str.Buffer; var size = str.UncompressedSize; var escapeSequencePos = str.CompressedSize; var numberOfEscapeSequences = BlittableJsonReaderBase.ReadVariableSizeInt(strSrcBuffer, ref escapeSequencePos); // We ensure our buffer will have enough space to deal with the whole string. int bufferSize = 2 * numberOfEscapeSequences + size + 2; if (bufferSize >= JsonOperationContext.ManagedPinnedBuffer.Size) { goto WriteLargeCompressedString; // OK, do it the slow way instead. } EnsureBuffer(bufferSize); _buffer[_pos++] = Quote; while (numberOfEscapeSequences > 0) { numberOfEscapeSequences--; var bytesToSkip = BlittableJsonReaderBase.ReadVariableSizeInt(strSrcBuffer, ref escapeSequencePos); WriteRawString(strBuffer, bytesToSkip); strBuffer += bytesToSkip; size -= bytesToSkip + 1 /*for the escaped char we skip*/; var b = *(strBuffer++); var auxPos = _pos; _buffer[auxPos++] = (byte)'\\'; _buffer[auxPos++] = GetEscapeCharacter(b); _pos = auxPos; } // write remaining (or full string) to the buffer in one shot WriteRawString(strBuffer, size); _buffer[_pos++] = Quote; return; WriteLargeCompressedString: UnlikelyWriteCompressedString(numberOfEscapeSequences, strSrcBuffer, escapeSequencePos, strBuffer, size); } finally { if (allocated != null) //precaution { _context.ReturnMemory(allocated); } } }
public unsafe int WriteValue(LazyCompressedStringValue str, out BlittableJsonToken token, UsageMode mode) { var startPos = _position; token = BlittableJsonToken.CompressedString; _position += WriteVariableSizeInt(str.UncompressedSize); _position += WriteVariableSizeInt(str.CompressedSize); var escapeSequencePos = GetSizeIncludingEscapeSequences(str.Buffer, str.CompressedSize); _unmanagedWriteBuffer.Write(str.Buffer, escapeSequencePos); _position += escapeSequencePos; return(startPos); }
public void WriteValue(LazyCompressedStringValue value) { var currentState = _continuationState.Pop(); BlittableJsonToken stringToken; //public unsafe int WriteValue(byte* buffer, int size, out BlittableJsonToken token, UsageMode mode, int? initialCompressedSize) //var valuePos = _writer.WriteValue(value, out stringToken, UsageMode.None, null); var valuePos = _writer.WriteValue(value, out stringToken, UsageMode.None); _writeToken = new WriteToken { ValuePos = valuePos, WrittenToken = stringToken }; if (currentState.FirstWrite == -1) { currentState.FirstWrite = valuePos; } currentState = FinishWritingScalarValue(currentState); _continuationState.Push(currentState); }