@@ -739,7 +739,7 @@ public String toAlphaCode(@Nonnull final AlphaCodeFormat format, @Nullable final
739739 checkNonnull ("format" , format );
740740 String result = name ().replace ('_' , '-' );
741741 if (format != AlphaCodeFormat .INTERNATIONAL ) {
742- final int index = name ().indexOf ('_' );
742+ final int index = name ().lastIndexOf ('_' );
743743 if (index != -1 ) {
744744 assert name ().length () > (index + 1 );
745745 final String shortName = name ().substring (index + 1 );
@@ -903,11 +903,15 @@ private Territory(
903903 * @return Territory.
904904 * @throws UnknownTerritoryException Thrown if the territory is not found.
905905 */
906+ @ SuppressWarnings ("TailRecursion" )
906907 @ Nonnull
907908 private static Territory createFromString (
908909 @ Nonnull final String alphaCode ,
909910 @ Nullable final Territory parentTerritory ) throws UnknownTerritoryException {
910- final String trimmed = Mapcode .convertStringToPlainAscii (alphaCode .trim ().replace ('_' , '-' )).toUpperCase ();
911+
912+ // Replace '_' with '-', but leave spaces alone (may be part of the name).
913+ final String trimmed = Mapcode .convertStringToPlainAscii (
914+ alphaCode .trim ().replace ('_' , '-' )).toUpperCase ();
911915
912916 // Try as alpha code.
913917 final List <Territory > territories = nameMap .get (trimmed );
@@ -920,14 +924,23 @@ private static Territory createFromString(
920924 return territory ;
921925 }
922926 }
923- throw new UnknownTerritoryException (trimmed );
924- }
927+ } else {
925928
926- // Check for a case such as USA-NLD (=NLD)
927- final int dividerLocation = Math .max (trimmed .indexOf ('-' ), trimmed .indexOf (' ' ));
928- if (dividerLocation >= 0 ) {
929- //noinspection TailRecursion
930- return createFromString (trimmed .substring (dividerLocation + 1 ), parentTerritory );
929+ // Check for a case such as "United States of America-IN".
930+ final int lastSeparator = Math .max ( // Find last separator.
931+ trimmed .lastIndexOf ('-' ), // Allow '-'.
932+ trimmed .lastIndexOf (' ' )); // And ' '.
933+ if (lastSeparator >= 0 ) {
934+ final String prefix = trimmed .substring (0 , lastSeparator );
935+ final Territory parent = createFromString (prefix , parentTerritory );
936+ if (PARENT_TERRITORIES .contains (parent )) {
937+ final String postfix = trimmed .substring (lastSeparator + 1 );
938+ final Territory child = createFromString (postfix , parentTerritory );
939+ if (child .parentTerritory == parent ) {
940+ return child ;
941+ }
942+ }
943+ }
931944 }
932945 throw new UnknownTerritoryException (trimmed );
933946 }
@@ -946,7 +959,7 @@ private static void addNameWithParentVariants(
946959 addNameWithSeperatorVariants (name , territory );
947960
948961 if (name .contains ("-" )) {
949- final String childTerritoryName = name .substring (name .indexOf ('-' ) + 1 );
962+ final String childTerritoryName = name .substring (name .lastIndexOf ('-' ) + 1 );
950963
951964 // Tolerate a child territory specified without the parent.
952965 // (e.g. "CA" rather than "US-CA")
0 commit comments