From 18244d6b86f7db62219657d484e8e5b0b72b78f7 Mon Sep 17 00:00:00 2001 From: Say Cheong Date: Sun, 1 Dec 2024 22:05:59 +0900 Subject: [PATCH 1/2] Removed unnecessary casting and logic --- Bencodex.Tests/EncoderTest.cs | 68 +++++++++++++++---------------- Bencodex/Encoder.cs | 77 ++++++++++------------------------- 2 files changed, 55 insertions(+), 90 deletions(-) diff --git a/Bencodex.Tests/EncoderTest.cs b/Bencodex.Tests/EncoderTest.cs index f99f16f..14e3c08 100644 --- a/Bencodex.Tests/EncoderTest.cs +++ b/Bencodex.Tests/EncoderTest.cs @@ -30,9 +30,9 @@ 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); } @@ -40,14 +40,14 @@ public void EncodeNull() 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); } @@ -55,21 +55,21 @@ public void EncodeBoolean() 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); } @@ -77,9 +77,9 @@ public void EncodeInteger() 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] { @@ -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] { @@ -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, @@ -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) @@ -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 ); } diff --git a/Bencodex/Encoder.cs b/Bencodex/Encoder.cs index f53981a..04b7f13 100644 --- a/Bencodex/Encoder.cs +++ b/Bencodex/Encoder.cs @@ -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; } @@ -79,54 +79,36 @@ 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; @@ -134,21 +116,13 @@ internal static void EncodeText(in Text value, byte[] buffer, ref long 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) @@ -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; @@ -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; @@ -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) { From 531be58b5bf827f021e91dcb9594597ace211bd0 Mon Sep 17 00:00:00 2001 From: Say Cheong Date: Sun, 1 Dec 2024 22:08:31 +0900 Subject: [PATCH 2/2] Changelog --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 4f5cdcc..a1f0b76 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -9,9 +9,11 @@ To be released. - Removed `Dictionary.GetValue()` methods. [[#122]] - Optimized `Encoder.Encode()` method. [[#124]] - Fixed a bug in `Encoder.Encode(IValue, Stream)` method. [[#124]] + - Optimized `Encoder.Encode()` method. [[#125]] [#122]: https://github.com/planetarium/bencodex.net/pull/122 [#124]: https://github.com/planetarium/bencodex.net/pull/124 +[#125]: https://github.com/planetarium/bencodex.net/pull/125 Version 0.16.0