| 1 | package MDK::Common::String; | ||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||
| 5 | MDK::Common::String - formatting functions | ||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||
| 9 | use MDK::Common::String qw(:all); | ||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||
| 11 | =head1 EXPORTS | ||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||
| 13 | =over | ||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||
| 15 | =item bestMatchSentence(STRING, LIST) | ||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||
| 17 | finds in the list the best corresponding string | ||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||
| 19 | =item formatList(INT, LIST) | ||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||
| 21 | if the list size is bigger than INT, replace the remaining elements with "...". | ||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||
| 23 | formatList(3, qw(a b c d e)) # => "a, b, c, ..." | ||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||
| 25 | =item formatError(STRING) | ||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||
| 27 | the string is something like "error at foo.pl line 2" that you get when | ||||||||||||||||||||||||
| 28 | catching an exception. formatError will remove the "at ..." so that you can | ||||||||||||||||||||||||
| 29 | nicely display the returned string to the user | ||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||
| 31 | =item formatTimeRaw(TIME) | ||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||
| 33 | the TIME is an epoch as returned by C<time>, the formatted time looks like "23:59:00" | ||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||
| 35 | =item formatLines(STRING) | ||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||
| 37 | remove "\n"s when the next line doesn't start with a space. Otherwise keep | ||||||||||||||||||||||||
| 38 | "\n"s to keep the indentation. | ||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||
| 40 | =item formatAlaTeX(STRING) | ||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||
| 42 | handle carriage return just like LaTeX: merge lines that are not separated by | ||||||||||||||||||||||||
| 43 | an empty line | ||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||
| 45 | =item begins_with(STRING, STRING) | ||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||
| 47 | return true if first argument begins with the second argument. Use this | ||||||||||||||||||||||||
| 48 | instead of regexps if you don't want regexps. | ||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||
| 50 | begins_with("hello world", "hello") # => 1 | ||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||
| 52 | =item warp_text(STRING, INT) | ||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||
| 54 | return a list of lines which do not exceed INT characters | ||||||||||||||||||||||||
| 55 | (or a string in scalar context) | ||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||
| 57 | =item warp_text(STRING) | ||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||
| 59 | warp_text at a default width (80) | ||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||
| 61 | =back | ||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||
| 63 | =head1 SEE ALSO | ||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||
| 65 | L<MDK::Common> | ||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||
| 67 | =cut | ||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||
| 70 | 1 | ▶ | use Exporter; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 71 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||
| 72 | 1 | our @EXPORT_OK = qw(bestMatchSentence formatList formatError formatTimeRaw formatLines formatAlaTeX begins_with warp_text); | |||||||||||||||||||||||
| 73 | 1 | our %EXPORT_TAGS = (all => [ @EXPORT_OK ]); | |||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||
| 76 | # count the number of character that match | ||||||||||||||||||||||||
| 77 | 0 | ▶ | sub bestMatchSentence { | ||||||||||||||||||||||
|
Subroutine
bestMatchSentence: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||
| 79 | 0 | my $best = -1; | |||||||||||||||||||||||
| 80 | 0 | my $bestSentence; | |||||||||||||||||||||||
| 81 | 0 | my @s = split /\W+/, shift; | |||||||||||||||||||||||
| 82 | 0 | foreach (@_) { | |||||||||||||||||||||||
| 83 | 0 | my $count = 0; | |||||||||||||||||||||||
| 84 | 0 | foreach my $e (@s) { | |||||||||||||||||||||||
| 85 | 0 | ▶ | $count += length($e) if /^$e$/; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 86 | 0 | ▶ | $count += length($e) if /^$e$/i; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 87 | 0 | ▶ | $count += length($e) if /$e/; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 88 | 0 | ▶ | $count += length($e) if /$e/i; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||
| 90 | 0 | ▶ | $best = $count, $bestSentence = $_ if $count > $best; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||
| 92 | 0 | ▶ | wantarray() ? ($bestSentence, $best) : $bestSentence; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||
| 96 | 0 | ▶ | sub formatList { | ||||||||||||||||||||||
|
Subroutine
formatList: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||
| 97 | 0 | my $nb = shift; | |||||||||||||||||||||||
| 98 | 0 | ▶ | join(", ", @_ <= $nb ? @_ : (@_[0..$nb-1], '...')); | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||
| 100 | 0 | ▶ | sub formatError { | ||||||||||||||||||||||
|
Subroutine
formatError: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||
| 101 | 0 | my ($err) = @_; | |||||||||||||||||||||||
| 102 | 0 | ▶ | if (!$::testing) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 103 | 0 | $err =~ s/Uncaught exception from user code:\n\t//s; #- happens with "use diagnostics" | |||||||||||||||||||||||
| 104 | 0 | $err =~ s/ at \S+ line .*?\.$/./s; | |||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||
| 106 | 0 | $err; | |||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||
| 108 | 0 | ▶ | sub formatTimeRaw { | ||||||||||||||||||||||
|
Subroutine
formatTimeRaw: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 109 | 0 | my ($s, $m, $h) = gmtime($_[0]); | |||||||||||||||||||||||
| 110 | 0 | sprintf "%d:%02d:%02d", $h, $m, $s; | |||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||
| 112 | 0 | ▶ | sub formatLines { | ||||||||||||||||||||||
|
Subroutine
formatLines: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||
| 113 | 0 | my ($t, $tmp); | |||||||||||||||||||||||
| 114 | 0 | foreach (split "\n", $_[0]) { | |||||||||||||||||||||||
| 115 | 0 | ▶ | if (/^\s/) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 116 | 0 | $t .= "$tmp\n"; | |||||||||||||||||||||||
| 117 | 0 | $tmp = $_; | |||||||||||||||||||||||
| 118 | } else { | ||||||||||||||||||||||||
| 119 | 0 | ▶ | $tmp = ($tmp ? "$tmp " : ($t && "\n") . $tmp) . $_; | ||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$t && "\n"
MC/DC0%0/1
$t && "\n"
$t
Truth table0%0/2
A
A= $t
| |||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||
| 122 | 0 | "$t$tmp\n"; | |||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||
| 124 | 0 | ▶ | sub formatAlaTeX { | ||||||||||||||||||||||
|
Subroutine
formatAlaTeX: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||
| 125 | 0 | my ($t, $tmp) = ('', ''); | |||||||||||||||||||||||
| 126 | 0 | foreach (split "\n", $_[0]) { | |||||||||||||||||||||||
| 127 | 0 | ▶ | if (/^$/) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 128 | 0 | ▶ | $t .= ($t && "\n") . $tmp; | ||||||||||||||||||||||
|
Condition0%0/2
$t && "\n"
MC/DC0%0/1
$t && "\n"
$t
Truth table0%0/2
A
A= $t
| |||||||||||||||||||||||||
| 129 | 0 | $tmp = ''; | |||||||||||||||||||||||
| 130 | } else { | ||||||||||||||||||||||||
| 131 | 0 | ▶ | $tmp = ($tmp && "$tmp ") . (/^\s*(.*?)\s*$/)[0]; | ||||||||||||||||||||||
|
Condition0%0/2
$tmp && "$tmp "
MC/DC0%0/1
$tmp && "$tmp "
$tmp
Truth table0%0/2
A
A= $tmp
| |||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||
| 134 | 0 | ▶ | $t . ($t && $tmp && "\n") . $tmp; | ||||||||||||||||||||||
|
Condition0%0/5
$t && $tmp && "\n"
$t and $tmp
MC/DC0%0/2
$t && $tmp && "\n"
$t
$tmp
Truth table0%0/3
A and B
A= $tB= $tmp
| |||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||
| 138 | 0 | ▶ | sub begins_with { | ||||||||||||||||||||||
|
Subroutine
begins_with: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 139 | 0 | my ($s, $prefix) = @_; | |||||||||||||||||||||||
| 140 | 0 | index($s, $prefix) == 0; | |||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||
| 143 | 0 | ▶ | sub warp_text { | ||||||||||||||||||||||
|
Subroutine
warp_text: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||
| 144 | 0 | my ($text, $o_width) = @_; | |||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||
| 146 | 0 | my @l; | |||||||||||||||||||||||
| 147 | 0 | foreach (split "\n", $text) { | |||||||||||||||||||||||
| 148 | 0 | my ($beg) = /^(\s*)/; | |||||||||||||||||||||||
| 149 | 0 | my $t = ''; | |||||||||||||||||||||||
| 150 | 0 | foreach (split /\s+/, $_) { | |||||||||||||||||||||||
| 151 | 0 | ▶ | if (length "$beg$t $_" > ($o_width || 80)) { | ||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$o_width || 80
MC/DC0%0/1
$o_width || 80
$o_width
Truth table0%0/2
A
A= $o_width
| |||||||||||||||||||||||||
| 152 | 0 | push @l, "$beg$t"; | |||||||||||||||||||||||
| 153 | 0 | $beg = ''; | |||||||||||||||||||||||
| 154 | 0 | $t = $_; | |||||||||||||||||||||||
| 155 | } else { | ||||||||||||||||||||||||
| 156 | 0 | ▶ | $t = $t ? "$t $_" : $_; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||
| 159 | 0 | push @l, "$beg$t"; | |||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||
| 161 | 0 | ▶ | wantarray() ? @l : join("\n", @l); | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||
| 164 | 1 | 1; | |||||||||||||||||||||||