Skip to content

Commit

Permalink
Removed unnecessary casting and logic
Browse files Browse the repository at this point in the history
  • Loading branch information
greymistcube committed Dec 1, 2024
1 parent 2a04ca2 commit e9fa230
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 90 deletions.
68 changes: 34 additions & 34 deletions Bencodex.Tests/EncoderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,56 +30,56 @@ public void EstimateLength()
public void EncodeNull()
{
var buffer = new byte[10];
long offset = 3L;
int offset = 3;
Encoder.EncodeNull(buffer, ref offset);
Assert.Equal(3L + 1L, offset);
Assert.Equal(3 + 1, offset);
AssertEqual(new byte[] { 0, 0, 0, 0x6e, 0, 0, 0, 0, 0, 0 }, buffer);
}

[Fact]
public void EncodeBoolean()
{
var buffer = new byte[10];
long offset = 2L;
int offset = 2;
Encoder.EncodeBoolean(new Boolean(true), buffer, ref offset);
Assert.Equal(2L + 1L, offset);
Assert.Equal(2 + 1, offset);
AssertEqual(new byte[] { 0, 0, 0x74, 0, 0, 0, 0, 0, 0, 0 }, buffer);

offset = 5L;
offset = 5;
Encoder.EncodeBoolean(new Boolean(false), buffer, ref offset);
Assert.Equal(5L + 1L, offset);
Assert.Equal(5 + 1, offset);
AssertEqual(new byte[] { 0, 0, 0x74, 0, 0, 0x66, 0, 0, 0, 0 }, buffer);
}

[Fact]
public void EncodeInteger()
{
var buffer = new byte[10];
long offset = 2L;
int offset = 2;
Encoder.EncodeInteger(0, buffer, ref offset);
Assert.Equal(2L + 3L, offset);
Assert.Equal(2 + 3, offset);
AssertEqual(new byte[] { 0, 0, 0x69, 0x30, 0x65, 0, 0, 0, 0, 0 }, buffer);

Clear(buffer, 0, buffer.Length);
offset = 1L;
offset = 1;
Encoder.EncodeInteger(-123, buffer, ref offset);
Assert.Equal(1L + 6L, offset);
Assert.Equal(1 + 6, offset);
AssertEqual(new byte[] { 0, 0x69, 0x2d, 0x31, 0x32, 0x33, 0x65, 0, 0, 0 }, buffer);

Clear(buffer, 0, buffer.Length);
offset = 4L;
offset = 4;
Encoder.EncodeInteger(456, buffer, ref offset);
Assert.Equal(4L + 5L, offset);
Assert.Equal(4 + 5, offset);
AssertEqual(new byte[] { 0, 0, 0, 0, 0x69, 0x34, 0x35, 0x36, 0x65, 0 }, buffer);
}

