blib/lib/Text/LineFold.pm

filter: Statementstmt 86.1% 192 / 223 Branchbran 68.1% 109 / 160 Conditioncond 48.2% 27 / 56 MC/DCmcdc 20.5% 7 / 34 Subroutinesub 100% 16 / 16 Podpod 100% 4 / 4 totaltotal 72.0% 355 / 493 CC 105 SCAR 37.5
CC
105
Cov
75%
unfold
74.7
fold
63.3
config
27.5
CRAP 42.5
« blib/lib/Unicode/LineBreak.pm ↑ Summary blib/lib/Unicode/GCString.pm »
1 #-*- perl -*-
2  
3 package Text::LineFold;
4 3 require 5.008;
5  
6 =encoding utf-8
7  
8 =head1 NAME
9  
10 Text::LineFold - Line Folding for Plain Text
11  
12 =head1 SYNOPSIS
13  
14 use Text::LineFold;
15 $lf = Text::LineFold->new();
16
17 # Fold lines
18 $folded = $lf->fold($string, 'PLAIN');
19 $indented = $lf->fold(' ' x 8, ' ' x 4, $string);
20  
21 # Unfold lines
22 $unfolded = $lf->unfold($string, 'FIXED');
23  
24 =head1 DESCRIPTION
25  
26 Text::LineFold folds or unfolds lines of plain text.
27 As it mainly focuses on plain text e-mail messages,
28 RFC 3676 flowed format is also supported.
29  
30 =cut
31  
32 ### Pragmas:
33 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 3 ▶use vars qw($VERSION @EXPORT_OK @ISA $Config);
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 ### Exporting:
37 3 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 ### Inheritance:
40 3 our @ISA = qw(Exporter Unicode::LineBreak);
41  
42 ### Other modules:
43 3 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 3 ▶use Encode qw(is_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 3 ▶use MIME::Charset;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 3 ▶use Unicode::LineBreak qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 ### Globals
49  
50 ### The package Version
51 3 our $VERSION = '2016.00702';
52  
53 ### Public Configuration Attributes
54 3 our $Config = {
55 ### %{$Unicode::LineBreak::Config},
56 Charset => 'UTF-8',
57 Language => 'XX',
58 OutputCharset => undef,
59 TabSize => 8,
60 };
61  
62 ### Privates
63  
64 my %FORMAT_FUNCS = (
65 'FIXED' => sub {
66 144 ▶ my $self = shift;
Subroutine __ANON__: called CC=8 SCAR=20.8
67 144 my $action = shift;
68 144 my $str = shift;
69 144 ▶ if ($action =~ /^so[tp]/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($action =~ /^so[tp]/) { } 30 114 144
elsif ($action eq '') { } 48 66 114
elsif ($action eq 'eol') { } 18 48 66
elsif ($action =~ /^eo/) { } 30 18 48
70 30 $self->{_} = {};
71 30 $self->{_}->{'ColMax'} = $self->config('ColMax');
72 30 ▶ $self->config('ColMax' => 0) if $str =~ /^>/;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^>/ 9 21 30
73 } elsif ($action eq "") {
74 48 $self->{_}->{line} = $str;
75 } elsif ($action eq "eol") {
76 18 return $self->config('Newline');
77 } elsif ($action =~ /^eo/) {
78 30 ▶ if (length $self->{_}->{line} and $self->config('ColMax')) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{'_'}{'line'} and $self->config('ColMax')) { } 15 15 30
Condition100%3/3
length $self->{"_"}{"line"} and $self->config("ColMax")
!ll&&!rl&&r
6915
MC/DC100%2/2
length $self->{"_"}{"line"} and $self->config("ColMax")
length $self->{"_"}{"line"} $self->config("ColMax")
Truth table100%3/3
A and B
A= length $self->{"_"}{"line"}B= $self->config("ColMax")
ABresult
0X0
100
111
79 15 $str = $self->config('Newline').$self->config('Newline');
80 } else {
81 15 $str = $self->config('Newline');
82 }
83 30 $self->config('ColMax' => $self->{_}->{'ColMax'});
84 30 delete $self->{_};
85 30 return $str;
86 }
87 96 undef;
88 },
89 'FLOWED' => sub { # RFC 3676
90 162 ▶ my $self = shift;
Subroutine __ANON__: called CC=14 SCAR=26.4
91 162 my $action = shift;
92 162 my $str = shift;
93 162 ▶ if ($action eq 'sol') {
Branch: 5 branches
BranchTrueFalseTotal
if ($action eq 'sol') { } 22 140 162
elsif ($action =~ /^so/) { } 32 108 140
elsif ($action eq '') { } 54 54 108
elsif ($action eq 'eol') { } 22 32 54
elsif ($action =~ /^eo/) { } 32 0 32
94 22 ▶ if ($self->{_}->{prefix}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_"}{"prefix"}) 2 20 22
95 2 return $self->{_}->{prefix}.' '.$str;
96 }
97 } elsif ($action =~ /^so/) {
98 32 $self->{_} = {};
99 32 ▶ if ($str =~ /^(>+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^(>+)/) { } 9 23 32
100 9 $self->{_}->{prefix} = $1;
101 } else {
102 23 $self->{_}->{prefix} = '';
103 }
104 } elsif ($action eq "") {
105 54 ▶ if ($str =~ /^(?: |From )/
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^(?: |From )/ or $str =~ /^>/ and not length $self->{"_"}{"prefix"}) 2 52 54
Condition66%4/6
$str =~ /^(?: |From )/ or $str =~ /^>/ and not length $self->{"_"}{"prefix"}
l!l&&r!l&&!r
2052
$str =~ /^>/ and not length $self->{"_"}{"prefix"}
!ll&&!rl&&r
41110
MC/DC33%1/3
$str =~ /^(?: |From )/ or $str =~ /^>/ and not length $self->{"_"}{"prefix"}
$str =~ /^(?: |From )/ $str =~ /^>/ not length $self->{"_"}{"prefix"}
Truth table50%2/4
A or B and not(C)
A= $str =~ /^(?: |From )/B= $str =~ /^>/C= not length $self->{"_"}{"prefix"}
ABCresult
1XX1
0111
00X0
0100
106 or $str =~ /^>/ and !length $self->{_}->{prefix}) {
107 2 return $self->{_}->{line} = ' ' . $str;
108 }
109 52 $self->{_}->{line} = $str;
110 } elsif ($action eq 'eol') {
111 22 ▶ $str = ' ' if length $str;
Branch: 1 branch
BranchTrueFalseTotal
if length $str 14 8 22
112 22 return $str.' '.$self->config('Newline');
113 } elsif ($action =~ /^eo/) {
114 32 ▶ if (length $self->{_}->{line} and !length $self->{_}->{prefix}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{'_'}{'line'} and not length $self->{'_'}{'prefix'}) { } 17 15 32
Condition100%3/3
length $self->{"_"}{"line"} and not length $self->{"_"}{"prefix"}
!ll&&!rl&&r
6917
MC/DC100%2/2
length $self->{"_"}{"line"} and not length $self->{"_"}{"prefix"}
length $self->{"_"}{"line"} not length $self->{"_"}{"prefix"}
Truth table100%3/3
A and not(B)
A= length $self->{"_"}{"line"}B= not length $self->{"_"}{"prefix"}
ABresult
0X0
100
111
115 17 $str = ' '.$self->config('Newline').$self->config('Newline');
116 } else {
117 15 $str = $self->config('Newline');
118 }
119 32 delete $self->{_};
120 32 return $str;
121 }
122 104 undef;
123 },
124 'PLAIN' => sub {
125 150 ▶ return $_[0]->config('Newline') if $_[1] =~ /^eo/;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] =~ /^eo/ 50 100 150
126 100 undef;
127 },
128 3 );
129  
130 =head2 Public Interface
131  
132 =over 4
133  
134 =item new ([KEY => VALUE, ...])
135  
136 I<Constructor>.
137 About KEY => VALUE pairs see config method.
138  
139 =back
140  
141 =cut
142  
143 sub new {
144 7 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
145 7 my $self = bless __PACKAGE__->SUPER::new(), $class;
146 7 $self->config(@_);
147 7 $self;
148 }
149  
150 =over 4
151  
152 =item $self->config (KEY)
153  
154 =item $self->config ([KEY => VAL, ...])
155  
156 I<Instance method>.
157 Get or update configuration. Following KEY => VALUE pairs may be specified.
158  
159 =over 4
160  
161 =item Charset => CHARSET
162  
163 Character set that is used to encode string.
164 It may be string or L<MIME::Charset> object.
165 Default is C<"UTF-8">.
166  
167 =item Language => LANGUAGE
168  
169 Along with Charset option, this may be used to define language/region
170 context.
171 Default is C<"XX">.
172 See also L<Unicode::LineBreak/Context> option.
173  
174 =item Newline => STRING
175  
176 String to be used for newline sequence.
177 Default is C<"\n">.
178  
179 =item OutputCharset => CHARSET
180  
181 Character set that is used to encode result of fold()/unfold().
182 It may be string or L<MIME::Charset> object.
183 If a special value C<"_UNICODE_"> is specified, result will be Unicode string.
184 Default is the value of Charset option.
185  
186 =item TabSize => NUMBER
187  
188 Column width of tab stops.
189 When 0 is specified, tab stops are ignored.
190 Default is 8.
191  
192 =item BreakIndent
193  
194 =item CharMax
195  
196 =item ColMax
197  
198 =item ColMin
199  
200 =item ComplexBreaking
201  
202 =item EAWidth
203  
204 =item HangulAsAL
205  
206 =item LBClass
207  
208 =item LegacyCM
209  
210 =item Prep
211  
212 =item Urgent
213  
214 See L<Unicode::LineBreak/Options>.
215  
216 =back
217  
218 =back
219  
220 =cut
221  
222 sub config {
223 85 ▶ my $self = shift;
Subroutine config: called CC=18 SCAR=33.1
Pod documented
224 85 my @opts = qw{Charset Language OutputCharset TabSize};
225 85 my %opts = map { (uc $_ => $_) } @opts;
226 85 my $newline = undef;
227  
228 # Get config.
229 85 ▶ if (scalar @_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) 78 7 85
230 78 ▶ if ($opts{uc $_[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{uc $_[0]}) 0 78 78
231 0 return $self->{$opts{uc $_[0]}};
232 }
233 78 return $self->SUPER::config($_[0]);
234 }
235  
236 # Set config.
237 7 my @o = ();
238 7 my %params = @_;
239 7 foreach my $k (keys %params) {
240 1 my $v = $params{$k};
241 1 ▶ if ($opts{uc $k}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts{uc $k}) { } 0 1 1
elsif (uc $k eq 'NEWLINE') { } 0 1 1
242 0 $self->{$opts{uc $k}} = $v;
243 } elsif (uc $k eq uc 'Newline') {
244 0 $newline = $v;
245 } else {
246 1 push @o, $k => $v;
247 }
248 }
249 7 ▶ $self->SUPER::config(@o) if scalar @o;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @o 1 6 7
250  
251 # Character set and language assumed.
252 7 ▶ if (ref $self->{Charset} eq 'MIME::Charset') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'Charset'} eq 'MIME::Charset') { } 0 7 7
253 0 $self->{_charset} = $self->{Charset};
254 } else {
255 7 ▶ $self->{Charset} ||= $Config->{Charset};
Condition33%1/3
$self->{"Charset"} ||= $Text::LineFold::Config->{"Charset"}
l!l&&r!l&&!r
070
MC/DC0%0/2
$self->{"Charset"} ||= $Text::LineFold::Config->{"Charset"}
$self->{"Charset"} $Text::LineFold::Config->{"Charset"}
Truth table33%1/3
A ||= B
A= $self->{"Charset"}B= $Text::LineFold::Config->{"Charset"}
ABresult
1X1
011
000
256 7 $self->{_charset} = MIME::Charset->new($self->{Charset});
257 }
258 7 $self->{Charset} = $self->{_charset}->as_string;
259 7 ▶ my $ocharset = uc($self->{OutputCharset} || $self->{Charset});
Condition33%1/3
$self->{'OutputCharset'} || $self->{'Charset'}
l!l&&r!l&&!r
070
MC/DC0%0/2
$self->{'OutputCharset'} || $self->{'Charset'}
$self->{'OutputCharset'} $self->{'Charset'}
Truth table33%1/3
A || B
A= $self->{'OutputCharset'}B= $self->{'Charset'}
ABresult
1X1
011
000
260 7 ▶ $ocharset = MIME::Charset->new($ocharset)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ocharset eq "MIME::Charset" or $ocharset eq "_UNICODE_" 7 0 7
Condition33%1/3
ref $ocharset eq "MIME::Charset" or $ocharset eq "_UNICODE_"
l!l&&r!l&&!r
007
MC/DC0%0/2
ref $ocharset eq "MIME::Charset" or $ocharset eq "_UNICODE_"
ref $ocharset eq "MIME::Charset" $ocharset eq "_UNICODE_"
Truth table33%1/3
A or B
A= ref $ocharset eq "MIME::Charset"B= $ocharset eq "_UNICODE_"
ABresult
1X1
011
000
261 unless ref $ocharset eq 'MIME::Charset' or $ocharset eq '_UNICODE_';
262 7 ▶ unless ($ocharset eq '_UNICODE_') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ocharset eq "_UNICODE_") 7 0 7
263 7 $self->{_charset}->encoder($ocharset);
264 7 $self->{OutputCharset} = $ocharset->as_string;
265 }
266 7 ▶ $self->{Language} = uc($self->{Language} || $Config->{Language});
Condition33%1/3
$self->{'Language'} || $Text::LineFold::Config->{'Language'}
l!l&&r!l&&!r
070
MC/DC0%0/2
$self->{'Language'} || $Text::LineFold::Config->{'Language'}
$self->{'Language'} $Text::LineFold::Config->{'Language'}
Truth table33%1/3
A || B
A= $self->{'Language'}B= $Text::LineFold::Config->{'Language'}
ABresult
1X1
011
000
267  
268 ## Context
269 $self->SUPER::config(Context =>
270 context(Charset => $self->{Charset},
271 7 Language => $self->{Language}));
272  
273 ## Set sizing method.
274 $self->SUPER::config(Sizing => sub {
275 2320 ▶ my ($self, $cols, $pre, $spc, $str) = @_;
Subroutine __ANON__: called CC=5 SCAR=16.1
276  
277 2320 my $tabsize = $self->{TabSize};
278 2320 my $spcstr = $spc.$str;
279 2320 $spcstr->pos(0);
280 2320 ▶ while (!$spcstr->eos and $spcstr->item->lbc == LB_SP) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $spcstr->eos and $spcstr->item->lbc == LB_SP()) 1014 2320 3334
Condition100%3/3
not $spcstr->eos and $spcstr->item->lbc == LB_SP()
!ll&&!rl&&r
3322871014
MC/DC100%2/2
not $spcstr->eos and $spcstr->item->lbc == LB_SP()
not $spcstr->eos $spcstr->item->lbc == LB_SP()
Truth table100%3/3
not(A) and B
A= not $spcstr->eosB= $spcstr->item->lbc == LB_SP()
ABresult
0X0
100
111
281 1014 my $c = $spcstr->next;
282 1014 ▶ if ($c eq "\t") {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\t") { } 10 1004 1014
283 10 ▶ $cols += $tabsize - $cols % $tabsize if $tabsize;
Branch: 1 branch
BranchTrueFalseTotal
if $tabsize 10 0 10
284 } else {
285 1004 $cols += $c->columns;
286 }
287 }
288 2320 return $cols + $spcstr->substr($spcstr->pos)->columns;
289 7 });
290  
291 ## Classify horizontal tab as line breaking class SP.
292 7 $self->SUPER::config(LBClass => [ord("\t") => LB_SP]);
293 ## Tab size
294 7 ▶ if (defined $self->{TabSize}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'TabSize'}) { } 0 7 7
295 0 ▶ croak "Invalid TabSize option" unless $self->{TabSize} =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"TabSize"} =~ /^\d+$/ 0 0 0
296 0 $self->{TabSize} += 0;
297 } else {
298 7 $self->{TabSize} = $Config->{TabSize};
299 }
300  
301 ## Newline
302 7 ▶ if (defined $newline) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newline) 0 7 7
303 0 ▶ $newline = $self->{_charset}->decode($newline)
Branch: 1 branch
BranchTrueFalseTotal
unless &is_utf8($newline) 0 0 0
304 unless is_utf8($newline);
305 0 $self->SUPER::config(Newline => $newline);
306 }
307 }
308  
309 =over 4
310  
311 =item $self->fold (STRING, [METHOD])
312  
313 =item $self->fold (INITIAL_TAB, SUBSEQUENT_TAB, STRING, ...)
314  
315 I<Instance method>.
316 fold() folds lines of string STRING and returns it.
317 Surplus SPACEs and horizontal tabs at end of line are removed,
318 newline sequences are replaced by that specified by Newline option
319 and newline is appended at end of text if it does not exist.
320 Horizontal tabs are treated as tab stops according to TabSize option.
321  
322 By the first style, following options may be specified for METHOD argument.
323  
324 =over 4
325  
326 =item C<"FIXED">
327  
328 Lines preceded by C<"E<gt>"> won't be folded.
329 Paragraphs are separated by empty line.
330  
331 =item C<"FLOWED">
332  
333 C<"Format=Flowed; DelSp=Yes"> formatting defined by RFC 3676.
334  
335 =item C<"PLAIN">
336  
337 Default method. All lines are folded.
338  
339 =back
340  
341 Second style is similar to L<Text::Wrap/wrap()>.
342 All lines are folded.
343 INITIAL_TAB is inserted at beginning of paragraphs and SUBSEQUENT_TAB
344 at beginning of other broken lines.
345  
346 =back
347  
348 =cut
349  
350 # Special breaking characters: VT, FF, NEL, LS, PS
351 3 my $special_break = qr/([\x{000B}\x{000C}\x{0085}\x{2028}\x{2029}])/os;
352  
353 sub fold {
354 12 ▶ my $self = shift;
Subroutine fold: called CC=23 SCAR=41.5
Pod documented
355 12 my $str;
356  
357 12 ▶ if (2 < scalar @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (2 < scalar @_) { } 2 10 12
358 2 ▶ my $initial_tab = shift || '';
Condition50%1/2
shift() || ''
l!l
20
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
359 2 ▶ $initial_tab = $self->{_charset}->decode($initial_tab)
Branch: 1 branch
BranchTrueFalseTotal
unless &is_utf8($initial_tab) 2 0 2
360 unless is_utf8($initial_tab);
361 2 ▶ my $subsequent_tab = shift || '';
Condition50%1/2
shift() || ''
l!l
20
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
362 2 ▶ $subsequent_tab = $self->{_charset}->decode($subsequent_tab)
Branch: 1 branch
BranchTrueFalseTotal
unless &is_utf8($subsequent_tab) 2 0 2
363 unless is_utf8($subsequent_tab);
364 2 my @str = @_;
365  
366 ## Decode and concat strings.
367 2 $str = shift @str;
368 2 ▶ $str = $self->{_charset}->decode($str) unless is_utf8($str);
Branch: 1 branch
BranchTrueFalseTotal
unless &is_utf8($str) 2 0 2
369 2 foreach my $s (@str) {
370 0 ▶ next unless defined $s and length $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and length $s 0 0 0
Condition0%0/3
defined $s and length $s
!ll&&!rl&&r
000
MC/DC0%0/2
defined $s and length $s
defined $s length $s
Truth table0%0/3
A and B
A= defined $sB= length $s
ABresult
0X0
100
111
371  
372 0 ▶ $s = $self->{_charset}->decode($s) unless is_utf8($s);
Branch: 1 branch
BranchTrueFalseTotal
unless &is_utf8($s) 0 0 0
373 0 ▶ unless (length $str) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $str) { } 0 0 0
if (do { $str =~ /(\s|$special_break)$/ or $s =~ /^(\s|$special_break)/ }) { } 0 0 0
374 0 $str = $s;
375 ▶ } elsif ($str =~ /(\s|$special_break)$/ or
Condition0%0/3
$str =~ /(\s|$special_break)$/ or $s =~ /^(\s|$special_break)/
l!l&&r!l&&!r
000
MC/DC0%0/2
$str =~ /(\s|$special_break)$/ or $s =~ /^(\s|$special_break)/
$str =~ /(\s|$special_break)$/ $s =~ /^(\s|$special_break)/
Truth table0%0/3
A or B
A= $str =~ /(\s|$special_break)$/B= $s =~ /^(\s|$special_break)/
ABresult
1X1
011
000
376 $s =~ /^(\s|$special_break)/) {
377 0 $str .= $s;
378 } else {
379 0 ▶ $str .= ' ' if $self->breakingRule($str, $s) == INDIRECT;
Branch: 1 branch
BranchTrueFalseTotal
if $self->breakingRule($str, $s) == 2 0 0 0
380 0 $str .= $s;
381 }
382 }
383  
384 ## Set format method.
385 $self->SUPER::config(Format => sub {
386 96 ▶ my $self = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
387 96 my $event = shift;
388 96 my $str = shift;
389 96 ▶ if ($event =~ /^eo/) { return $self->config('Newline'); }
Branch: 1 branch
BranchTrueFalseTotal
if ($event =~ /^eo/) 32 64 96
390 64 ▶ if ($event =~ /^so[tp]/) { return $initial_tab.$str; }
Branch: 1 branch
BranchTrueFalseTotal
if ($event =~ /^so[tp]/) 14 50 64
391 50 ▶ if ($event eq 'sol') { return $subsequent_tab.$str; }
Branch: 1 branch
BranchTrueFalseTotal
if ($event eq "sol") 18 32 50
392 32 undef;
393 2 });
394 } else {
395 10 $str = shift;
396 10 ▶ my $method = uc(shift || '');
Condition50%1/2
shift() || ''
l!l
100
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
397 10 ▶ return '' unless defined $str and length $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and length $str 0 10 10
Condition33%1/3
defined $str and length $str
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $str and length $str
defined $str length $str
Truth table33%1/3
A and B
A= defined $strB= length $str
ABresult
0X0
100
111
398  
399 ## Decode string.
400 10 ▶ $str = $self->{_charset}->decode($str) unless is_utf8($str);
Branch: 1 branch
BranchTrueFalseTotal
unless &is_utf8($str) 10 0 10
401  
402 ## Set format method.
403 $self->SUPER::config(Format => $FORMAT_FUNCS{$method} ||
404 10 ▶ $FORMAT_FUNCS{'PLAIN'});
Condition33%1/3
$FORMAT_FUNCS{$method} || $FORMAT_FUNCS{'PLAIN'}
l!l&&r!l&&!r
1000
MC/DC0%0/2
$FORMAT_FUNCS{$method} || $FORMAT_FUNCS{'PLAIN'}
$FORMAT_FUNCS{$method} $FORMAT_FUNCS{'PLAIN'}
Truth table33%1/3
A || B
A= $FORMAT_FUNCS{$method}B= $FORMAT_FUNCS{'PLAIN'}
ABresult
1X1
011
000
405 }
406  
407 ## Do folding.
408 12 my $result = '';
409 12 foreach my $s (split $special_break, $str) {
410 12 ▶ if ($s =~ $special_break) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /$special_break/s) { } 0 12 12
411 0 $result .= $s;
412 } else {
413 12 $result .= $self->break($str);
414 }
415 }
416  
417 ## Encode result.
418 12 ▶ if ($self->{OutputCharset} eq '_UNICODE_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'OutputCharset'} eq '_UNICODE_') { } 0 12 12
419 0 return $result;
420 } else {
421 12 return $self->{_charset}->encode($result);
422 }
423 }
424  
425 =over 4
426  
427 =item $self->unfold (STRING, METHOD)
428  
429 Conjunct folded paragraphs of string STRING and returns it.
430  
431 Following options may be specified for METHOD argument.
432  
433 =over 4
434  
435 =item C<"FIXED">
436  
437 Default method.
438 Lines preceded by C<"E<gt>"> won't be conjuncted.
439 Treat empty line as paragraph separator.
440  
441 =item C<"FLOWED">
442  
443 Unfold C<"Format=Flowed; DelSp=Yes"> formatting defined by RFC 3676.
444  
445 =item C<"FLOWEDSP">
446  
447 Unfold C<"Format=Flowed; DelSp=No"> formatting defined by RFC 3676.
448  
449 =begin comment
450  
451 =item C<"OBSFLOWED">
452  
453 Unfold C<"Format=Flowed> formatting defined by (obsoleted) RFC 2646
454 as well as possible.
455  
456 =end comment
457  
458 =back
459  
460 =back
461  
462 =cut
463  
464 sub unfold {
465 8 ▶ my $self = shift;
Subroutine unfold: called CC=38 SCAR=43.1
Pod documented
466 8 my $str = shift;
467 8 ▶ return '' unless defined $str and length $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and length $str 0 8 8
Condition33%1/3
defined $str and length $str
!ll&&!rl&&r
008
MC/DC0%0/2
defined $str and length $str
defined $str length $str
Truth table33%1/3
A and B
A= defined $strB= length $str
ABresult
0X0
100
111
468  
469 ## Get format method.
470 8 ▶ my $method = uc(shift || 'FIXED');
Condition50%1/2
shift() || 'FIXED'
l!l
80
MC/DC0%0/1
shift() || 'FIXED'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
471 8 ▶ $method = 'FIXED' unless $method =~ /^(?:FIXED|FLOWED|FLOWEDSP|OBSFLOWED)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $method =~ /^(?:FIXED|FLOWED|FLOWEDSP|OBSFLOWED)$/ 0 8 8
472 8 my $delsp = $method eq 'FLOWED';
473  
474 ## Decode string and canonizalize newline.
475 8 ▶ $str = $self->{_charset}->decode($str) unless is_utf8($str);
Branch: 1 branch
BranchTrueFalseTotal
unless &is_utf8($str) 8 0 8
476 8 $str =~ s/\r\n|\r/\n/g;
477  
478 ## Do unfolding.
479 8 my $result = '';
480 8 foreach my $s (split $special_break, $str) {
481 8 ▶ if ($s eq '') {
Branch: 4 branches
BranchTrueFalseTotal
if ($s eq '') { } 0 8 8
elsif ($s =~ /$special_break/s) { } 0 8 8
elsif ($method eq 'FIXED') { } 3 5 8
elsif ($method eq 'FLOWED' or $method eq 'FLOWEDSP' or $method eq 'OBSFLOWED') { } 5 0 5
482 0 next;
483 } elsif ($s =~ $special_break) {
484 0 $result .= $s;
485 0 next;
486 } elsif ($method eq 'FIXED') {
487 3 pos($s) = 0;
488 3 ▶ while ($s !~ /\G\z/cg) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($s =~ /\G\z/cg) 48 3 51
489 48 ▶ if ($s =~ /\G\n/cg) {
Branch: 7 branches
BranchTrueFalseTotal
if ($s =~ /\G\n/cg) { } 6 42 48
elsif ($s =~ /\G(.+)\n\n/cg) { } 15 27 42
elsif ($s =~ /\G(>.*)\n/cg) { } 9 18 27
elsif ($s =~ /\G(.+)\n(?=>)/cg) { } 0 18 18
elsif ($s =~ /\G(.+?)( *)\n(?=(.+))/cg) { } 18 0 18
elsif ($s =~ /\G(.+)\n/cg) { } 0 0 0
elsif ($s =~ /\G(.+)/cg) { } 0 0 0
490 6 $result .= $self->config('Newline');
491 } elsif ($s =~ /\G(.+)\n\n/cg) {
492 15 $result .= $1.$self->config('Newline');
493 } elsif ($s =~ /\G(>.*)\n/cg) {
494 9 $result .= $1.$self->config('Newline');
495 } elsif ($s =~ /\G(.+)\n(?=>)/cg) {
496 0 $result .= $1.$self->config('Newline');
497 } elsif ($s =~ /\G(.+?)( *)\n(?=(.+))/cg) {
498 18 my ($l, $s, $n) = ($1, $2, $3);
499 18 $result .= $l;
500 18 ▶ if ($n =~ /^ /) {
Branch: 3 branches
BranchTrueFalseTotal
if ($n =~ /^ /) { } 0 18 18
elsif (length $s) { } 0 18 18
elsif (length $l) { } 18 0 18
501 0 $result .= $self->config('Newline');
502 } elsif (length $s) {
503 0 $result .= $s;
504 } elsif (length $l) {
505 18 ▶ $result .= ' '
Branch: 1 branch
BranchTrueFalseTotal
if $self->breakingRule($l, $n) == 2 11 7 18
506 if $self->breakingRule($l, $n) == INDIRECT;
507 }
508 } elsif ($s =~ /\G(.+)\n/cg) {
509 0 $result .= $1.$self->config('Newline');
510 } elsif ($s =~ /\G(.+)/cg) {
511 0 $result .= $1.$self->config('Newline');
512 0 last;
513 }
514 }
515 ▶ } elsif ($method eq 'FLOWED' or $method eq 'FLOWEDSP' or
Condition50%3/6
$method eq "FLOWED" or $method eq "FLOWEDSP" or $method eq "OBSFLOWED"
l!l&&r!l&&!r
500
$method eq "FLOWED" or $method eq "FLOWEDSP"
l!l&&r!l&&!r
410
MC/DC0%0/3
$method eq "FLOWED" or $method eq "FLOWEDSP" or $method eq "OBSFLOWED"
$method eq "FLOWED" $method eq "FLOWEDSP" $method eq "OBSFLOWED"
Truth table50%2/4
A or B or C
A= $method eq "FLOWED"B= $method eq "FLOWEDSP"C= $method eq "OBSFLOWED"
ABCresult
1XX1
01X1
0011
0000
516 $method eq 'OBSFLOWED') {
517 5 my $prefix = undef;
518 5 pos($s) = 0;
519 5 ▶ while ($s !~ /\G\z/cg) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($s =~ /\G\z/cg) 96 5 101
520 96 ▶ if ($s =~ /\G(>+) ?(.*?)( ?)\n/cg) {
Branch: 3 branches
BranchTrueFalseTotal
if ($s =~ /\G(>+) ?(.*?)( ?)\n/cg) { } 26 70 96
elsif ($s =~ /\G ?(.*?)( ?)\n/cg) { } 70 0 70
elsif ($s =~ /\G ?(.*)/cg) { } 0 0 0
521 26 my ($p, $l, $s) = ($1, $2, $3);
522 26 ▶ unless (defined $prefix) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $prefix) { } 9 17 26
if (do { $p ne $prefix }) { } 1 8 9
523 17 $result .= $p.' '.$l;
524 } elsif ($p ne $prefix) {
525 1 $result .= $self->config('Newline');
526 1 $result .= $p.' '.$l;
527 } else {
528 8 $result .= $l;
529 }
530 26 ▶ unless (length $s) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $s) { } 9 17 26
531 17 $result .= $self->config('Newline');
532 17 $prefix = undef;
533 } else {
534 9 $prefix = $p;
535 9 ▶ $result .= $s unless $delsp;
Branch: 1 branch
BranchTrueFalseTotal
unless $delsp 7 2 9
536 }
537 } elsif ($s =~ /\G ?(.*?)( ?)\n/cg) {
538 70 my ($l, $s) = ($1, $2);
539 70 ▶ unless (defined $prefix) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $prefix) { } 40 30 70
if (do { '' ne $prefix }) { } 0 40 40
540 30 $result .= $l;
541 } elsif ('' ne $prefix) {
542 0 $result .= $self->config('Newline');
543 0 $result .= $l;
544 } else {
545 40 $result .= $l;
546 }
547 70 ▶ unless (length $s) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $s) { } 40 30 70
548 30 $result .= $self->config('Newline');
549 30 $prefix = undef;
550 } else {
551 40 ▶ $result .= $s unless $delsp;
Branch: 1 branch
BranchTrueFalseTotal
unless $delsp 3 37 40
552 40 $prefix = '';
553 }
554 } elsif ($s =~ /\G ?(.*)/cg) {
555 0 $result .= $1.$self->config('Newline');
556 0 last;
557 }
558 }
559 }
560 }
561 ## Encode result.
562 8 ▶ if ($self->{OutputCharset} eq '_UNICODE_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'OutputCharset'} eq '_UNICODE_') { } 0 8 8
563 0 return $result;
564 } else {
565 8 return $self->{_charset}->encode($result);
566 }
567 }
568  
569 =head1 BUGS
570  
571 Please report bugs or buggy behaviors to developer.
572  
573 CPAN Request Tracker:
574 L<http://rt.cpan.org/Public/Dist/Display.html?Name=Unicode-LineBreak>.
575  
576 =head1 VERSION
577  
578 Consult $VERSION variable.
579  
580 =head1 SEE ALSO
581  
582 L<Unicode::LineBreak>, L<Text::Wrap>.
583  
584 =head1 AUTHOR
585  
586 Copyright (C) 2009-2012 Hatuka*nezumi - IKEDA Soji <hatuka(at)nezumi.nu>.
587  
588 This program is free software; you can redistribute it and/or modify it
589 under the same terms as Perl itself.
590  
591 =cut
592  
593 3 1;
« blib/lib/Unicode/LineBreak.pm ↑ Summary blib/lib/Unicode/GCString.pm »