| 1 | package Sisimai::String; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 99 | ▶ | use v5.26; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 99 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 99 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 99 | ▶ | use Encode; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 99 | my $EncodingsC = [qw/big5-eten gb2312/]; | |||||||||||||||||||||||||||||||||||||||||
| 8 | 99 | my $EncodingsE = [qw/iso-8859-1/]; | |||||||||||||||||||||||||||||||||||||||||
| 9 | 99 | my $EncodingsJ = [qw/7bit-jis iso-2022-jp euc-jp shiftjis/]; | |||||||||||||||||||||||||||||||||||||||||
| 10 | 99 | ▶ | use Encode::Guess; Encode::Guess->add_suspects(@$EncodingsC, @$EncodingsE, @$EncodingsJ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 38 | ▶ | sub encodenames { return [@$EncodingsC, @$EncodingsE, @$EncodingsJ] }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encodenames: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 3305 | ▶ | sub is_8bit { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_8bit: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | # The argument is 8-bit text or not | ||||||||||||||||||||||||||||||||||||||||||
| 15 | # @param [String] argv1 Any string to be checked | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # @return [Integer] 0: ASCII Characters only | ||||||||||||||||||||||||||||||||||||||||||
| 17 | # 1: Including 8-bit character | ||||||||||||||||||||||||||||||||||||||||||
| 18 | 3305 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 3305 | ▶ | my $argv1 = shift // return 0; return 0 if ref $argv1 ne 'SCALAR'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
shift() // (return 0)
MC/DC100%1/1
shift() // (return 0)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 3304 | ▶ | return 1 unless $$argv1 =~ /\A[\x00-\x7f]+\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 3252 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 40972 | ▶ | sub sweep { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sweep: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | # Clean the string out | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # @param [String] argv1 String to be cleaned | ||||||||||||||||||||||||||||||||||||||||||
| 27 | # @return [Scalar] Cleaned out string | ||||||||||||||||||||||||||||||||||||||||||
| 28 | # @example Clean up text | ||||||||||||||||||||||||||||||||||||||||||
| 29 | # sweep(' neko ') #=> 'neko' | ||||||||||||||||||||||||||||||||||||||||||
| 30 | 40972 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 31 | 40972 | ▶ | my $argv1 = shift // return ""; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() // (return "")
MC/DC100%1/1
shift() // (return "")
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 40971 | chomp $argv1; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 40971 | y/ //s, s/\A //g, s/ \z//g, s/ [-]{2,}[^ ].+\z// for $argv1; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 40971 | return $argv1; | |||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 279627 | ▶ | sub aligned { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
aligned: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | # Check if each element of the 2nd argument is aligned in the 1st argument or not | ||||||||||||||||||||||||||||||||||||||||||
| 40 | # @param [String] argv1 String to be checked | ||||||||||||||||||||||||||||||||||||||||||
| 41 | # @param [Array] argv2 List including the ordered strings | ||||||||||||||||||||||||||||||||||||||||||
| 42 | # @return [Bool] 0, 1 | ||||||||||||||||||||||||||||||||||||||||||
| 43 | # @since v5.0.0 | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 279627 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 279627 | ▶ | my $argv1 = shift || return 0; return 0 unless length $$argv1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
shift() || (return 0)
MC/DC100%1/1
shift() || (return 0)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 267087 | ▶ | my $argv2 = shift || return 0; return 0 unless scalar @$argv2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
shift() || (return 0)
MC/DC100%1/1
shift() || (return 0)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 267085 | my $align = -1; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 267085 | my $right = 0; | |||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 267085 | for my $e ( @$argv2 ) { | |||||||||||||||||||||||||||||||||||||||||
| 51 | # Get the position of each element in the 1st argument using index() | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 299790 | my $p = index($$argv1, $e, $align + 1); | |||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 299790 | ▶ | last if $p < 0; # Break this loop when there is no string in the 1st argument | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 41461 | $align = length($e) + $p - 1; # There is an aligned string in the 1st argument | |||||||||||||||||||||||||||||||||||||||||
| 56 | 41461 | $right++; | |||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 267085 | ▶ | return 1 if $right == scalar @$argv2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 258329 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 18 | ▶ | sub to_plain { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_plain: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | # Convert given HTML text to plain text | ||||||||||||||||||||||||||||||||||||||||||
| 64 | # @param [Scalar] argv1 HTML text(reference to string) | ||||||||||||||||||||||||||||||||||||||||||
| 65 | # @param [Integer] loose Loose check flag | ||||||||||||||||||||||||||||||||||||||||||
| 66 | # @return [Scalar] Plain text(reference to string) | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 18 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 18 | ▶ | my $argv1 = shift // return undef; return undef if ref $argv1 ne 'SCALAR'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
shift() // (return undef)
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 16 | ▶ | my $loose = shift // 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() // 0
MC/DC100%1/1
shift() // 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 16 | my $plain = $$argv1; | |||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 16 | state $match = { | |||||||||||||||||||||||||||||||||||||||||
| 73 | 'html' => qr|<html[ >].+?</html>|sim, | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 'body' => qr|<head>.+</head>.*<body[ >].+</body>|sim, | ||||||||||||||||||||||||||||||||||||||||||
| 75 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 16 | ▶ | if( $loose || $plain =~ $match->{'html'} || $plain =~ $match->{'body'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$loose or $plain =~ /$match->{"html"}/u or $plain =~ /$match->{"body"}/u
$loose or $plain =~ /$match->{"html"}/u
MC/DC66%2/3
$loose or $plain =~ /$match->{"html"}/u or $plain =~ /$match->{"body"}/u
$loose
$plain =~ /$match->{"html"}/u
$plain =~ /$match->{"body"}/u
Truth table75%3/4
A or B or C
A= $looseB= $plain =~ /$match->{"html"}/uC= $plain =~ /$match->{"body"}/u
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | # <html> ... </html> | ||||||||||||||||||||||||||||||||||||||||||
| 79 | # 1. Remove <head>...</head> | ||||||||||||||||||||||||||||||||||||||||||
| 80 | # 2. Remove <style>...</style> | ||||||||||||||||||||||||||||||||||||||||||
| 81 | # 3. <a href = 'http://...'>...</a> to " http://... " | ||||||||||||||||||||||||||||||||||||||||||
| 82 | # 4. <a href = 'mailto:...'>...</a> to " Value <mailto:...> " | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 4 | my $cv = lc $plain; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 4 | ▶ | $plain =~ s|<head>.+?</head>||gsim if index($cv, '</head>') > -1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 4 | ▶ | $plain =~ s|<style.+?>.+?</style>||gsim if index($cv, '</style>') > -1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | s|<a\s+href\s*=\s*(['"])(https?://[^'"]+?)\1[^>]*?>(.*?)</a>| [$3]($2) |gsim, | ||||||||||||||||||||||||||||||||||||||||||
| 88 | s|<a\s+href\s*=\s*["']mailto:([^\s]+?)["']>(.*?)</a>| [$2](mailto:$1) |gsim, | ||||||||||||||||||||||||||||||||||||||||||
| 89 | s/<[^<@>]+?>\s*/ /g, # Delete HTML tags except <neko@example.jp> | ||||||||||||||||||||||||||||||||||||||||||
| 90 | s/</</g, # Convert to left angle brackets | ||||||||||||||||||||||||||||||||||||||||||
| 91 | s/>/>/g, # Convert to right angle brackets | ||||||||||||||||||||||||||||||||||||||||||
| 92 | s/&/&/g, # Convert to "&" | ||||||||||||||||||||||||||||||||||||||||||
| 93 | s/"/"/g, # Convert to '"' | ||||||||||||||||||||||||||||||||||||||||||
| 94 | s/'/'/g, # Convert to "'" | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 4 | s/ / /g for $plain; | |||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 4 | ▶ | if( length($$argv1) > length($plain) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 3 | $plain =~ y/ //s; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 3 | $plain .= "\n" | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 16 | return \$plain; | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 67 | ▶ | sub to_utf8 { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_utf8: called CC=10 SCAR=23.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | # Convert given string to UTF-8 | ||||||||||||||||||||||||||||||||||||||||||
| 107 | # @param [String] argv1 String to be converted | ||||||||||||||||||||||||||||||||||||||||||
| 108 | # @param [String] argv2 Encoding name before converting | ||||||||||||||||||||||||||||||||||||||||||
| 109 | # @return [String] UTF-8 Encoded string | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 67 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 67 | ▶ | my $argv1 = shift || return ""; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || (return '')
MC/DC0%0/1
shift() || (return '')
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 67 | my $argv2 = shift; | |||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 67 | state $dontencode = ['utf8', 'utf-8', 'us-ascii', 'ascii']; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 67 | my $tobeutf8ed = $$argv1; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 67 | ▶ | my $encodefrom = lc $argv2 || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
lc $argv2 || ''
MC/DC0%0/1
lc $argv2 || ''
lc $argv2
Truth table50%1/2
A
A= lc $argv2
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 67 | my $hasencoded = 0; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 67 | my $hasguessed = Encode::Guess->guess($tobeutf8ed); | |||||||||||||||||||||||||||||||||||||||||
| 119 | 67 | ▶ | my $encodingto = ref $hasguessed ? lc($hasguessed->name) : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 67 | ▶ | if( $encodefrom ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | # The 2nd argument is a encoding name of the 1st argument | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 67 | while(1) { | |||||||||||||||||||||||||||||||||||||||||
| 124 | # Encode a given string when the encoding of the string is neigther | ||||||||||||||||||||||||||||||||||||||||||
| 125 | # utf8 nor ascii. | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 67 | ▶ | last if grep { $encodefrom eq $_ } @$dontencode; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 67 | ▶ | last if grep { $encodingto eq $_ } @$dontencode; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 51 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 130 | # Try to convert the string to UTF-8 | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 51 | Encode::from_to($tobeutf8ed, $encodefrom, 'utf8'); | |||||||||||||||||||||||||||||||||||||||||
| 132 | 51 | $hasencoded = 1; | |||||||||||||||||||||||||||||||||||||||||
| 133 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 51 | last; | |||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 67 | ▶ | return \$tobeutf8ed if $hasencoded; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 16 | ▶ | return \$tobeutf8ed unless $encodingto; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 16 | ▶ | return \$tobeutf8ed if grep { $encodingto eq $_ } @$dontencode; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | # a. The 2nd argument was not given or failed to convert from $encodefrom to UTF-8 | ||||||||||||||||||||||||||||||||||||||||||
| 142 | # b. Guessed encoding name is available, try to encode using it. | ||||||||||||||||||||||||||||||||||||||||||
| 143 | # c. Encode a given string when the encoding of the string is neigther utf8 nor ascii. | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | eval { Encode::from_to($tobeutf8ed, $encodingto, 'utf8') }; | |||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | return \$tobeutf8ed; | |||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 99 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 149 | __END__ | ||||||||||||||||||||||||||||||||||||||||||