[Fact]
public void EncodeBinary()
{
var buffer = new byte[20];
long offset = 2L;
int offset = 2;
Encoder.EncodeBinary(new Binary("hello world", Encoding.ASCII), buffer, ref offset);
Assert.Equal(2L + 14L, offset);
Assert.Equal(2 + 14, offset);
AssertEqual(
new byte[20]
{
Expand All @@ -97,9 +97,9 @@ public void EncodeBinary()
public void EncodeText()
{
var buffer = new byte[20];
long offset = 5L;
int offset = 5;
Encoder.EncodeText("한글", buffer, ref offset);
Assert.Equal(5L + 9L, offset);
Assert.Equal(5 + 9, offset);
AssertEqual(
new byte[20]
{
Expand All @@ -115,7 +115,7 @@ public void EncodeText()
[Fact]
public void CountDecimalDigits()
{
for (long i = 0; i <= 1000L; i++)
for (int i = 0; i <= 1000; i++)
{
Assert.Equal(
i.ToString(CultureInfo.InvariantCulture).Length,
Expand All @@ -126,7 +126,7 @@ public void CountDecimalDigits()
var random = new System.Random();
for (int i = 0; i < 100; i++)
{
long n = (long)random.Next(0, int.MaxValue);
int n = random.Next(0, int.MaxValue);
Assert.Equal(
n.ToString(CultureInfo.InvariantCulture).Length,
Encoder.CountDecimalDigits(n)
Expand All @@ -138,35 +138,35 @@ public void CountDecimalDigits()
public void EncodeDigits()
{
var buffer = new byte[10];
long offset = 2L;
Encoder.EncodeDigits(0L, buffer, ref offset);
Assert.Equal(2L + 1L, offset);
int offset = 2;
Encoder.EncodeDigits(0, buffer, ref offset);
Assert.Equal(2 + 1, offset);
AssertEqual(new byte[] { 0, 0, 0x30, 0, 0, 0, 0, 0, 0, 0 }, buffer);

Clear(buffer, 0, buffer.Length);
offset = 0L;
Encoder.EncodeDigits(5L, buffer, ref offset);
Assert.Equal(0L + 1L, offset);
offset = 0;
Encoder.EncodeDigits(5, buffer, ref offset);
Assert.Equal(0 + 1, offset);
AssertEqual(new byte[] { 0x35, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, buffer);

Clear(buffer, 0, buffer.Length);
offset = 5L;
Encoder.EncodeDigits(10L, buffer, ref offset);
Assert.Equal(5L + 2L, offset);
offset = 5;
Encoder.EncodeDigits(10, buffer, ref offset);
Assert.Equal(5 + 2, offset);
AssertEqual(new byte[] { 0, 0, 0, 0, 0, 0x31, 0x30, 0, 0, 0 }, buffer);

Clear(buffer, 0, buffer.Length);
offset = 6L;
Encoder.EncodeDigits(123L, buffer, ref offset);
Assert.Equal(6L + 3L, offset);
offset = 6;
Encoder.EncodeDigits(123, buffer, ref offset);
Assert.Equal(6 + 3, offset);
AssertEqual(new byte[] { 0, 0, 0, 0, 0, 0, 0x31, 0x32, 0x33, 0 }, buffer);

Clear(buffer, 0, buffer.Length);
offset = 0L;
Encoder.EncodeDigits(9876543210L, buffer, ref offset);
Assert.Equal(0L + 10L, offset);
offset = 0;
Encoder.EncodeDigits(987654321, buffer, ref offset);
Assert.Equal(0 + 9, offset);
AssertEqual(
new byte[] { 0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x30 },
new byte[] { 0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0 },
buffer
);
}
Expand Down
77 changes: 21 additions & 56 deletions Bencodex/Encoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static byte[] Encode(IValue value)
{
long estimatedLength = EstimateLength(value);
var buffer = new byte[estimatedLength];
long offset = 0;
int offset = 0;
Encode(value, buffer, ref offset);
return buffer;
}
Expand Down Expand Up @@ -79,76 +79,50 @@ internal static long EstimateLength(IValue value)
return value.EncodingLength;
}

internal static void EncodeNull(byte[] buffer, ref long offset)
internal static void EncodeNull(byte[] buffer, ref int offset)
{
buffer[offset++] = _n;
}

internal static void EncodeBoolean(in Types.Boolean value, byte[] buffer, ref long offset)
internal static void EncodeBoolean(in Types.Boolean value, byte[] buffer, ref int offset)
{
buffer[offset++] = value.Value ? _t : _f;
}

internal static void EncodeInteger(in Integer value, byte[] buffer, ref long offset)
internal static void EncodeInteger(in Integer value, byte[] buffer, ref int offset)
{
buffer[offset++] = _i;
string digits = value.Value.ToString(CultureInfo.InvariantCulture);
if (offset + digits.Length <= int.MaxValue)
{
Encoding.ASCII.GetBytes(digits, 0, digits.Length, buffer, (int)offset);
}
else
{
byte[] digitBytes = Encoding.ASCII.GetBytes(digits);
Array.Copy(digitBytes, 0L, buffer, offset, digitBytes.LongLength);
}

Encoding.ASCII.GetBytes(digits, 0, digits.Length, buffer, offset);
offset += digits.Length;
buffer[offset++] = _e;
}

internal static void EncodeBinary(in Binary value, byte[] buffer, ref long offset)
internal static void EncodeBinary(in Binary value, byte[] buffer, ref int offset)
{
long len = value.ByteArray.Length;
int len = value.ByteArray.Length;
EncodeDigits(len, buffer, ref offset);
buffer[offset++] = _c;

if (offset + len <= int.MaxValue)
{
value.ByteArray.CopyTo(buffer, (int)offset);
offset += len;
return;
}

byte[] b = value.ToByteArray();
Array.Copy(b, 0L, buffer, offset, b.LongLength);
value.ByteArray.CopyTo(buffer, offset);
offset += len;
return;
}

internal static void EncodeText(in Text value, byte[] buffer, ref long offset)
internal static void EncodeText(in Text value, byte[] buffer, ref int offset)
{
buffer[offset++] = _u;
int utf8Length = value.Utf8Length;
EncodeDigits(utf8Length, buffer, ref offset);
buffer[offset++] = _c;

string str = value.Value;
if (offset + str.Length <= int.MaxValue)
{
Encoding.UTF8.GetBytes(str, 0, str.Length, buffer, (int)offset);
offset += utf8Length;
return;
}

byte[] utf8 = Encoding.UTF8.GetBytes(value.Value);
Array.Copy(utf8, 0L, buffer, offset, utf8.LongLength);
offset += utf8.LongLength;
Encoding.UTF8.GetBytes(str, 0, str.Length, buffer, offset);
offset += utf8Length;
return;
}

// TODO: Needs a unit test.
internal static void EncodeList(in List value, byte[] buffer, ref long offset)
internal static void EncodeList(in List value, byte[] buffer, ref int offset)
{
buffer[offset++] = _l;
foreach (IValue v in value)
Expand All @@ -161,7 +135,7 @@ internal static void EncodeList(in List value, byte[] buffer, ref long offset)
}

// TODO: Needs a unit test.
internal static void EncodeDictionary(in Dictionary value, byte[] buffer, ref long offset)
internal static void EncodeDictionary(in Dictionary value, byte[] buffer, ref int offset)
{
buffer[offset++] = _d;

Expand All @@ -187,7 +161,7 @@ internal static void EncodeDictionary(in Dictionary value, byte[] buffer, ref lo
return;
}

internal static long CountDecimalDigits(long value)
internal static int CountDecimalDigits(int value)
{
#pragma warning disable SA1503 // Braces should not be omitted
if (value < 10L) return 1;
Expand All @@ -199,34 +173,25 @@ internal static long CountDecimalDigits(long value)
if (value < 10000000L) return 7;
if (value < 100000000L) return 8;
if (value < 1000000000L) return 9;
if (value < 10000000000L) return 10;
if (value < 100000000000L) return 11;
if (value < 1000000000000L) return 12;
if (value < 10000000000000L) return 13;
if (value < 100000000000000L) return 14;
if (value < 1000000000000000L) return 15;
if (value < 10000000000000000L) return 16;
if (value < 100000000000000000L) return 17;
if (value < 1000000000000000000L) return 18;
return 19;
return 10;
#pragma warning restore SA1503
}

internal static void EncodeDigits(long positiveInt, byte[] buffer, ref long offset)
internal static void EncodeDigits(int nonNegativeInt, byte[] buffer, ref int offset)
{
const int asciiZero = 0x30; // '0'
long length = CountDecimalDigits(positiveInt);
for (long i = offset + length - 1; i >= offset; i--)
int length = CountDecimalDigits(nonNegativeInt);
for (int i = offset + length - 1; i >= offset; i--)
{
buffer[i] = (byte)(positiveInt % 10 + asciiZero);
positiveInt /= 10;
buffer[i] = (byte)(nonNegativeInt % 10 + asciiZero);
nonNegativeInt /= 10;
}

offset += length;
}

// TODO: Needs a unit test.
internal static void Encode(in IValue value, byte[] buffer, ref long offset)
internal static void Encode(in IValue value, byte[] buffer, ref int offset)
{
switch (value)
{
Expand Down

0 comments on commit e9fa230

Please sign in to comment.