@@ -225,6 +225,10 @@ static Point decode(@Nonnull final String argMapcode,
225225 Data .getBoundaries (i ).getMaxX (), Data .getBoundaries (i ).getMaxY (),
226226 i , extrapostfix );
227227
228+ // first of all, make sure the zone fits the country
229+ if (!mapcodeZone .isEmpty () && (territory != territory .AAA )) {
230+ mapcodeZone = mapcodeZone .restrictZoneTo (Data .getBoundaries (upto ));
231+ }
228232
229233 if (Data .isRestricted (i ) && !mapcodeZone .isEmpty ()) {
230234 int nrZoneOverlaps = 0 ;
@@ -233,8 +237,6 @@ static Point decode(@Nonnull final String argMapcode,
233237 // see if midpoint of mapcode zone is in any sub-area...
234238 for (j = i - 1 ; j >= from ; j --) {
235239 if (!Data .isRestricted (j )) {
236- final int xdiv8 = Common .xDivider (Data .getBoundaries (j ).getMinY (),
237- Data .getBoundaries (j ).getMaxY ()) / 4 ;
238240 if (Data .getBoundaries (j ).containsPoint (result )) {
239241 nrZoneOverlaps ++;
240242 break ;
@@ -292,15 +294,10 @@ static Point decode(@Nonnull final String argMapcode,
292294 }
293295 }
294296
295- final Point result ;
296- if (mapcodeZone .isEmpty ()) {
297- result = Point .undefined ();
298- } else if (territory == territory .AAA ) {
299- result = mapcodeZone .midPoint ().wrap ();
300- } else {
297+ if (territory != territory .AAA ) {
301298 mapcodeZone = mapcodeZone .restrictZoneTo (Data .getBoundaries (upto ));
302- result = mapcodeZone .midPoint ().wrap ();
303299 }
300+ final Point result = mapcodeZone .midPoint ().wrap ();
304301
305302 LOG .trace ("decode: result=({}, {})" ,
306303 result .isDefined () ? result .getLatDeg () : Double .NaN ,
@@ -551,6 +548,10 @@ private static MapcodeZone decodeNameless(final String str, final int firstrec,
551548 }
552549 }
553550
551+ if (nrX > a ) { // past end!
552+ return MapcodeZone .empty ();
553+ }
554+
554555 final int m = firstrec + nrX ;
555556
556557 int side = DataAccess .smartDiv (m );
@@ -910,8 +911,8 @@ private static MapcodeZone decodeExtension(final int y, final int x, final int d
910911 processor *= 30 ;
911912 }
912913
913- double lon4 = (x * 4 * Point .MAX_PRECISION_FACTOR ) + (( lon32 * dividerx4 ) ) + (lon_offset4 * Point .MAX_PRECISION_FACTOR );
914- double lat1 = (y * Point .MAX_PRECISION_FACTOR ) + (( lat32 * dividery ) );
914+ double lon4 = (x * 4 * Point .MAX_PRECISION_FACTOR ) + (lon32 * dividerx4 ) + (lon_offset4 * Point .MAX_PRECISION_FACTOR );
915+ double lat1 = (y * Point .MAX_PRECISION_FACTOR ) + (lat32 * dividery );
915916
916917 // determine the range of coordinates that are encode to this mapcode
917918 if (odd ) { // odd
0 commit comments