mirror of
https://github.com/soarqin/DSP_Mods.git
synced 2025-12-09 14:13:31 +08:00
code cleanup
This commit is contained in:
@@ -7,21 +7,21 @@ namespace CompressSave.Wrapper;
|
||||
|
||||
public unsafe class BufferWriter : BinaryWriter
|
||||
{
|
||||
ByteSpan currentBuffer => doubleBuffer.writeBuffer;
|
||||
private ByteSpan CurrentBuffer => _doubleBuffer.WriteBuffer;
|
||||
|
||||
DoubleBuffer doubleBuffer;
|
||||
private readonly DoubleBuffer _doubleBuffer;
|
||||
|
||||
private Encoding _encoding;
|
||||
private readonly Encoding _encoding;
|
||||
|
||||
private int maxBytesPerChar;
|
||||
private readonly int _maxBytesPerChar;
|
||||
|
||||
byte[] Buffer => currentBuffer.Buffer;
|
||||
private byte[] Buffer => CurrentBuffer.Buffer;
|
||||
|
||||
long SuplusCapacity => endPos - curPos;
|
||||
private long SuplusCapacity => _endPos - _curPos;
|
||||
|
||||
long swapedBytes = 0;
|
||||
private long _swapedBytes;
|
||||
|
||||
public long WriteSum => swapedBytes + curPos - startPos;
|
||||
public long WriteSum => _swapedBytes + _curPos - _startPos;
|
||||
|
||||
public override Stream BaseStream => _baseStream;
|
||||
|
||||
@@ -29,16 +29,16 @@ public unsafe class BufferWriter : BinaryWriter
|
||||
{
|
||||
if (chars == null)
|
||||
{
|
||||
throw new ArgumentNullException("chars");
|
||||
throw new ArgumentNullException(nameof(chars));
|
||||
}
|
||||
byte[] bytes = _encoding.GetBytes(chars, index, count);
|
||||
Write(bytes);
|
||||
}
|
||||
|
||||
byte* curPos;
|
||||
byte* endPos;
|
||||
byte* startPos;
|
||||
private Stream _baseStream;
|
||||
private byte* _curPos;
|
||||
private byte* _endPos;
|
||||
private byte* _startPos;
|
||||
private readonly Stream _baseStream;
|
||||
|
||||
public BufferWriter(DoubleBuffer doubleBuffer, CompressionStream outStream)
|
||||
: this(doubleBuffer, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true), outStream)
|
||||
@@ -46,34 +46,34 @@ public unsafe class BufferWriter : BinaryWriter
|
||||
|
||||
}
|
||||
|
||||
BufferWriter(DoubleBuffer buffer , UTF8Encoding encoding, CompressionStream outStream) : base(Stream.Null, encoding)
|
||||
private BufferWriter(DoubleBuffer buffer , UTF8Encoding encoding, CompressionStream outStream) : base(Stream.Null, encoding)
|
||||
{
|
||||
_baseStream = outStream;
|
||||
swapedBytes = 0;
|
||||
doubleBuffer = buffer;
|
||||
_swapedBytes = 0;
|
||||
_doubleBuffer = buffer;
|
||||
RefreshStatus();
|
||||
_encoding = encoding;
|
||||
maxBytesPerChar = _encoding.GetMaxByteCount(1);
|
||||
_maxBytesPerChar = _encoding.GetMaxByteCount(1);
|
||||
}
|
||||
|
||||
void SwapBuffer()
|
||||
private void SwapBuffer()
|
||||
{
|
||||
currentBuffer.Position = 0;
|
||||
CurrentBuffer.Position = 0;
|
||||
|
||||
currentBuffer.Length = (int)(curPos - startPos);
|
||||
swapedBytes += currentBuffer.Length;
|
||||
doubleBuffer.SwapBuffer();
|
||||
CurrentBuffer.Length = (int)(_curPos - _startPos);
|
||||
_swapedBytes += CurrentBuffer.Length;
|
||||
_doubleBuffer.SwapBuffer();
|
||||
RefreshStatus();
|
||||
}
|
||||
|
||||
void RefreshStatus()
|
||||
private void RefreshStatus()
|
||||
{
|
||||
startPos = (byte*)Unsafe.AsPointer(ref Buffer[0]);
|
||||
curPos = startPos;
|
||||
endPos = (byte*)Unsafe.AsPointer(ref Buffer[Buffer.Length - 1]) + 1;
|
||||
_startPos = (byte*)Unsafe.AsPointer(ref Buffer[0]);
|
||||
_curPos = _startPos;
|
||||
_endPos = (byte*)Unsafe.AsPointer(ref Buffer[Buffer.Length - 1]) + 1;
|
||||
}
|
||||
|
||||
void CheckCapacityAndSwap(int requiredCapacity)
|
||||
private void CheckCapacityAndSwap(int requiredCapacity)
|
||||
{
|
||||
if (SuplusCapacity < requiredCapacity)
|
||||
{
|
||||
@@ -84,7 +84,7 @@ public unsafe class BufferWriter : BinaryWriter
|
||||
public override void Write(byte value)
|
||||
{
|
||||
CheckCapacityAndSwap(1);
|
||||
*(curPos++) = value;
|
||||
*(_curPos++) = value;
|
||||
}
|
||||
|
||||
public override void Write(bool value) => Write((byte)(value ? 1 : 0));
|
||||
@@ -115,43 +115,43 @@ public unsafe class BufferWriter : BinaryWriter
|
||||
|
||||
public override void Write(sbyte value) => Write((byte)value);
|
||||
|
||||
public override void Write(byte[] _buffer) => Write(_buffer, 0, _buffer.Length);
|
||||
public override void Write(byte[] buffer) => Write(buffer, 0, buffer.Length);
|
||||
|
||||
|
||||
public override void Write(byte[] _buffer, int index, int count)
|
||||
public override void Write(byte[] buffer, int index, int count)
|
||||
{
|
||||
if (_buffer == null)
|
||||
if (buffer == null)
|
||||
{
|
||||
throw new ArgumentNullException("buffer");
|
||||
throw new ArgumentNullException(nameof(buffer));
|
||||
}
|
||||
fixed (byte* start = _buffer)
|
||||
fixed (byte* start = buffer)
|
||||
{
|
||||
byte* srcPos = start + index;
|
||||
while (SuplusCapacity <= count)
|
||||
{
|
||||
int dstSuplus = (int)SuplusCapacity;
|
||||
//Array.Copy(_buffer, index + writed, Buffer, Position, SuplusCapacity);
|
||||
Unsafe.CopyBlock(curPos, srcPos, (uint)dstSuplus);
|
||||
Unsafe.CopyBlock(_curPos, srcPos, (uint)dstSuplus);
|
||||
count -= dstSuplus;
|
||||
srcPos += dstSuplus;
|
||||
curPos = endPos;
|
||||
_curPos = _endPos;
|
||||
SwapBuffer();
|
||||
}
|
||||
Unsafe.CopyBlock(curPos, srcPos, (uint)count);
|
||||
curPos += count;
|
||||
Unsafe.CopyBlock(_curPos, srcPos, (uint)count);
|
||||
_curPos += count;
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe override void Write(char ch)
|
||||
public override void Write(char ch)
|
||||
{
|
||||
if (char.IsSurrogate(ch))
|
||||
{
|
||||
throw new ArgumentException("Arg_SurrogatesNotAllowedAsSingleChar");
|
||||
}
|
||||
|
||||
CheckCapacityAndSwap(maxBytesPerChar);
|
||||
CheckCapacityAndSwap(_maxBytesPerChar);
|
||||
|
||||
curPos += _encoding.GetBytes(&ch, 1, curPos, (int)SuplusCapacity);
|
||||
_curPos += _encoding.GetBytes(&ch, 1, _curPos, (int)SuplusCapacity);
|
||||
}
|
||||
|
||||
//slow
|
||||
@@ -159,24 +159,24 @@ public unsafe class BufferWriter : BinaryWriter
|
||||
{
|
||||
if (chars == null)
|
||||
{
|
||||
throw new ArgumentNullException("chars");
|
||||
throw new ArgumentNullException(nameof(chars));
|
||||
}
|
||||
byte[] bytes = _encoding.GetBytes(chars, 0, chars.Length);
|
||||
Write(bytes);
|
||||
}
|
||||
|
||||
public unsafe override void Write(double value)
|
||||
public override void Write(double value)
|
||||
{
|
||||
CheckCapacityAndSwap(8);
|
||||
ulong num = (ulong)(*(long*)(&value));
|
||||
*(curPos++) = (byte)num;
|
||||
*(curPos++) = (byte)(num >> 8);
|
||||
*(curPos++) = (byte)(num >> 16);
|
||||
*(curPos++) = (byte)(num >> 24);
|
||||
*(curPos++) = (byte)(num >> 32);
|
||||
*(curPos++) = (byte)(num >> 40);
|
||||
*(curPos++) = (byte)(num >> 48);
|
||||
*(curPos++) = (byte)(num >> 56);
|
||||
*(_curPos++) = (byte)num;
|
||||
*(_curPos++) = (byte)(num >> 8);
|
||||
*(_curPos++) = (byte)(num >> 16);
|
||||
*(_curPos++) = (byte)(num >> 24);
|
||||
*(_curPos++) = (byte)(num >> 32);
|
||||
*(_curPos++) = (byte)(num >> 40);
|
||||
*(_curPos++) = (byte)(num >> 48);
|
||||
*(_curPos++) = (byte)(num >> 56);
|
||||
}
|
||||
|
||||
//slow
|
||||
@@ -195,15 +195,15 @@ public unsafe class BufferWriter : BinaryWriter
|
||||
public override void Write(short value)
|
||||
{
|
||||
CheckCapacityAndSwap(2);
|
||||
*(curPos++) = (byte)value;
|
||||
*(curPos++) = (byte)(value >> 8);
|
||||
*(_curPos++) = (byte)value;
|
||||
*(_curPos++) = (byte)(value >> 8);
|
||||
}
|
||||
|
||||
public override void Write(ushort value)
|
||||
{
|
||||
CheckCapacityAndSwap(2);
|
||||
*(curPos++) = (byte)value;
|
||||
*(curPos++) = (byte)(value >> 8);
|
||||
*(_curPos++) = (byte)value;
|
||||
*(_curPos++) = (byte)(value >> 8);
|
||||
}
|
||||
|
||||
|
||||
@@ -213,56 +213,56 @@ public unsafe class BufferWriter : BinaryWriter
|
||||
{
|
||||
SwapBuffer();
|
||||
}
|
||||
*(curPos++) = (byte)value;
|
||||
*(curPos++) = (byte)(value >> 8);
|
||||
*(curPos++) = (byte)(value >> 16);
|
||||
*(curPos++) = (byte)(value >> 24);
|
||||
*(_curPos++) = (byte)value;
|
||||
*(_curPos++) = (byte)(value >> 8);
|
||||
*(_curPos++) = (byte)(value >> 16);
|
||||
*(_curPos++) = (byte)(value >> 24);
|
||||
}
|
||||
|
||||
public override void Write(uint value)
|
||||
{
|
||||
CheckCapacityAndSwap(4);
|
||||
*(curPos++) = (byte)value;
|
||||
*(curPos++) = (byte)(value >> 8);
|
||||
*(curPos++) = (byte)(value >> 16);
|
||||
*(curPos++) = (byte)(value >> 24);
|
||||
*(_curPos++) = (byte)value;
|
||||
*(_curPos++) = (byte)(value >> 8);
|
||||
*(_curPos++) = (byte)(value >> 16);
|
||||
*(_curPos++) = (byte)(value >> 24);
|
||||
}
|
||||
|
||||
|
||||
public override void Write(long value)
|
||||
{
|
||||
CheckCapacityAndSwap(8);
|
||||
*(curPos++) = (byte)value;
|
||||
*(curPos++) = (byte)(value >> 8);
|
||||
*(curPos++) = (byte)(value >> 16);
|
||||
*(curPos++) = (byte)(value >> 24);
|
||||
*(curPos++) = (byte)(value >> 32);
|
||||
*(curPos++) = (byte)(value >> 40);
|
||||
*(curPos++) = (byte)(value >> 48);
|
||||
*(curPos++) = (byte)(value >> 56);
|
||||
*(_curPos++) = (byte)value;
|
||||
*(_curPos++) = (byte)(value >> 8);
|
||||
*(_curPos++) = (byte)(value >> 16);
|
||||
*(_curPos++) = (byte)(value >> 24);
|
||||
*(_curPos++) = (byte)(value >> 32);
|
||||
*(_curPos++) = (byte)(value >> 40);
|
||||
*(_curPos++) = (byte)(value >> 48);
|
||||
*(_curPos++) = (byte)(value >> 56);
|
||||
}
|
||||
|
||||
public override void Write(ulong value)
|
||||
{
|
||||
CheckCapacityAndSwap(8);
|
||||
*(curPos++) = (byte)value;
|
||||
*(curPos++) = (byte)(value >> 8);
|
||||
*(curPos++) = (byte)(value >> 16);
|
||||
*(curPos++) = (byte)(value >> 24);
|
||||
*(curPos++) = (byte)(value >> 32);
|
||||
*(curPos++) = (byte)(value >> 40);
|
||||
*(curPos++) = (byte)(value >> 48);
|
||||
*(curPos++) = (byte)(value >> 56);
|
||||
*(_curPos++) = (byte)value;
|
||||
*(_curPos++) = (byte)(value >> 8);
|
||||
*(_curPos++) = (byte)(value >> 16);
|
||||
*(_curPos++) = (byte)(value >> 24);
|
||||
*(_curPos++) = (byte)(value >> 32);
|
||||
*(_curPos++) = (byte)(value >> 40);
|
||||
*(_curPos++) = (byte)(value >> 48);
|
||||
*(_curPos++) = (byte)(value >> 56);
|
||||
}
|
||||
|
||||
public unsafe override void Write(float value)
|
||||
public override void Write(float value)
|
||||
{
|
||||
CheckCapacityAndSwap(4);
|
||||
uint num = *(uint*)(&value);
|
||||
*(curPos++) = (byte)num;
|
||||
*(curPos++) = (byte)(num >> 8);
|
||||
*(curPos++) = (byte)(num >> 16);
|
||||
*(curPos++) = (byte)(num >> 24);
|
||||
*(_curPos++) = (byte)num;
|
||||
*(_curPos++) = (byte)(num >> 8);
|
||||
*(_curPos++) = (byte)(num >> 16);
|
||||
*(_curPos++) = (byte)(num >> 24);
|
||||
}
|
||||
|
||||
|
||||
@@ -271,7 +271,7 @@ public unsafe class BufferWriter : BinaryWriter
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
throw new ArgumentNullException("value");
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
}
|
||||
byte[] bytes = _encoding.GetBytes(value);
|
||||
Write7BitEncodedInt(bytes.Length);
|
||||
@@ -279,7 +279,7 @@ public unsafe class BufferWriter : BinaryWriter
|
||||
}
|
||||
|
||||
|
||||
protected new void Write7BitEncodedInt(int value)
|
||||
private new void Write7BitEncodedInt(int value)
|
||||
{
|
||||
uint num;
|
||||
for (num = (uint)value; num >= 128; num >>= 7)
|
||||
|
||||
Reference in New Issue
Block a user