Skip to content

Commit e066566

Browse files
author
Petr Sramek
committed
extracted buffer size calculation to static local method
1 parent 4234e27 commit e066566

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/PolylineAlgorithm/PolylineEncoder.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public string Encode(IEnumerable<(double Latitude, double Longitude)> coordinate
4343

4444
// Initializing local variables
4545
int index = 0;
46-
Memory<char> buffer = new char[count * 12];
46+
Memory<char> buffer = new char[GetSafeBufferSize(count)];
4747
int previousLatitude = 0;
4848
int previousLongitude = 0;
4949

@@ -57,6 +57,10 @@ public string Encode(IEnumerable<(double Latitude, double Longitude)> coordinate
5757
}
5858

5959
return buffer[..index].ToString();
60+
61+
// Each coordinate consist of two values, each one is 4 or 5 chars long
62+
// We use constant 12 = [2 * 6] to create safe buffer size
63+
static int GetSafeBufferSize(int count) => count * 12;
6064
}
6165

6266
private bool TryValidate(IEnumerable<(double Latitude, double Longitude)> collection, ref ICollection<CoordinateValidationException> exceptions) {

0 commit comments

Comments
 (0)