@@ -311,10 +311,16 @@ sub format_address {
311311 say STDERR " component_aliases" ;
312312 say STDERR Dumper $self -> {component_aliases };
313313 }
314-
315314 # done with the options
316315
317- # 3. set the aliases, unless this would overwrite something
316+ # 3. deal wtih terrible inputs
317+ $self -> _sanity_cleaning($rh_components );
318+ if ($debug ){
319+ say STDERR " after sanity_cleaning applied" ;
320+ say STDERR Dumper $rh_components ;
321+ }
322+
323+ # 4. set the aliases, unless this would overwrite something
318324 # need to do this in the right order (as defined in the components file)
319325 # For example:
320326 # both 'city_district' and 'suburb' are aliases of 'neighbourhood'
@@ -364,13 +370,6 @@ sub format_address {
364370 say STDERR Dumper $rh_components ;
365371 }
366372
367- # 4. deal wtih terrible inputs
368- $self -> _sanity_cleaning($rh_components );
369- if ($debug ){
370- say STDERR " after sanity_cleaning applied" ;
371- say STDERR Dumper $rh_components ;
372- }
373-
374373 # 5. determine the template
375374 my $template_text ;
376375 my $rh_config = $self -> {templates }{uc ($cc )} || $self -> {templates }{default };
@@ -605,7 +604,7 @@ sub _determine_country_code {
605604 if (my $cc = lc ($rh_components -> {country_code })) {
606605
607606 # is it two letters long?
608- return if ($cc !~ m / ^ [a-z][a-z] $ / );
607+ return if (length ( $cc ) != 2 );
609608 return ' GB' if ($cc eq ' uk' );
610609
611610 $cc = uc ($cc );
@@ -663,17 +662,14 @@ sub _determine_country_code {
663662
664663# hacks for bad country data
665664sub _fix_country {
666- my $self = shift ;
667- my $rh_components = shift || return ;
665+ my $self = shift ;
666+ my $rh_c = shift || return ;
668667
669668 # is the country a number?
670669 # if so, and there is a state, use state as country
671- if (defined ($rh_components -> {country })) {
672- if (looks_like_number($rh_components -> {country })) {
673- if (defined ($rh_components -> {state })) {
674- $rh_components -> {country } = $rh_components -> {state };
675- delete $rh_components -> {state };
676- }
670+ if (defined ($rh_c -> {country }) && defined ($rh_c -> {state })){
671+ if (looks_like_number($rh_c -> {country })){
672+ $rh_c -> {country } = delete $rh_c -> {state };
677673 }
678674 }
679675 return ;
@@ -683,14 +679,20 @@ sub _fix_country {
683679# note may also set other values in some odd edge cases
684680sub _add_state_code {
685681 my $self = shift ;
686- my $rh_components = shift ;
687- return $self -> _add_code(' state' , $rh_components );
682+ my $rh_components = shift // return ;
683+ if (defined ($rh_components -> {state })){
684+ return $self -> _add_code(' state' , $rh_components );
685+ }
686+ return ;
688687}
689688
690689sub _add_county_code {
691690 my $self = shift ;
692- my $rh_components = shift ;
693- return $self -> _add_code(' county' , $rh_components );
691+ my $rh_components = shift // return ;
692+ if (defined ($rh_components -> {county })){
693+ return $self -> _add_code(' county' , $rh_components );
694+ }
695+ return ;
694696}
695697
696698sub _add_code {
@@ -702,18 +704,15 @@ sub _add_code {
702704
703705 my $code = $keyname . ' _code' ;
704706
705- if (defined ($rh_components -> {$code })) { # do we already have code?
706- # but could have situation
707- # where code and long name are
708- # the same which we want to correct
709- if ($rh_components -> {$code } ne $rh_components -> {$keyname }) {
710- return ;
711- }
707+ if (defined ($rh_components -> {$code })) {
708+ # do we already have code?
709+ # we could have situation where code and long name are same
710+ # so we want to corrent
711+ return if ($rh_components -> {$code } ne $rh_components -> {$keyname });
712712 }
713713
714714 # ensure country_code is uppercase as we use it as conf key
715- $rh_components -> {country_code } = uc ($rh_components -> {country_code });
716- my $cc = $rh_components -> {country_code };
715+ my $cc = uc ($rh_components -> {country_code });
717716
718717 if (my $mapping = $self -> {$code . ' s' }{$cc }) {
719718
@@ -731,7 +730,9 @@ sub _add_code {
731730 push (@confnames , $mapping -> {$abbrv });
732731 }
733732
733+ # FIXME: should only uc the names once when reading from conf
734734 foreach my $confname (@confnames ) {
735+
735736 if ($uc_name eq uc ($confname )) {
736737 $rh_components -> {$code } = $abbrv ;
737738 last LOCCODE;
@@ -752,9 +753,9 @@ sub _add_code {
752753 # didn't find a valid code or name
753754
754755 # try again for odd variants like "United States Virgin Islands"
755- if ($keyname eq ' state ' ) {
756- if (! defined ( $rh_components -> { state_code }) ) {
757- if ($cc eq ' US ' ) {
756+ if ($cc eq ' US ' ) {
757+ if ($keyname eq ' state ' ) {
758+ if (! defined ( $rh_components -> { state_code }) ) {
758759 if ($rh_components -> {state } =~ m / ^united states/ i ) {
759760 my $state = $rh_components -> {state };
760761 $state =~ s / ^United States/ US/ i ;
@@ -976,34 +977,35 @@ sub _select_first {
976977}
977978
978979my %small_district = (
979- ' br ' => 1,
980- ' cr ' => 1,
981- ' es ' => 1,
982- ' ni ' => 1,
983- ' py ' => 1,
984- ' ro ' => 1,
985- ' tg ' => 1,
986- ' tm ' => 1,
987- ' xk ' => 1,
980+ ' BR ' => 1,
981+ ' CR ' => 1,
982+ ' ES ' => 1,
983+ ' NI ' => 1,
984+ ' PY ' => 1,
985+ ' RO ' => 1,
986+ ' TG ' => 1,
987+ ' TM ' => 1,
988+ ' XK ' => 1,
988989);
989990
990991# correct the alias for "district"
991992# in OSM some countries use district to mean "city_district"
992993# others to mean "state_district"
993994sub _set_district_alias {
994995 my $self = shift ;
995- my $cc = shift ;
996+ my $cc = shift // return ;
997+
998+ my $ucc = uc ($cc );
996999
9971000 # this may get called repeatedly
9981001 # no need to do the work again
999- if (defined ($cc )){
1000- my $ucc = uc ($cc );
1001- return if (defined ($self -> {set_district_alias }{$ucc }));
1002- $self -> {set_district_alias }{$ucc } = 1;
1003- }
1002+ return if (defined ($self -> {set_district_alias }{$ucc }));
1003+
1004+ # note that we are here so don't do this work again
1005+ $self -> {set_district_alias }{$ucc } = 1;
10041006
10051007 my $oldalias ;
1006- if (defined ($small_district {$cc })){
1008+ if (defined ($small_district {$ucc })){
10071009 $self -> {component2type }{district } = ' neighbourhood' ;
10081010 $oldalias = ' state_district' ;
10091011
@@ -1021,7 +1023,9 @@ sub _set_district_alias {
10211023 }
10221024
10231025 # remove from the old alias list
1024- my @temp = grep { $_ ne ' district' } @{$self -> {component_aliases }{$oldalias }};
1026+ my @temp = grep { $_ ne ' district' }
1027+ @{$self -> {component_aliases }{$oldalias }};
1028+
10251029 $self -> {component_aliases }{$oldalias } = \@temp ;
10261030 return ;
10271031}
0 commit comments