Skip to content

Commit 884c09c

Browse files
authored
Merge pull request #125 from greymistcube/refactor/remove-incomplete-feature
Remove improperly supported "unbounded" size
2 parents 65b5d5a + 531be58 commit 884c09c

File tree

3 files changed

+57
-90
lines changed

3 files changed

+57
-90
lines changed

Bencodex.Tests/EncoderTest.cs

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,56 +30,56 @@ public void EstimateLength()
3030
public void EncodeNull()
3131
{
3232
var buffer = new byte[10];
33-
long offset = 3L;
33+
int offset = 3;
3434
Encoder.EncodeNull(buffer, ref offset);
35-
Assert.Equal(3L + 1L, offset);
35+
Assert.Equal(3 + 1, offset);
3636
AssertEqual(new byte[] { 0, 0, 0, 0x6e, 0, 0, 0, 0, 0, 0 }, buffer);
3737
}
3838

3939
[Fact]
4040
public void EncodeBoolean()
4141
{
4242
var buffer = new byte[10];
43-
long offset = 2L;
43+
int offset = 2;
4444
Encoder.EncodeBoolean(new Boolean(true), buffer, ref offset);
45-
Assert.Equal(2L + 1L, offset);
45+
Assert.Equal(2 + 1, offset);
4646
AssertEqual(new byte[] { 0, 0, 0x74, 0, 0, 0, 0, 0, 0, 0 }, buffer);
4747

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

5454
[Fact]
5555
public void EncodeInteger()
5656
{
5757
var buffer = new byte[10];
58-
long offset = 2L;
58+
int offset = 2;
5959
Encoder.EncodeInteger(0, buffer, ref offset);
60-
Assert.Equal(2L + 3L, offset);
60+
Assert.Equal(2 + 3, offset);
6161
AssertEqual(new byte[] { 0, 0, 0x69, 0x30, 0x65, 0, 0, 0, 0, 0 }, buffer);
6262

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

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

7676
[Fact]
7777
public void EncodeBinary()
7878
{
7979
var buffer = new byte[20];
80-
long offset = 2L;
80+
int offset = 2;
8181
Encoder.EncodeBinary(new Binary("hello world", Encoding.ASCII), buffer, ref offset);
82-
Assert.Equal(2L + 14L, offset);
82+
Assert.Equal(2 + 14, offset);
8383
AssertEqual(
8484
new byte[20]
8585
{
@@ -97,9 +97,9 @@ public void EncodeBinary()
9797
public void EncodeText()
9898
{
9999
var buffer = new byte[20];
100-
long offset = 5L;
100+
int offset = 5;
101101
Encoder.EncodeText("한글", buffer, ref offset);
102-
Assert.Equal(5L + 9L, offset);
102+
Assert.Equal(5 + 9, offset);
103103
AssertEqual(
104104
new byte[20]
105105
{
@@ -115,7 +115,7 @@ public void EncodeText()
115115
[Fact]
116116
public void CountDecimalDigits()
117117
{
118-
for (long i = 0; i <= 1000L; i++)
118+
for (int i = 0; i <= 1000; i++)
119119
{
120120
Assert.Equal(
121121
i.ToString(CultureInfo.InvariantCulture).Length,
@@ -126,7 +126,7 @@ public void CountDecimalDigits()
126126
var random = new System.Random();
127127
for (int i = 0; i < 100; i++)
128128
{
129-
long n = (long)random.Next(0, int.MaxValue);
129+
int n = random.Next(0, int.MaxValue);
130130
Assert.Equal(
131131
n.ToString(CultureInfo.InvariantCulture).Length,
132132
Encoder.CountDecimalDigits(n)
@@ -138,35 +138,35 @@ public void CountDecimalDigits()
138138
public void EncodeDigits()
139139
{
140140
var buffer = new byte[10];
141-
long offset = 2L;
142-
Encoder.EncodeDigits(0L, buffer, ref offset);
143-
Assert.Equal(2L + 1L, offset);
141+
int offset = 2;
142+
Encoder.EncodeDigits(0, buffer, ref offset);
143+
Assert.Equal(2 + 1, offset);
144144
AssertEqual(new byte[] { 0, 0, 0x30, 0, 0, 0, 0, 0, 0, 0 }, buffer);
145145

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

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

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

164164
Clear(buffer, 0, buffer.Length);
165-
offset = 0L;
166-
Encoder.EncodeDigits(9876543210L, buffer, ref offset);
167-
Assert.Equal(0L + 10L, offset);
165+
offset = 0;
166+
Encoder.EncodeDigits(987654321, buffer, ref offset);
167+
Assert.Equal(0 + 9, offset);
168168
AssertEqual(
169-
new byte[] { 0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x30 },
169+
new byte[] { 0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0 },
170170
buffer
171171
);
172172
}

Bencodex/Encoder.cs

Lines changed: 21 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static byte[] Encode(IValue value)
2424
{
2525
long estimatedLength = EstimateLength(value);
2626
var buffer = new byte[estimatedLength];
27-
long offset = 0;
27+
int offset = 0;
2828
Encode(value, buffer, ref offset);
2929
return buffer;
3030
}
@@ -79,76 +79,50 @@ internal static long EstimateLength(IValue value)
7979
return value.EncodingLength;
8080
}
8181

82-
internal static void EncodeNull(byte[] buffer, ref long offset)
82+
internal static void EncodeNull(byte[] buffer, ref int offset)
8383
{
8484
buffer[offset++] = _n;
8585
}
8686

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

92-
internal static void EncodeInteger(in Integer value, byte[] buffer, ref long offset)
92+
internal static void EncodeInteger(in Integer value, byte[] buffer, ref int offset)
9393
{
9494
buffer[offset++] = _i;
9595
string digits = value.Value.ToString(CultureInfo.InvariantCulture);
96-
if (offset + digits.Length <= int.MaxValue)
97-
{
98-
Encoding.ASCII.GetBytes(digits, 0, digits.Length, buffer, (int)offset);
99-
}
100-
else
101-
{
102-
byte[] digitBytes = Encoding.ASCII.GetBytes(digits);
103-
Array.Copy(digitBytes, 0L, buffer, offset, digitBytes.LongLength);
104-
}
105-
96+
Encoding.ASCII.GetBytes(digits, 0, digits.Length, buffer, offset);
10697
offset += digits.Length;
10798
buffer[offset++] = _e;
10899
}
109100

110-
internal static void EncodeBinary(in Binary value, byte[] buffer, ref long offset)
101+
internal static void EncodeBinary(in Binary value, byte[] buffer, ref int offset)
111102
{
112-
long len = value.ByteArray.Length;
103+
int len = value.ByteArray.Length;
113104
EncodeDigits(len, buffer, ref offset);
114105
buffer[offset++] = _c;
115-
116-
if (offset + len <= int.MaxValue)
117-
{
118-
value.ByteArray.CopyTo(buffer, (int)offset);
119-
offset += len;
120-
return;
121-
}
122-
123-
byte[] b = value.ToByteArray();
124-
Array.Copy(b, 0L, buffer, offset, b.LongLength);
106+
value.ByteArray.CopyTo(buffer, offset);
125107
offset += len;
126108
return;
127109
}
128110

129-
internal static void EncodeText(in Text value, byte[] buffer, ref long offset)
111+
internal static void EncodeText(in Text value, byte[] buffer, ref int offset)
130112
{
131113
buffer[offset++] = _u;
132114
int utf8Length = value.Utf8Length;
133115
EncodeDigits(utf8Length, buffer, ref offset);
134116
buffer[offset++] = _c;
135117

136118
string str = value.Value;
137-
if (offset + str.Length <= int.MaxValue)
138-
{
139-
Encoding.UTF8.GetBytes(str, 0, str.Length, buffer, (int)offset);
140-
offset += utf8Length;
141-
return;
142-
}
143-
144-
byte[] utf8 = Encoding.UTF8.GetBytes(value.Value);
145-
Array.Copy(utf8, 0L, buffer, offset, utf8.LongLength);
146-
offset += utf8.LongLength;
119+
Encoding.UTF8.GetBytes(str, 0, str.Length, buffer, offset);
120+
offset += utf8Length;
147121
return;
148122
}
149123

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

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

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

190-
internal static long CountDecimalDigits(long value)
164+
internal static int CountDecimalDigits(int value)
191165
{
192166
#pragma warning disable SA1503 // Braces should not be omitted
193167
if (value < 10L) return 1;
@@ -199,34 +173,25 @@ internal static long CountDecimalDigits(long value)
199173
if (value < 10000000L) return 7;
200174
if (value < 100000000L) return 8;
201175
if (value < 1000000000L) return 9;
202-
if (value < 10000000000L) return 10;
203-
if (value < 100000000000L) return 11;
204-
if (value < 1000000000000L) return 12;
205-
if (value < 10000000000000L) return 13;
206-
if (value < 100000000000000L) return 14;
207-
if (value < 1000000000000000L) return 15;
208-
if (value < 10000000000000000L) return 16;
209-
if (value < 100000000000000000L) return 17;
210-
if (value < 1000000000000000000L) return 18;
211-
return 19;
176+
return 10;
212177
#pragma warning restore SA1503
213178
}
214179

215-
internal static void EncodeDigits(long positiveInt, byte[] buffer, ref long offset)
180+
internal static void EncodeDigits(int nonNegativeInt, byte[] buffer, ref int offset)
216181
{
217182
const int asciiZero = 0x30; // '0'
218-
long length = CountDecimalDigits(positiveInt);
219-
for (long i = offset + length - 1; i >= offset; i--)
183+
int length = CountDecimalDigits(nonNegativeInt);
184+
for (int i = offset + length - 1; i >= offset; i--)
220185
{
221-
buffer[i] = (byte)(positiveInt % 10 + asciiZero);
222-
positiveInt /= 10;
186+
buffer[i] = (byte)(nonNegativeInt % 10 + asciiZero);
187+
nonNegativeInt /= 10;
223188
}
224189

225190
offset += length;
226191
}
227192

228193
// TODO: Needs a unit test.
229-
internal static void Encode(in IValue value, byte[] buffer, ref long offset)
194+
internal static void Encode(in IValue value, byte[] buffer, ref int offset)
230195
{
231196
switch (value)
232197
{

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ To be released.
99
- Removed `Dictionary.GetValue<T>()` methods. [[#122]]
1010
- Optimized `Encoder.Encode()` method. [[#124]]
1111
- Fixed a bug in `Encoder.Encode(IValue, Stream)` method. [[#124]]
12+
- Optimized `Encoder.Encode()` method. [[#125]]
1213

1314
[#122]: https://github.com/planetarium/bencodex.net/pull/122
1415
[#124]: https://github.com/planetarium/bencodex.net/pull/124
16+
[#125]: https://github.com/planetarium/bencodex.net/pull/125
1517

1618

1719
Version 0.16.0

0 commit comments

Comments
 (0)