Skip to content

Commit d7e5a38

Browse files
committed
CSHARP-1607: Fix indexes for very large arrays.
1 parent 353cdb0 commit d7e5a38

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

src/MongoDB.Bson.Tests/IO/ArrayElementNameAcceleratorTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2014 MongoDB Inc.
1+
/* Copyright 2010-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -36,6 +36,7 @@ public void GetElementNameBytes_should_return_expected_result(int numberOfCached
3636
}
3737
}
3838

39+
[Test]
3940
public void GetElementNameBytes_should_return_expected_result_for_boundary_conditions(
4041
[Values(0, 9, 10, 99, 100, 999, 1000, 9999, 10000, 99999, 100000, 999999, 1000000, 9999999, 100000000, int.MaxValue)]
4142
int index,

src/MongoDB.Bson/IO/ArrayElementNameAccelerator.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2014 MongoDB Inc.
1+
/* Copyright 2010-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -74,26 +74,27 @@ private byte[] CreateElementNameBytes(int index)
7474

7575
const int asciiZero = 48;
7676

77-
var a = (byte)(asciiZero + index % 10);
78-
index = index / 10;
79-
var b = (byte)(asciiZero + index % 10);
80-
index = index / 10;
81-
var c = (byte)(asciiZero + index % 10);
82-
index = index / 10;
83-
var d = (byte)(asciiZero + index % 10);
84-
index = index / 10;
77+
var n = index;
78+
var a = (byte)(asciiZero + n % 10);
79+
n = n / 10;
80+
var b = (byte)(asciiZero + n % 10);
81+
n = n / 10;
82+
var c = (byte)(asciiZero + n % 10);
83+
n = n / 10;
84+
var d = (byte)(asciiZero + n % 10);
85+
n = n / 10;
8586

86-
if (index == 0)
87+
if (n == 0)
8788
{
8889
if (d != (byte)asciiZero) { return new[] { d, c, b, a }; }
8990
if (c != (byte)asciiZero) { return new[] { c, b, a }; }
9091
if (b != (byte)asciiZero) { return new[] { b, a }; }
9192
return new[] { a };
9293
}
9394

94-
var e = (byte)(asciiZero + index % 10);
95-
index = index / 10;
96-
if (index == 0) { return new[] { e, d, c, b, a }; }
95+
var e = (byte)(asciiZero + n % 10);
96+
n = n / 10;
97+
if (n == 0) { return new[] { e, d, c, b, a }; }
9798

9899
// really large indexes should be extremely rare and not worth optimizing further
99100
return Utf8Encodings.Strict.GetBytes(index.ToString());

0 commit comments

Comments
 (0)