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