private void UnlikelyWriteLargeString(byte *strBuffer, int size, int numberOfEscapeSequences, int escapeSequencePos) { var ptr = strBuffer; EnsureBuffer(1); _buffer[_pos++] = Quote; while (numberOfEscapeSequences > 0) { numberOfEscapeSequences--; var bytesToSkip = BlittableJsonReaderBase.ReadVariableSizeInt(ptr, ref escapeSequencePos); UnlikelyWriteLargeRawString(strBuffer, bytesToSkip); strBuffer += bytesToSkip; size -= bytesToSkip + 1 /*for the escaped char we skip*/; var b = *(strBuffer++); EnsureBuffer(2); _buffer[_pos++] = (byte)'\\'; _buffer[_pos++] = GetEscapeCharacter(b); } // write remaining (or full string) to the buffer in one shot UnlikelyWriteLargeRawString(strBuffer, size); EnsureBuffer(1); _buffer[_pos++] = Quote; }
private void UnlikelyWriteEscapeSequences(byte *strBuffer, int size, int numberOfEscapeSequences, int escapeSequencePos) { // We ensure our buffer will have enough space to deal with the whole string. int bufferSize = 2 * numberOfEscapeSequences + size + 1; EnsureBuffer(bufferSize); var ptr = strBuffer; var buffer = _buffer; while (numberOfEscapeSequences > 0) { numberOfEscapeSequences--; var bytesToSkip = BlittableJsonReaderBase.ReadVariableSizeInt(ptr, ref escapeSequencePos); WriteRawString(strBuffer, bytesToSkip); strBuffer += bytesToSkip; size -= bytesToSkip + 1 /*for the escaped char we skip*/; var b = *(strBuffer++); int 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); }
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); } } }
private static unsafe int GetSizeIncludingEscapeSequences(byte *buffer, int size) { var escapeSequencePos = size; // now need to also include the size of the escape positions var numberOfEscapeSequences = BlittableJsonReaderBase.ReadVariableSizeInt(buffer, ref escapeSequencePos); for (int i = 0; i < numberOfEscapeSequences; i++) { BlittableJsonReaderBase.ReadVariableSizeInt(buffer, ref escapeSequencePos); } return(escapeSequencePos); }
private int GetSizeOfEscapePositions() { var escapeSequencePos = CompressedSize; var numberOfEscapeSequences = BlittableJsonReaderBase.ReadVariableSizeInt(Buffer, ref escapeSequencePos); while (numberOfEscapeSequences > 0) { numberOfEscapeSequences--; BlittableJsonReaderBase.ReadVariableSizeInt(Buffer, ref escapeSequencePos); } var sizeOfEscapePositions = escapeSequencePos - CompressedSize; return(sizeOfEscapePositions); }
public void WriteString(LazyStringValue str, bool skipEscaping = false) { if (str == null) { WriteNull(); return; } var size = str.Size; if (size == 1 && str.IsControlCodeCharacter(out var b)) { WriteString($@"\u{b:X4}", skipEscaping: true); return; } var strBuffer = str.Buffer; var escapeSequencePos = size; var numberOfEscapeSequences = skipEscaping ? 0 : BlittableJsonReaderBase.ReadVariableSizeInt(str.Buffer, ref escapeSequencePos); // We ensure our buffer will have enough space to deal with the whole string. int bufferSize = 2 * numberOfEscapeSequences + size + 1; if (bufferSize >= JsonOperationContext.ManagedPinnedBuffer.Size) { UnlikelyWriteLargeString(strBuffer, size, numberOfEscapeSequences, escapeSequencePos); // OK, do it the slow way. return; } EnsureBuffer(size + 2); _buffer[_pos++] = Quote; if (numberOfEscapeSequences == 0) { // PERF: Fast Path. WriteRawString(strBuffer, size); } else { UnlikelyWriteEscapeSequences(strBuffer, size, numberOfEscapeSequences, escapeSequencePos); } _buffer[_pos++] = Quote; }