Skip to content

Commit 2036823

Browse files
committed
minor chnges
1 parent e6cb1aa commit 2036823

1 file changed

Lines changed: 39 additions & 11 deletions

File tree

src/PolylineAlgorithm/PolylineEncoding.cs

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//
55

66
namespace PolylineAlgorithm;
7+
78
using PolylineAlgorithm.Internal;
89
using PolylineAlgorithm.Properties;
910
using System;
@@ -37,7 +38,7 @@ public static class PolylineEncoding {
3738
/// langword="false"/>.
3839
/// </returns>
3940

40-
public static bool TryReadValue(ref int variance, ref ReadOnlyMemory<char> buffer, ref int position) {
41+
public static bool TryReadValue(ref int variance, ReadOnlyMemory<char> buffer, ref int position) {
4142
// Validate that the position is within the bounds of the buffer.
4243
if (position == buffer.Length) {
4344
return false;
@@ -130,7 +131,7 @@ public static double Denormalize(int value, CoordinateValueType type) {
130131
/// <see langword="true"/> if the value was successfully written to the buffer; otherwise, <see langword="false"/>.
131132
/// </returns>
132133

133-
public static bool TryWriteValue(int variance, ref Span<char> buffer, ref int position) {
134+
public static bool TryWriteValue(int variance, Span<char> buffer, ref int position) {
134135
// Validate that the position and required space for write is within the bounds of the buffer.
135136
if (buffer.Length < position + GetCharCount(variance)) {
136137
return false;
@@ -145,7 +146,10 @@ public static bool TryWriteValue(int variance, ref Span<char> buffer, ref int po
145146

146147
// Write the value to the buffer in a way that encodes it using the specified algorithm.
147148
while (rem >= Defaults.Algorithm.Space) {
148-
buffer[position++] = (char)((Defaults.Algorithm.Space | rem & Defaults.Algorithm.UnitSeparator) + Defaults.Algorithm.QuestionMark);
149+
buffer[position++] =
150+
(char)((Defaults.Algorithm.Space
151+
| (rem & Defaults.Algorithm.UnitSeparator))
152+
+ Defaults.Algorithm.QuestionMark);
149153
rem >>= Defaults.Algorithm.ShiftLength;
150154
}
151155

@@ -198,7 +202,9 @@ public static int Normalize(double value, CoordinateValueType type) {
198202
return 0;
199203
}
200204

201-
return (int)Math.Round(value * Defaults.Algorithm.Precision);
205+
checked {
206+
return (int)Math.Round(value * Defaults.Algorithm.Precision, MidpointRounding.AwayFromZero);
207+
}
202208
}
203209

204210
/// <summary>
@@ -229,24 +235,46 @@ public static int Normalize(double value, CoordinateValueType type) {
229235
_ => 6,
230236
};
231237

238+
/// <summary>
239+
/// Validates whether the specified normalized value falls within the acceptable range for the given value type.
240+
/// </summary>
241+
/// <param name="normalized">
242+
/// The normalized value to validate.
243+
/// </param>
244+
/// <param name="type">
245+
/// The type of value to validate, such as latitude or longitude.
246+
/// </param>
247+
/// <returns>
248+
/// <see langword="true"/> if the <paramref name="normalized"/> is within the valid range for the specified <paramref
249+
/// name="type"/>; otherwise, <see langword="false"/>.
250+
/// </returns>
251+
private static bool ValidateValue(int normalized, CoordinateValueType type) => type switch {
252+
CoordinateValueType.Latitude => normalized >= Defaults.Coordinate.Latitude.Normalized.Min &&
253+
normalized <= Defaults.Coordinate.Latitude.Normalized.Max,
254+
CoordinateValueType.Longitude => normalized >= Defaults.Coordinate.Longitude.Normalized.Min &&
255+
normalized <= Defaults.Coordinate.Longitude.Normalized.Max,
256+
_ => false,
257+
};
258+
232259
/// <summary>
233260
/// Validates whether the specified denormalized value falls within the acceptable range for the given value type.
234261
/// </summary>
235-
/// <param name="value">
262+
/// <param name="denormalized">
236263
/// The denormalized value to validate.
237264
/// </param>
238265
/// <param name="type">
239266
/// The type of value to validate, such as latitude or longitude.
240267
/// </param>
241268
/// <returns>
242-
/// <see langword="true"/> if the <paramref name="value"/> is within the valid range for the specified <paramref
269+
/// <see langword="true"/> if the <paramref name="denormalized"/> is within the valid range for the specified <paramref
243270
/// name="type"/>; otherwise, <see langword="false"/>.
244271
/// </returns>
245-
private static bool ValidateValue<T>(T value, CoordinateValueType type) => (type, value) switch {
246-
(CoordinateValueType.Latitude, int normalized) when normalized >= Defaults.Coordinate.Latitude.Normalized.Min && normalized <= Defaults.Coordinate.Latitude.Normalized.Max => true,
247-
(CoordinateValueType.Longitude, int normalized) when normalized >= Defaults.Coordinate.Longitude.Normalized.Min && normalized <= Defaults.Coordinate.Longitude.Normalized.Max => true,
248-
(CoordinateValueType.Latitude, double denormalized) when denormalized >= Defaults.Coordinate.Latitude.Min && denormalized <= Defaults.Coordinate.Latitude.Max => true,
249-
(CoordinateValueType.Longitude, double denormalized) when denormalized >= Defaults.Coordinate.Longitude.Min && denormalized <= Defaults.Coordinate.Longitude.Max => true,
272+
private static bool ValidateValue(double denormalized, CoordinateValueType type) => type switch {
273+
CoordinateValueType.Latitude => denormalized >= Defaults.Coordinate.Latitude.Min &&
274+
denormalized <= Defaults.Coordinate.Latitude.Max,
275+
CoordinateValueType.Longitude => denormalized >= Defaults.Coordinate.Longitude.Min &&
276+
denormalized <= Defaults.Coordinate.Longitude.Max,
250277
_ => false,
251278
};
279+
252280
}

0 commit comments

Comments
 (0)