| 1 | #!/usr/bin/perl | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | package Devel::StringInfo; | ||||||||||||||||||||
| 4 | 2 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 2 | ▶ | use utf8 (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use Encode qw(decode encode); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 2 | ▶ | use Encode::Guess (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 2 | ▶ | use Tie::IxHash; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 2 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 2 | our $VERSION = "0.04"; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 2 | ▶ | use Sub::Exporter -setup => { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | exports => [ | ||||||||||||||||||||
| 18 | 1 | ▶ | string_info => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 1 | my ( $class, $name, $args ) = @_; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 1 | my $dumper = $class->new($args); | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 1 | ▶ | return sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | 1 | my $str = shift; | |||||||||||||||||||
| 25 | 1 | $dumper->dump_info($str); | |||||||||||||||||||
| 26 | 1 | }; | |||||||||||||||||||
| 27 | } | ||||||||||||||||||||
| 28 | 2 | ], | |||||||||||||||||||
| 29 | 2 | }; | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 2 | has guess_encoding => ( | |||||||||||||||||||
| 32 | isa => "Bool", | ||||||||||||||||||||
| 33 | is => "rw", | ||||||||||||||||||||
| 34 | default => 1, | ||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | has encoding_suspects => ( | ||||||||||||||||||||
| 38 | isa => "ArrayRef", | ||||||||||||||||||||
| 39 | is => "rw", | ||||||||||||||||||||
| 40 | auto_deref => 1, | ||||||||||||||||||||
| 41 | 2 | ▶ | default => sub { [] }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 42 | 2 | ); | |||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 2 | has include_value_info => ( | |||||||||||||||||||
| 45 | isa => "Bool", | ||||||||||||||||||||
| 46 | is => "rw", | ||||||||||||||||||||
| 47 | default => 0, | ||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 2 | has include_decoded => ( | |||||||||||||||||||
| 51 | isa => "Bool", | ||||||||||||||||||||
| 52 | is => "rw", | ||||||||||||||||||||
| 53 | default => 1, | ||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 2 | has include_hex => ( | |||||||||||||||||||
| 57 | isa => "Bool", | ||||||||||||||||||||
| 58 | is => "rw", | ||||||||||||||||||||
| 59 | default => 0, | ||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 2 | has include_raw => ( | |||||||||||||||||||
| 63 | isa => "Bool", | ||||||||||||||||||||
| 64 | is => "rw", | ||||||||||||||||||||
| 65 | default => 1, | ||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 30 | ▶ | sub sorted_hash { | ||||||||||||||||||
|
Subroutine
sorted_hash: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 69 | 30 | my ( @args ) = @_; | |||||||||||||||||||
| 70 | 30 | tie my %hash, 'Tie::IxHash', @args; | |||||||||||||||||||
| 71 | 30 | return \%hash, | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 12 | ▶ | sub dump_info { | ||||||||||||||||||
|
Subroutine
dump_info: called CC=5 SCAR=18.0
Pod
documented
| |||||||||||||||||||||
| 75 | 12 | my ( $self, $string, @args ) = @_; | |||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 12 | require YAML; | |||||||||||||||||||
| 78 | 12 | local $YAML::SortKeys = 0; # let IxHash decide | |||||||||||||||||||
| 79 | 12 | local $YAML::UseHeader = 0; | |||||||||||||||||||
| 80 | 12 | my $dump = YAML::Dump(sorted_hash @args, $self->filter_data( $self->gather_data($string) )); | |||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 12 | ▶ | if ( $self->include_raw ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | 12 | ▶ | if ( $string =~ /\n/s ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | 0 | $dump .= "raw = <<END_OF_STRING\n$string\nEND_OF_STRING\n"; | |||||||||||||||||||
| 85 | } else { | ||||||||||||||||||||
| 86 | 12 | $dump .= "raw = <<$string>>\n"; | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 12 | ▶ | if ( $self->include_hex ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | 0 | require Data::HexDump::XXD; | |||||||||||||||||||
| 92 | 0 | $dump .= Data::HexDump::XXD::xxd($string) . "\n"; | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 12 | ▶ | if ( defined wantarray ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | 12 | return $dump; | |||||||||||||||||||
| 97 | } else { | ||||||||||||||||||||
| 98 | 0 | warn "$dump\n"; | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 12 | ▶ | sub filter_data { | ||||||||||||||||||
|
Subroutine
filter_data: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 103 | 12 | my ( $self, @args ) = @_; | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 12 | return @args; # FIXME strip out false keys if omit_false, etc | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 23 | ▶ | sub gather_data { | ||||||||||||||||||
|
Subroutine
gather_data: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||||||||
| 109 | 23 | my ( $self, $string ) = @_; | |||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 23 | ▶ | my @ret = ( | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | string => $string, | ||||||||||||||||||||
| 113 | $self->gather_data_unicode($string), | ||||||||||||||||||||
| 114 | ( $self->include_value_info ? $self->gather_data_value($string) : () ),, | ||||||||||||||||||||
| 115 | ); | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 23 | ▶ | wantarray ? @ret : sorted_hash(@ret); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 23 | ▶ | sub gather_data_unicode { | ||||||||||||||||||
|
Subroutine
gather_data_unicode: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 121 | 23 | my ( $self, $string ) = @_; | |||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 23 | ▶ | if ( utf8::is_utf8($string) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 124 | return ( | ||||||||||||||||||||
| 125 | 13 | $self->gather_data_is_unicode($string), | |||||||||||||||||||
| 126 | ); | ||||||||||||||||||||
| 127 | } else { | ||||||||||||||||||||
| 128 | return ( | ||||||||||||||||||||
| 129 | 10 | $self->gather_data_is_octets($string), | |||||||||||||||||||
| 130 | ) | ||||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 0 | ▶ | sub gather_data_vlaue { | ||||||||||||||||||
|
Subroutine
gather_data_vlaue: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 135 | 0 | my ( $self, $string ) = @_; | |||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 0 | for ( $string ) { | |||||||||||||||||||
| 138 | return ( | ||||||||||||||||||||
| 139 | 0 | is_alphanumeric => 0+ /^[[:alnum:]]+$/s, | |||||||||||||||||||
| 140 | is_printable => 0+ /^[[:print:]+]$/s, | ||||||||||||||||||||
| 141 | is_ascii => 0+ /^[[:ascii:]+]$/s, | ||||||||||||||||||||
| 142 | has_zero => 0+ /\x{00}/s, | ||||||||||||||||||||
| 143 | has_line_ending => 0+ /[\r\n]/s, | ||||||||||||||||||||
| 144 | looks_like_number => looks_like_number($string), | ||||||||||||||||||||
| 145 | ); | ||||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | } | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | 13 | ▶ | sub gather_data_is_unicode { | ||||||||||||||||||
|
Subroutine
gather_data_is_unicode: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 150 | 13 | my ( $self, $string ) = @_; | |||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | return ( | ||||||||||||||||||||
| 153 | is_utf8 => 1, | ||||||||||||||||||||
| 154 | char_length => length($string), | ||||||||||||||||||||
| 155 | octet_length => length(encode(utf8 => $string)), | ||||||||||||||||||||
| 156 | 13 | downgradable => 0+ do { | |||||||||||||||||||
| 157 | 13 | my $copy = $string; | |||||||||||||||||||
| 158 | 13 | utf8::downgrade($copy, 1); # fail OK | |||||||||||||||||||
| 159 | }, | ||||||||||||||||||||
| 160 | ); | ||||||||||||||||||||
| 161 | } | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | 10 | ▶ | sub gather_data_is_octets { | ||||||||||||||||||
|
Subroutine
gather_data_is_octets: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||
| 164 | 10 | my ( $self, $string ) = @_; | |||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | return ( | ||||||||||||||||||||
| 167 | 10 | ▶ | is_utf8 => 0, | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 168 | octet_length => length($string), | ||||||||||||||||||||
| 169 | ( utf8::valid($string) | ||||||||||||||||||||
| 170 | ? $self->gather_data_utf8_octets($string) | ||||||||||||||||||||
| 171 | : $self->gather_data_non_utf8_octets($string) ), | ||||||||||||||||||||
| 172 | ); | ||||||||||||||||||||
| 173 | } | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | 10 | ▶ | sub gather_data_utf8_octets { | ||||||||||||||||||
|
Subroutine
gather_data_utf8_octets: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||
| 176 | 10 | my ( $self, $string ) = @_; | |||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | 10 | my $decoded = decode( utf8 => $string ); | |||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | 10 | my $guessed = sorted_hash $self->gather_data_encoding_info($string); | |||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | 10 | ▶ | if ( ($guessed->{guessed_encoding}||'') eq 'utf8' ) { | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$guessed->{'guessed_encoding'} || ''
MC/DC100%1/1
$guessed->{'guessed_encoding'} || ''
$guessed->{'guessed_encoding'}
Truth table100%2/2
A
A= $guessed->{'guessed_encoding'}
| |||||||||||||||||||||
| 183 | return ( | ||||||||||||||||||||
| 184 | 2 | ▶ | valid_utf8 => 1, | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 185 | ( $self->include_decoded ? $self->gather_data_decoded( $decoded, $string ) : () ),, | ||||||||||||||||||||
| 186 | ); | ||||||||||||||||||||
| 187 | } else { | ||||||||||||||||||||
| 188 | return ( | ||||||||||||||||||||
| 189 | 8 | ▶ | valid_utf8 => 1, | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 190 | ( $self->include_decoded ? ( | ||||||||||||||||||||
| 191 | as_utf8 => sorted_hash($self->gather_data_decoded( $decoded, $string ) ), | ||||||||||||||||||||
| 192 | as_guess => $guessed, | ||||||||||||||||||||
| 193 | ) : () ), | ||||||||||||||||||||
| 194 | ); | ||||||||||||||||||||
| 195 | } | ||||||||||||||||||||
| 196 | } | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | 0 | ▶ | sub gather_data_non_utf8_octets { | ||||||||||||||||||
|
Subroutine
gather_data_non_utf8_octets: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 199 | 0 | my ( $self, $string ) = @_; | |||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | return ( | ||||||||||||||||||||
| 202 | 0 | valid_utf8 => 0, | |||||||||||||||||||
| 203 | $self->gather_data_encoding_info($string), | ||||||||||||||||||||
| 204 | ); | ||||||||||||||||||||
| 205 | } | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | 10 | ▶ | sub gather_data_encoding_info { | ||||||||||||||||||
|
Subroutine
gather_data_encoding_info: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||
| 208 | 10 | my ( $self, $string ) = @_; | |||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | 10 | ▶ | return unless $self->guess_encoding; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | 9 | my $decoder = Encode::Guess::guess_encoding( $string, $self->encoding_suspects ); | |||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | 9 | ▶ | if ( ref $decoder ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 215 | 8 | my $decoded = $decoder->decode($string); | |||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | return ( | ||||||||||||||||||||
| 218 | 8 | ▶ | guessed_encoding => $decoder->name, | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 219 | ( $self->include_decoded ? $self->gather_data_decoded( $decoded, $string ) : () ), | ||||||||||||||||||||
| 220 | ); | ||||||||||||||||||||
| 221 | } else { | ||||||||||||||||||||
| 222 | return ( | ||||||||||||||||||||
| 223 | 1 | guess_error => $decoder, | |||||||||||||||||||
| 224 | ); | ||||||||||||||||||||
| 225 | } | ||||||||||||||||||||
| 226 | } | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | 18 | ▶ | sub gather_data_decoded { | ||||||||||||||||||
|
Subroutine
gather_data_decoded: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 229 | 18 | my ( $self, $decoded, $string ) = @_; | |||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | 18 | ▶ | if ( $string ne $decoded ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 232 | return ( | ||||||||||||||||||||
| 233 | 11 | decoded_is_same => 0, | |||||||||||||||||||
| 234 | decoded => { | ||||||||||||||||||||
| 235 | string => $decoded, | ||||||||||||||||||||
| 236 | $self->gather_data($decoded), | ||||||||||||||||||||
| 237 | } | ||||||||||||||||||||
| 238 | ); | ||||||||||||||||||||
| 239 | } else { | ||||||||||||||||||||
| 240 | return ( | ||||||||||||||||||||
| 241 | 7 | decoded_is_same => 1, | |||||||||||||||||||
| 242 | ); | ||||||||||||||||||||
| 243 | } | ||||||||||||||||||||
| 244 | } | ||||||||||||||||||||
| 245 | |||||||||||||||||||||
| 246 | 2 | __PACKAGE__; | |||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | __END__ | ||||||||||||||||||||