| 1 | package Bio::Gonzales::String::Util; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use Carp; | ||
| 6 | |||
| 7 | use 5.010; | ||
| 8 | |||
| 9 | use base 'Exporter'; | ||
| 10 | our ( @EXPORT, @EXPORT_OK, %EXPORT_TAGS ); | ||
| 11 | our $VERSION = 0.01_01; | ||
| 12 | |||
| 13 | @EXPORT = qw(); | ||
| 14 | %EXPORT_TAGS = (); | ||
| 15 | @EXPORT_OK = qw(common_prefix common_suffix); | ||
| 16 | |||
| 17 | sub common_suffix { | ||
| 18 | my $comm = shift @_; | ||
| 19 | while ( my $e = shift @_ ) { | ||
| 20 | $e = substr( $e, -length($comm) ) | ||
| 21 | if ( length($e) > length($comm) ); | ||
| 22 | $comm = substr( $comm, -length($e) ) | ||
| 23 | if ( length($e) < length($comm) ); | ||
| 24 | if ( ( $e ^ $comm ) =~ /(\0*)$/ ) { | ||
| 25 | $comm = substr( $comm, -length($1) ); | ||
| 26 | } else { | ||
| 27 | return undef; | ||
| 28 | } | ||
| 29 | } | ||
| 30 | return $comm; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub common_prefix { | ||
| 34 | my $comm = shift @_; | ||
| 35 | while ( my $e = shift @_ ) { | ||
| 36 | $e = substr( $e, 0, length($comm) ) | ||
| 37 | if ( length($e) > length($comm) ); | ||
| 38 | $comm = substr( $comm, 0, length($e) ) | ||
| 39 | if ( length($e) < length($comm) ); | ||
| 40 | if ( ( $e ^ $comm ) =~ /^(\0*)/ ) { | ||
| 41 | $comm = substr( $comm, 0, length($1) ); | ||
| 42 | } else { | ||
| 43 | return undef; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | return $comm; | ||
| 47 | } | ||
| 48 | |||
| 49 | 1; |