Quiq/String.pm

filter: Statementstmt 92.0% 150 / 163 Branchbran 86.3% 76 / 88 Conditioncond 72.4% 21 / 29 MC/DCmcdc 61.1% 11 / 18 Subroutinesub 95.2% 20 / 21 Podpod 100% 12 / 12 totaltotal 87.6% 290 / 331 CC 57 SCAR 21.5
CC
57
Cov
88%
autoDecode
20.0
removeIndentation
15.0
removeComments
12.0
CRAP 8.5
« Quiq/Html/List.pm ↑ Summary Quiq/Trash.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::String - Operationen auf Zeichenketten
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =cut
14  
15 # -----------------------------------------------------------------------------
16  
17 package Quiq::String;
18 270 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 270 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 270 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 270 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 270 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 270 our $VERSION = '1.240';
26  
27 270 ▶use Encode::Guess ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 270 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 270 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 270 ▶use Quiq::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 # -----------------------------------------------------------------------------
33  
34 =head1 METHODS
35  
36 =head2 Eigenschaften
37  
38 =head3 maxLineLength() - Länge der längsten Zeile
39  
40 =head4 Synopsis
41  
42 $len = $class->maxLineLength($text);
43  
44 =head4 Arguments
45  
46 =over 4
47  
48 =item $text
49  
50 Ein String, typischerweise mehrzeilig.
51  
52 =back
53  
54 =head4 Returns
55  
56 Länge der längsten Zeile (Integer)
57  
58 =head4 Description
59  
60 Ermittele die Länge der längsten Zeile und liefere diese zurück. Newline
61 wird nicht mitgezählt.
62  
63 =cut
64  
65 # -----------------------------------------------------------------------------
66  
67 1 ▶sub maxLineLength {
Subroutine maxLineLength: called CC=3 SCAR=11.0
Pod documented
68 1 my ($class,$text) = @_;
69  
70 1 my $maxLen = 0;
71 1 for (split /\n/,$text) {
72 5 chomp;
73 5 my $l = length;
74 5 ▶ if ($l > $maxLen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l > $maxLen) 2 3 5
75 2 $maxLen = $l;
76 }
77 }
78  
79 1 return $maxLen;
80 }
81  
82 # -----------------------------------------------------------------------------
83  
84 =head2 Encoding
85  
86 =head3 autoDecode() - Auto-dekodiere Zeichenkette
87  
88 =head4 Synopsis
89  
90 $str = $class->autoDecode($str);
91 $str = $class->autoDecode($str,$otherEncoding);
92  
93 =head4 Description
94  
95 Analysiere Zeichenkette $str hinsichtlich ihres Character-Encodings
96 und dekodiere sie entsprechend. Unterschieden werden:
97  
98 =over 2
99  
100 =item *
101  
102 ASCII
103  
104 =item *
105  
106 UTF-8
107  
108 =item *
109  
110 UTF-16/32 mit BOM
111  
112 =back
113  
114 und $otherEncoding. Ist $otherEncoding nicht angegeben, wird
115 ISO-8859-1 angenommen.
116  
117 =cut
118  
119 # -----------------------------------------------------------------------------
120  
121 0 ▶sub autoDecode {
Subroutine autoDecode: not called CC=4 SCAR=30.0
Pod documented
122 0 my $class = shift;
123 0 my $str = shift;
124 0 ▶ my $otherEncoding = shift || 'iso-8859-1';
Condition0%0/2
shift() || 'iso-8859-1'
l!l
00
MC/DC0%0/1
shift() || 'iso-8859-1'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
125  
126 # Encoding ermitteln und Text dekodieren
127  
128 # $Encode::Guess::NoUTFAutoGuess = 1;
129 0 my $dec = Encode::Guess->guess($str);
130 0 ▶ if (ref $dec) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $dec) { } 0 0 0
elsif ($dec =~ /No appropriate encodings found/i) { } 0 0 0
131 # Wir dekodieren Unicode
132 0 $str = $dec->decode($str);
133 }
134 elsif ($dec =~ /No appropriate encodings found/i) {
135 # Wir dekodieren $otherEncoding
136 0 $str = Encode::decode($otherEncoding,$str);
137 }
138 else {
139 # Unerwarteter Fehler
140 0 $class->throw(
141 'PATH-00099: Zeichen-Dekodierung fehlgeschlagen',
142 Message => $dec,
143 );
144 }
145  
146 0 return $str;
147 }
148  
149 # -----------------------------------------------------------------------------
150  
151 =head3 indent() - Rücke Text ein
152  
153 =head4 Synopsis
154  
155 $str2 = $class->indent($str,$indentStr,@opt);
156 $class->indent(\$str,$indentStr,@opt);
157  
158 =head4 Options
159  
160 =over 4
161  
162 =item -indentBlankLines => $bool (Default: 0)
163  
164 Rücke auch Leerzeilen ein. Per Default werden nur Zeilen mit
165 wenigstens einem Zeichen eingerückt.
166  
167 Diese Option ist nützlich, wenn die Funktion zum Auskommentieren
168 genutzt werden soll.
169  
170 =item -strip => $bool (Default: 0)
171  
172 Entferne Newlines am Anfang und Whitespace am Ende. Per Default
173 geschieht dies nicht.
174  
175 =back
176  
177 =head4 Description
178  
179 Rücke den Text $str um Zeichenkette $indentStr ein und liefere
180 das Resultat zurück.
181  
182 Die Einrück-Zeichenkette $indentStr wird jeder Zeile von $str
183 hinzugefügt, außer Leerzeilen.
184  
185 =head4 Example
186  
187 =over 2
188  
189 =item *
190  
191 Texteinrückung um vier Leerzeichen
192  
193 $class->indent($txt,' ' x 4);
194
195 |Dies ist | Dies ist
196 |ein Test- - wird zu -> | ein Test-
197 |Text. | Text.
198  
199 =back
200  
201 =cut
202  
203 # -----------------------------------------------------------------------------
204  
205 45 ▶sub indent {
Subroutine indent: called CC=7 SCAR=19.5
Pod documented
206 45 my $class = shift;
207 45 my $str = shift;
208 45 my $indentStr = shift;
209 # @_: @opt
210  
211 # Optionen
212  
213 45 my $indentBlankLines = 0;
214 45 my $strip = 0;
215 45 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 43 45
216 2 Quiq::Option->extract(\@_,
217 -indentBlankLines => \$indentBlankLines,
218 -strip => \$strip,
219 );
220 }
221  
222 # Verarbeiten
223  
224 45 ▶ my $ref = ref $str? $str: \$str;
Branch: 1 branch
BranchTrueFalseTotal
ref $str ? : 40 5 45
225  
226 45 ▶ if ($strip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strip) 1 44 45
227 1 $$ref =~ s/^\n+//; # Newlines am Anfang entfernen
228 1 $$ref =~ s/\s+$//; # Whitespace am Ende entfernen
229 }
230  
231 45 ▶ if (defined $indentStr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $indentStr) 43 2 45
232 43 ▶ if ($indentBlankLines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indentBlankLines) { } 1 42 43
233 # alle Zeilen einrücken
234 1 $$ref =~ s/^/$indentStr/mg;
235 }
236 else {
237 # Leerzeilen nicht einrücken
238 42 $$ref =~ s/^(.)/$indentStr$1/mg;
239 }
240 }
241  
242 45 ▶ return $str unless ref $str;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $str 5 40 45
243 }
244  
245 # -----------------------------------------------------------------------------
246  
247 =head3 determineIndentation() - Einrücktiefe eines Textes
248  
249 =head4 Synopsis
250  
251 $n = $class->determineIndentation($str);
252  
253 =head4 Description
254  
255 Ermittele die Einrücktiefe des Textes $str und liefere diese zurück.
256 Die Einrücktiefe ist der größte gemeinsame Teiler aller
257 Zeilen-Einrückungen. Beispiel:
258  
259 |Dies
260 | ist
261 | ein
262 | Test
263  
264 Einrücktiefe ist 4.
265  
266 WICHTIG: Für die Einrückung zählen nur Leerzeichen, keine Tabs!
267  
268 =cut
269  
270 # -----------------------------------------------------------------------------
271  
272 28 ▶sub determineIndentation {
Subroutine determineIndentation: called CC=2 SCAR=6.9
Pod documented
273 28 my ($class,$str) = @_;
274  
275 # Ermittele die verschiedenen Einrücktiefen der Zeilen
276  
277 28 my %ind;
278 28 ▶ while ($str =~ /^( *)/mg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^( *)/gm) 232 28 260
279 232 $ind{length $1}++;
280 }
281  
282 28 return Quiq::Array->gcd([keys %ind]);
283 }
284  
285 # -----------------------------------------------------------------------------
286  
287 =head3 reduceIndentation() - Reduziere Einrücktiefe eines Textes
288  
289 =head4 Synopsis
290  
291 $str = $class->reduceIndentation($n,$str);
292 $class->reduceIndentation($n,\$str);
293  
294 =head4 Description
295  
296 Reduziere die Einrücktiefe des Textes $str auf Tiefe $n.
297  
298 =head4 Example
299  
300 Text:
301  
302 |Dies
303 | ist
304 | ein
305 | Test
306  
307 Reduktion auf Einrücktiefe 2:
308  
309 Quiq::String->reduceIndentation(2,$str);
310  
311 Resultat:
312  
313 |Dies
314 | ist
315 | ein
316 | Test
317  
318 =cut
319  
320 # -----------------------------------------------------------------------------
321  
322 25 ▶sub reduceIndentation {
Subroutine reduceIndentation: called CC=7 SCAR=20.2
Pod documented
323 25 my ($class,$n,$arg) = @_;
324  
325 25 ▶ my $ref = ref $arg? $arg: \$arg;
Branch: 1 branch
BranchTrueFalseTotal
ref $arg ? : 22 3 25
326
327 25 my $m = $class->determineIndentation($$ref);
328 25 ▶ if ($m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m) 11 14 25
329 11 ▶ if ($m < $n || $m%$n) {
Branch: 2 branches
BranchTrueFalseTotal
if ($m < $n or $m % $n) { } 0 11 11
elsif ($m > $n) { } 11 0 11
Condition33%1/3
$m < $n or $m % $n
l!l&&r!l&&!r
0011
MC/DC0%0/2
$m < $n or $m % $n
$m < $n $m % $n
Truth table33%1/3
A or B
A= $m < $nB= $m % $n
ABresult
1X1
011
000
330 0 $class->throw(
331 'STRING-00001: Einrücktiefe kann nicht reduziert werden',
332 TextIndentation => $m,
333 WantedIndentation => $n,
334 );
335 }
336 elsif ($m > $n) {
337 11 my $div = $m/$n;
338 11 $$ref =~ s|^( +)|' ' x (length($1)/$div)|emg;
339 }
340 }
341
342 25 ▶ return ref $arg? (): $$ref;
Branch: 1 branch
BranchTrueFalseTotal
ref $arg ? : 22 3 25
343 }
344  
345 # -----------------------------------------------------------------------------
346  
347 =head3 removeIndentation() - Entferne Text-Einrückung
348  
349 =head4 Synopsis
350  
351 $str = $class->removeIndentation($str,@opt); # [1]
352 $class->removeIndentation(\$str,@opt); # [2]
353  
354 =head4 Options
355  
356 =over 4
357  
358 =item -addNL => $bool (Default: 0)
359  
360 Nach dem Entfernen aller NEWLINEs am Ende füge ein NEWLINE hinzu.
361  
362 =back
363  
364 =head4 Description
365  
366 [1] Entferne Text-Einrückung aus Zeichenkette $str und liefere das
367 Resultat zurück.
368  
369 [2] Wird eine Referenz auf $str übergeben, wird die
370 Zeichenkette "in place" manipuliert und nichts zurückgegeben.
371  
372 =over 2
373  
374 =item *
375  
376 NEWLINEs am Anfang werden entfernt.
377  
378 =item *
379  
380 Whitespace (SPACEs, TABs, NEWLINEs) am Ende wird entfernt.
381 Das Resultat endet also grundsätzlich nicht mit einem NEWLINE.
382  
383 =item *
384  
385 Die Methode kehrt zurück, wenn $str anschließend nicht mit wenigstens
386 einem Whitespace-Zeichen beginnt, denn dann existiert keine
387 Einrückung, die zu entfernen wäre.
388  
389 =item *
390  
391 Die Einrückung von $str ist die längste Folge von SPACEs
392 und TABs, die allen Zeilen von $str gemeinsam ist,
393 ausgenommen Leerzeilen. Diese Einrückung wird am Anfang
394 aller Zeilen von $str entfernt.
395  
396 =item *
397  
398 Eine Leerzeile ist eine Zeile, die nur aus Whitespace besteht.
399  
400 =back
401  
402 =head4 Example
403  
404 =over 2
405  
406 =item *
407  
408 Einrückung entfernen, Leerzeile übergehen:
409  
410 |
411 | Dies ist
412 | <- Leerzeile ohne Einrückung
413 | ein Test-
414 | Text.
415 |
416  
417 wird zu
418  
419 |Dies ist
420 |
421 |ein Test-
422 |Text.
423  
424 =item *
425  
426 Tiefere Einrückung bleibt bestehen:
427  
428 |
429 | Dies ist
430 | ein Test-
431 | Text.
432 |
433  
434 wird zu
435  
436 | Dies ist
437 |ein Test-
438 |Text.
439  
440 =back
441  
442 =cut
443  
444 # -----------------------------------------------------------------------------
445  
446 670 ▶sub removeIndentation {
Subroutine removeIndentation: called CC=14 SCAR=27.1
Pod documented
447 670 my $class = shift;
448 670 my $arg = shift;
449 # @_: @opt
450  
451 670 ▶ my $ref = ref $arg? $arg: \$arg;
Branch: 1 branch
BranchTrueFalseTotal
ref $arg ? : 494 176 670
452  
453 670 my $addNL = 0;
454  
455 670 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 670 670
456 0 Quiq::Option->extract(\@_,
457 -addNL => \$addNL,
458 );
459 }
460  
461 670 ▶ if (defined $$ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$ref) 667 3 670
462 667 $$ref =~ s/^\n+//;
463 667 $$ref =~ s/\s+$//;
464 667 ▶ if ($addNL && $$ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addNL and $$ref) 0 667 667
Condition33%1/3
$addNL and $$ref
!ll&&!rl&&r
66700
MC/DC0%0/2
$addNL and $$ref
$addNL $$ref
Truth table33%1/3
A and B
A= $addNLB= $$ref
ABresult
0X0
100
111
465 0 $$ref .= "\n";
466 }
467  
468 # Wir brauchen uns nur mit dem String befassen, wenn
469 # das erste Zeichen ein Whitespacezeichen ist. Wenn dies nicht
470 # der Fall ist, existiert keine Einrückung.
471  
472 667 ▶ if ($$ref =~ /^\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$ref =~ /^\s/) 49 618 667
473 49 my $ind;
474 49 ▶ while ($$ref =~ /^([ \t]*)(.?)/gm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$ref =~ /^([ \t]*)(.?)/gm) 262 49 311
475 262 ▶ next if length $2 == 0; # leere Zeile oder nur Whitespace
Branch: 1 branch
BranchTrueFalseTotal
if length $2 == 0 6 256 262
476 256 ▶ $ind = $1 if !defined $ind || length $1 < length $ind;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $ind or length $1 < length $ind 53 203 256
Condition100%3/3
not defined $ind or length $1 < length $ind
l!l&&r!l&&!r
494203
MC/DC100%2/2
not defined $ind or length $1 < length $ind
not defined $ind length $1 < length $ind
Truth table100%3/3
not(A) or B
A= not defined $indB= length $1 < length $ind
ABresult
1X1
011
000
477 256 ▶ last if !$ind;
Branch: 1 branch
BranchTrueFalseTotal
unless $ind 0 256 256
478 }
479 49 ▶ $$ref =~ s/^$ind//gm if $ind;
Branch: 1 branch
BranchTrueFalseTotal
if $ind 49 0 49
480 }
481 }
482  
483 670 ▶ return ref $arg? (): $$ref;
Branch: 1 branch
BranchTrueFalseTotal
ref $arg ? : 494 176 670
484 }
485  
486 # -----------------------------------------------------------------------------
487  
488 =head3 removeIndentationNl() - Entferne Text-Einrückung
489  
490 =head4 Synopsis
491  
492 $str = $class->removeIndentationNl($str,@opt); # [1]
493 $class->removeIndentationNl(\$str,@opt); # [2]
494  
495 =cut
496  
497 # -----------------------------------------------------------------------------
498  
499 9 ▶sub removeIndentationNl {
Subroutine removeIndentationNl: called CC=5 SCAR=16.1
Pod documented
500 9 my $class = shift;
501 9 my $arg = shift;
502 # @_: @opt
503  
504 9 ▶ my $ref = ref $arg? $arg: \$arg;
Branch: 1 branch
BranchTrueFalseTotal
ref $arg ? : 2 7 9
505  
506 9 $class->removeIndentation($ref);
507 9 ▶ if (defined($$ref) && $$ref ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$ref and $$ref ne "") 8 1 9
Condition66%2/3
defined $$ref and $$ref ne ""
!ll&&!rl&&r
108
MC/DC50%1/2
defined $$ref and $$ref ne ""
defined $$ref $$ref ne ""
Truth table66%2/3
A and B
A= defined $$refB= $$ref ne ""
ABresult
0X0
100
111
508 8 $$ref .= "\n";
509 }
510
511 9 ▶ return ref $arg? (): $$ref;
Branch: 1 branch
BranchTrueFalseTotal
ref $arg ? : 2 7 9
512 }
513  
514 # -----------------------------------------------------------------------------
515  
516 =head2 Kommentare
517  
518 =head3 removeComments() - Entferne Kommentare aus Quelltext
519  
520 =head4 Synopsis
521  
522 $newCode = $this->removeComments($code,$start);
523 $newCode = $this->removeComments($code,$start,$stop);
524  
525 =head4 Description
526  
527 Entferne alle Kommentare aus Quelltext $code und liefere das
528 Resultat zurück. Die Kommentarzeichen werden durch die Parameter
529 $start und $stop definiert. Siehe Abschnitt Examples.
530  
531 Die Methode entfernt nicht nur die Kommentare selbst, sondern
532 auch nachfolgenden oder vorausgehenden Whitespace, so dass
533 kein überflüssiger Leerraum entsteht.
534  
535 Im Falle von einzeiligen Kommentaren (d.h. nur $start ist
536 definiert), wird vorausgesetzt, dass dem Kommentarzeichen (der
537 Kommentar-Zeichenkette) im Quelltext zusätzlich ein Leerzeichen
538 oder Tab vorausgeht, sofern es nicht am Anfang der Zeile
539 steht. D.h.
540  
541 my $ind = ' ' x 4; # Einrückung
542 ^ ^
543 hier müssen Leerzeichen (oder Tabs) stehen
544
545 my $ind = ' ' x 4;# Einrückung
546 ^
547 Wird nicht erkannt!
548
549 my $ind = ' ' x 4; #Einrückung
550 ^
551 Wird nicht erkannt!
552  
553 Im Falle von mehrzeiligen Kommentaren ($start und $stop sind
554 definiert) ist dies das vorausgehende Leereichen nicht nötig.
555  
556 =head4 Examples
557  
558 HTML, XML:
559  
560 $code = Quiq::String->removeComments($code,'<!--','-->');
561  
562 C, Java, CSS:
563  
564 $code = Quiq::String->removeComments($code,'/*','*/');
565  
566 C++, JavaScript:
567  
568 $code = Quiq::String->removeComments($code,'//');
569  
570 Shell, Perl, Python, Ruby, ...:
571  
572 $code = Quiq::String->removeComments($code,'#');
573  
574 SQL:
575  
576 $code = Quiq::String->removeComments($code,'--');
577  
578 =cut
579  
580 # -----------------------------------------------------------------------------
581  
582 25 ▶sub removeComments {
Subroutine removeComments: called CC=12 SCAR=24.9
Pod documented
583 25 my ($this,$code,$start,$stop) = @_;
584  
585 25 my $regex;
586 25 ▶ if ($start && $stop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start and $stop) { } 8 17 25
Condition66%2/3
$start and $stop
!ll&&!rl&&r
0178
MC/DC50%1/2
$start and $stop
$start $stop
Truth table66%2/3
A and B
A= $startB= $stop
ABresult
0X0
100
111
587 # (potentiell) mehrzeiliger Kommentar
588 8 $regex = qr/\Q$start\E.*?\Q$stop\E/s;
589 }
590 else {
591 # einzeilger Kommentar
592 17 $regex = qr/(?:^|(?<=[\t ]))\Q$start\E .*/m;
593 }
594
595 # Spaces u. Tabs an Zeilenenden entfernen
596 25 $code =~ s|[\t ]+$||mg;
597  
598 # Entferne alle Kommentare aus dem Quelltext und speichere die
599 # übrigbleibenden Fragmente in einem Array
600 25 my @frag = split m|$regex|s,$code;
601
602 25 ▶ if (@frag) {
Branch: 1 branch
BranchTrueFalseTotal
if (@frag) 17 8 25
603 17 ▶ if ($frag[-1] =~ /^$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frag[-1] =~ /^$/) 2 15 17
604 # End-Kommentar, da das letzte Element "leer" ist. Ein
605 # End-Kommentar zeichnet sich dadurch aus, dass wir
606 # Leerzeilen ggf. *davor* entfernen müssen. Vorgehen:
607 # 1) Wir entfernen das letzte Element
608 # 2) Wir entfernen Whitspace am Ende des vorhergehenden
609 # Elements, bis auf ein Newline, falls vorhanden
610 2 pop @frag;
611 2 ▶ $frag[-1] =~ s/\s+$/substr($&,0,1) eq "\n"? "\n": ''/e;
Branch: 1 branch
BranchTrueFalseTotal
substr($&, 0, 1) eq "\n" ? : 2 0 2
612 }
613  
614 17 ▶ for (my $i = 0; $i < @frag-1; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @frag - 1) 18 17 35
615 # Wir entfernen alle Spaces und Tabs, die vor dem Kommentar stehen
616 18 $frag[$i] =~ s/([\t ]+)$//;
617  
618 18 ▶ if ($frag[$i] eq '' || substr($frag[$i],-1,1) eq "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($frag[$i] eq '' or substr($frag[$i], -1, 1) eq "\n") { } 14 4 18
Condition100%3/3
$frag[$i] eq "" or substr($frag[$i], -1, 1) eq "\n"
l!l&&r!l&&!r
774
MC/DC100%2/2
$frag[$i] eq "" or substr($frag[$i], -1, 1) eq "\n"
$frag[$i] eq "" substr($frag[$i], -1, 1) eq "\n"
Truth table100%3/3
A or B
A= $frag[$i] eq ""B= substr($frag[$i], -1, 1) eq "\n"
ABresult
1X1
011
000
619 # Ganzzeiliger Kommentar
620
621 # Einrückung des (ganzzeiligen) Kommentars vom linken Rand
622 14 my $ind = $1;
623  
624 14 my ($prevInd,$prevContent) = $frag[$i] =~ /([\t ]*)(.*)$/;
625 14 ▶ if ($ind && $prevContent && $ind eq $prevInd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ind and $prevContent and $ind eq $prevInd) { } 3 11 14
Condition100%6/6
$ind and $prevContent and $ind eq $prevInd
!ll&&!rl&&r
923
$ind and $prevContent
!ll&&!rl&&r
545
MC/DC100%3/3
$ind and $prevContent and $ind eq $prevInd
$ind $prevContent $ind eq $prevInd
Truth table100%4/4
A and B and C
A= $indB= $prevContentC= $ind eq $prevInd
ABCresult
0XX0
10X0
1100
1111
626 # Die vorhergehende Zeile ist keine Leerzeile (sie hat
627 # Content) und ist identisch eingerückt wie der Kommentar:
628 # Wir entfernen nur den Zeilenumbruch der Zeile.
629 3 $frag[$i+1] =~ s/^\n//;
630 }
631 else {
632 # Die vorhergehende Zeile ist eine Leerzeile oder
633 # hat anders eingerückten Content: Wir entfernen den
634 # Zeilenumbruch der Zeile und alle folgenden Leerzeilen.
635 11 $frag[$i+1] =~ s/^\n+//;
636 }
637 }
638 else {
639 # Teilzeiliger Kommentar: nichts zu tun, da der Kommentar
640 # und der Leerraum davor bereits entfernt ist
641 }
642 }
643 }
644  
645 25 return join('',@frag);
646 }
647  
648 # -----------------------------------------------------------------------------
649  
650 =head2 Quoting
651  
652 =head3 quote() - Fasse Zeichenkette in Single Quotes ein
653  
654 =head4 Synopsis
655  
656 $quotedStr = $class->quote($str);
657  
658 =head4 Description
659  
660 Fasse Zeichenkette $str in einfache Anführungsstriche (') ein und liefere
661 das Resultat zurück. Enthält die Zeichenkette bereits einfache
662 Anführungsstriche, werden diese per Backslash geschützt.
663  
664 =cut
665  
666 # -----------------------------------------------------------------------------
667  
668 2 ▶sub quote {
Subroutine quote: called CC=1 SCAR=0.0
Pod documented
669 2 my ($class,$str) = @_;
670  
671 2 $str =~ s/'/\\'/g;
672 2 $str = "'$str'";
673  
674 2 return $str;
675 }
676  
677 # -----------------------------------------------------------------------------
678  
679 =head2 Zeilenfortsetzungen
680  
681 =head3 joinLines() - Füge Zeilen mit Zeilenfortsetzungszeichen zusammen
682  
683 =head4 Synopsis
684  
685 $newText = $class->joinLines($char,$text);
686  
687 =head4 Arguments
688  
689 =over 4
690  
691 =item $char
692  
693 Zeilenfortsetzungszeichen
694  
695 =item $text
696  
697 Text mit Zeilenfortsetzungszeichen
698  
699 =back
700  
701 =head4 Description
702  
703 Vereinige alle Zeilen in $text, die auf das Zeilenfortsetzungszeichen
704 $char enden, mit der Folgezeile zusammen und liefere das Resultat
705 zurück. Whitespace am Anfang der Folgezeile wird entfernt.
706  
707 Wird das Zeilenfortsetzungszeichen verdoppelt, bleibt es stehen
708 und die Zeilen werden nicht zusammengefasst.
709  
710 B<Anmerkung:> Der Backslash (\) ist als Fortsetzungszeichen nicht
711 so gut geeignet, da er von Perl interpretiert wird und
712 in Zeichenketten mit doppelten Anführungsstrichen verdoppelt
713 werden muss.
714  
715 =head4 Example
716  
717 Verwende die Tilde (~) als Fortsetzungszeichen:
718  
719 Quiq::String->joinLines('~',"Dies ist ~\nein Text");
720 ==>
721 "Dies ist ein Text"
722  
723 Das Fortsetzungszeichen kann literal am Ende einer Zeile stehen,
724 wenn es verdoppelt wird:
725  
726 Quiq::String->joinLines('~',"Dies ist ~~\nein Text");
727 ==>
728 "Dies ist ~\nein Text"
729  
730 =cut
731  
732 # -----------------------------------------------------------------------------
733  
734 5 ▶sub joinLines {
Subroutine joinLines: called CC=1 SCAR=0.0
Pod documented
735 5 my ($class,$char,$text) = @_;
736 5 $text =~ s/(?<!\Q$char\E)\Q$char\E\n\s*//g; # Zeilen zusammenfassen
737 5 $text =~ s/\Q$char$char\E\n/$char\n/g; # verdoppeltes Fortsetzungszeichen
738 5 return $text;
739 }
740  
741 # -----------------------------------------------------------------------------
742  
743 =head2 Umbruch
744  
745 =head3 wrap() - Umbreche Fließtext
746  
747 =head4 Synopsis
748  
749 $text = $class->wrap($text,@opt);
750  
751 =head4 Options
752  
753 =over 4
754  
755 =item -width => $n (Default: 70)
756  
757 Maximale Zeilenbreite des resultierenden Fließtextes (sofern kein
758 einzelnes Wort länger als die Zeilenbreite ist).
759  
760 =back
761  
762 =head4 Description
763  
764 Umbreche Fließext $text, so dass die Zeilenlänge $width möglichst
765 nicht überschritten wird. Sie kann überschritten werden, wenn ein
766 Wort länger als $width ist, sonst ist die Zeilenlänge <= $width.
767  
768 Whitespace:
769  
770 =over 2
771  
772 =item *
773  
774 Whitespace am Anfang und am Ende von $text wird entfernt
775  
776 =item *
777  
778 Folgen von Whitespace-Zeichen innerhalb von $text werden zu
779 einem Whitespace-Zeichen reduziert
780  
781 =back
782  
783 Paragraphen:
784  
785 Ist der Text in Paragraphen organisiert und soll dies erhalten
786 bleiben, muss jeder Paragraph einzeln umbrochen werden.
787  
788 =head4 Example
789  
790 Maximale Zeilenlänge auf 12 Zeichen begrenzen:
791  
792 $txt = "Dies ist ein Test mit einem kurzen Text.";
793 $txt = Quiq::String->wrap($txt,-width=>12);
794 # =>
795 Dies ist ein
796 Test mit
797 einem kurzen
798 Text.
799  
800 =cut
801  
802 # -----------------------------------------------------------------------------
803  
804 4 ▶sub wrap {
Subroutine wrap: called CC=7 SCAR=19.5
Pod documented
805 4 my $class = shift;
806 4 my $text = shift;
807 # @_: @opt
808  
809 # Optionen
810  
811 4 my $width = 70;
812 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 1 4
813 3 Quiq::Option->extract(\@_,
814 -width => \$width,
815 );
816 }
817  
818 4 $text =~ s/^\s+//;
819 4 $text =~ s/\s+$//;
820  
821 4 my @words = split /\s+/,$text;
822  
823 4 my $newText = '';
824 4 my $line = '';
825 4 ▶ while (my $word = shift @words) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $word = shift @words) 24 4 28
826 24 my $l = length $line;
827 24 ▶ if ($l == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($l == 0) { } 3 21 24
elsif ($l + length($word) + 1 > $width) { } 9 12 21
828 3 ▶ if (length($word) > $width) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $word > $width) { } 0 3 3
829 0 $newText .= "$word\n";
830 }
831 else {
832 3 $line .= $word;
833 }
834 }
835 elsif ($l+length($word)+1 > $width) {
836 9 $newText .= "$line\n";
837 9 $line = $word;
838 }
839 else {
840 12 $line .= " $word";
841 }
842 }
843 4 ▶ if ($line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line) 3 1 4
844 3 $newText.= $line;
845 }
846  
847 4 return $newText;
848 }
849  
850 # -----------------------------------------------------------------------------
851  
852 =head2 Zusammenfügen
853  
854 =head3 concat() - Konkateniere Werte mit Trennzeichen
855  
856 =head4 Synopsis
857  
858 $val = $class->concat($sep,@values);
859  
860 =head4 Arguments
861  
862 =over 4
863  
864 =item @values
865  
866 Die Liste der Werte
867  
868 =back
869  
870 =head4 Returns
871  
872 (String) Konkatenation der Werte
873  
874 =head4 Description
875  
876 Konkateniere die werte @values mit Trennzeichen $sep. Ist ein Wert nicht
877 vorhanden (undef oder Leerstring), wird er ausgelassen.
878  
879 =cut
880  
881 # -----------------------------------------------------------------------------
882  
883 1 ▶sub concat {
Subroutine concat: called CC=5 SCAR=16.1
Pod documented
884 1 my $class = shift;
885 1 my $sep = shift;
886 # @_: @values
887  
888 1 my $val = '';
889 1 for my $str (@_) {
890 7 ▶ if (!defined($str) || $str eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $str or $str eq "") 3 4 7
Condition100%3/3
not defined $str or $str eq ""
l!l&&r!l&&!r
124
MC/DC100%2/2
not defined $str or $str eq ""
not defined $str $str eq ""
Truth table100%3/3
not(A) or B
A= not defined $strB= $str eq ""
ABresult
1X1
011
000
891 3 next;
892 }
893 4 ▶ if ($val ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($val ne "") 3 1 4
894 3 $val .= $sep;
895 }
896 4 $val .= $str;
897 }
898  
899 1 return $val;
900 }
901  
902 # -----------------------------------------------------------------------------
903  
904 =head1 VERSION
905  
906 1.240
907  
908 =head1 AUTHOR
909  
910 Frank Seitz, L<http://fseitz.de/>
911  
912 =head1 COPYRIGHT
913  
914 Copyright (C) 2026 Frank Seitz
915  
916 =head1 LICENSE
917  
918 This code is free software; you can redistribute it and/or modify
919 it under the same terms as Perl itself.
920  
921 =cut
922  
923 # -----------------------------------------------------------------------------
924  
925 270 1;
926  
927 # eof
« Quiq/Html/List.pm ↑ Summary Quiq/Trash.pm »