Skip to content

Commit c4c7669

Browse files
Implement max digit behavior
1 parent 1a81ec9 commit c4c7669

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

Source/OpenLocationCode.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,12 @@ let kMinTrimmableCodeLen = 6
8282
/// complete valid charset for Open Location Codes.
8383
let kLegalCharacters = CharacterSet.init(charactersIn: "23456789CFGHJMPQRVWX+0")
8484

85-
/// Default size of encoded Open Location Codes.
85+
/// Default length of encoded Open Location Codes (not including + symbol).
8686
public let kDefaultFullCodeLength = 11
8787

88+
/// The maximum significant digits in a plus code.
89+
let kMaxFullCodeLength = 15
90+
8891
/// Default truncation amount for short codes.
8992
public let kDefaultShortCodeTruncation = 4
9093

@@ -93,7 +96,6 @@ public let kDefaultShortCodeTruncation = 4
9396
/// chars saved doesn't make up for need to resolve).
9497
let kMinShortCodeTruncation = 4
9598

96-
9799
/// Coordinates of a decoded Open Location Code.
98100
/// The coordinates include the latitude and longitude of the lower left and
99101
/// upper right corners and the center of the bounding box for the area the
@@ -499,11 +501,11 @@ public class OpenLocationCode {
499501
/// normalised to the range -180 to 180.
500502
/// - Parameter codeLength: The number of significant digits in the output
501503
/// code, not including any separator characters. Possible values are;
502-
/// `2`, `4`, `6`, `8`, `10`, `11`, `12`, `13` and above. Lower values
504+
/// `2`, `4`, `6`, `8`, `10`, `11`, `12`, `13`, `14`, and `15`. Values
505+
/// above `15` are accepted, but treated as `15`. Lower values
503506
/// indicate a larger area, higher values a more precise area.
504507
/// You can also shorten a code after encoding for codes used with a
505508
/// reference point (e.g. your current location, a city, etc).
506-
///
507509
/// - Returns: Open Location Code for the given coordinate.
508510
public static func encode(latitude: Double,
509511
longitude: Double,
@@ -514,6 +516,8 @@ public class OpenLocationCode {
514516
// 'Invalid Open Location Code length - '
515517
return nil
516518
}
519+
var codeLength = codeLength
520+
codeLength = min(codeLength, kMaxFullCodeLength)
517521

518522
// Ensure that latitude and longitude are valid.
519523
var latitude = clipLatitude(latitude)
@@ -632,6 +636,10 @@ public class OpenLocationCode {
632636
code = String(code.filter {
633637
kCodeAlphabetCharset.contains(String($0).unicodeScalars.first!)
634638
})
639+
// Constrain to max digits (NB. plus symbol already removed).
640+
if (code.length > kMaxFullCodeLength) {
641+
code = code.substring(to:kMaxFullCodeLength)
642+
}
635643

636644
// Decode the lat/lng pair component.
637645
let codeArea = decodePairs(code[0..<kPairCodeLength])!

0 commit comments

Comments
 (0)