| 1 | ## -*- Mode: CPerl -*- | ||||||||||||||||||||||||||||||||||||||||||
| 2 | ## File: Lingua::TT::TextAlignment.pm | ||||||||||||||||||||||||||||||||||||||||||
| 3 | ## Author: Bryan Jurish <TT/IO.pm> | ||||||||||||||||||||||||||||||||||||||||||
| 4 | ## Descript: TT Utils: alignment raw text <-> tokenized text | ||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | package Lingua::TT::TextAlignment; | ||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Lingua::TT::Persistent; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Lingua::TT::Document; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | ##============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 14 | ## Globals & Constants | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | our @ISA = qw(Lingua::TT::Persistent Exporter); | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | our @EXPORT = qw(); | |||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | our %EXPORT_TAGS = ( | |||||||||||||||||||||||||||||||||||||||||
| 20 | escape=>[qw(escape_rtt unescape_rtt)], | ||||||||||||||||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | $EXPORT_TAGS{all} = [map {@$_} values %EXPORT_TAGS]; | |||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | our @EXPORT_OK = @{$EXPORT_TAGS{all}}; | |||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | ##============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 26 | ## Constructors etc. | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | ## $ta = CLASS_OR_OBJECT->new(%opts) | ||||||||||||||||||||||||||||||||||||||||||
| 29 | ## + %opts, %$ta: | ||||||||||||||||||||||||||||||||||||||||||
| 30 | ## buf=>$buf, ##-- raw text buffer | ||||||||||||||||||||||||||||||||||||||||||
| 31 | ## lines=>\@lines, ##-- raw tt-lines loaded with Lingua::TT::IO->getLines | ||||||||||||||||||||||||||||||||||||||||||
| 32 | ## off=>$off, len=>$len, ##-- byte offsets and lengths in $buf of lines in \@lines | ||||||||||||||||||||||||||||||||||||||||||
| 33 | ## ## : ($o,$l)=(vec($off,$i,32),vec($len,$i,32)) iff bytes::susbstr($buf,$o,$l) ~ $lines->[$i] | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | my $that = shift; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | my $ta = bless({ | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $that || $that
MC/DC0%0/2
ref $that || $that
ref $that
$that
Truth table0%0/3
A || B
A= ref $thatB= $that
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | buf=>'', | ||||||||||||||||||||||||||||||||||||||||||
| 38 | lines=>[], | ||||||||||||||||||||||||||||||||||||||||||
| 39 | off=>'', len=>'', | ||||||||||||||||||||||||||||||||||||||||||
| 40 | @_ | ||||||||||||||||||||||||||||||||||||||||||
| 41 | }, ref($that)||$that); | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | return $ta; | |||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | ## undef = $ta->clear() | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | sub clear { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clear: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | my $ta = shift; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | @{$ta->{lines}} = qw(); | |||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | %$ta = %{$ta->new(lines=>$ta->{lines})}; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | return $ta; | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | ##============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 55 | ## Methods: I/O | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | ##-------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 58 | ## Methods: I/O: RTT ("RAW \t TEXT \t ...", with %%$c= comments) | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | ## $str_escaped = escape_rtt($str) | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub escape_rtt { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
escape_rtt: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | $s =~ s/\\/\\\\/g; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | $s =~ s/\f/\\f/g; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | $s =~ s/\t/\\t/g; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | $s =~ s/\r/\\r/g; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | $s =~ s/\n/\\n/g; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | return $s; | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | ## $str_escaped = unescape_rtt($str) | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | sub unescape_rtt { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unescape_rtt: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | $s =~ s/\\\\/\\/g; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | $s =~ s/\\f/\f/g; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | $s =~ s/\\t/\t/g; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | $s =~ s/\\r/\r/g; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | $s =~ s/\\n/\n/g; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | return $s; | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | ## $ta = $ta->toRttFile($filename_or_fh,%opts) | ||||||||||||||||||||||||||||||||||||||||||
| 83 | ## + saves $ta to rtt-file | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 1 | ▶ | BEGIN { *save = *saveRtt = *saveRttFile = *saveNativeFile = *saveNativeFh = \&toRttFile; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub toRttFile { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
toRttFile: not called CC=23 SCAR=62.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | my ($ta,$file,%opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | my $ttio = Lingua::TT::IO->toFile($file,%opts) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $ta || $ta
MC/DC0%0/2
ref $ta || $ta
ref $ta
$ta
Truth table0%0/3
A || B
A= ref $taB= $ta
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | or die((ref($ta)||$ta)."::toRttFile(): open failed for '$file': $!"); | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | my $fh = $ttio->{fh}; | |||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 1 | ▶ | no bytes; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | my $bufr = \$ta->{buf}; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | my $offr = \$ta->{off}; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | my $lenr = \$ta->{len}; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | my $buf_is_utf8 = utf8::is_utf8($$bufr); | |||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | my ($pos,$i) = (0,0); | |||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | my ($l,$off,$len,$t0, $t,$rest); | |||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | my $ctxt = ''; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | my $compact = $opts{compact}; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | my $wrote_rtt_header = 0; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | foreach (@{$ta->{lines}}) { | |||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | $off = vec($$offr,$i,32); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | $len = vec($$lenr,$i,32); | |||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | ##-- check for raw-text prefix | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if ($pos < $off) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | $ctxt .= escape_rtt(bytes::substr($$bufr,$pos,$off-$pos)); | |||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | utf8::decode($ctxt) if ($buf_is_utf8); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | $ctxt =~ s/%%.*?%%//g if (!$opts{keep_text_comments}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | if ($ctxt ne '' && (!$compact || $ctxt !~ /^\s+$/)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$ctxt ne "" and not $compact && $ctxt =~ /^\s+$/
$compact && $ctxt =~ /^\s+$/
MC/DC0%0/3
$ctxt ne "" and not $compact && $ctxt =~ /^\s+$/
$ctxt ne ""
$compact
$ctxt =~ /^\s+$/
Truth table0%0/4
A and not(B && C)
A= $ctxt ne ""B= $compactC= $ctxt =~ /^\s+$/
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | $fh->print("%%\$c=$ctxt\n"); | |||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | $ctxt = ''; | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if (/^%%\$RTT:/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | ##-- RTT processing instruction: skip it | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | ▶ | elsif (/^%%/ || /^$/) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
/^%%/ or /^$/
MC/DC0%0/2
/^%%/ or /^$/
/^%%/
/^$/
Truth table0%0/3
A or B
A= /^%%/B= /^$/
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | ##-- eos or comment | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | $l = $_; | |||||||||||||||||||||||||||||||||||||||||
| 123 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 124 | ##-- normal word | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | $t0 = bytes::substr($$bufr,$off,$len); | |||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | utf8::decode($t0) if ($buf_is_utf8); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | if ($compact) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | ##-- compact mode: check for normalized text identity (on DECODED text) | ||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | ($t,$rest) = split(/\t/,$_,2); | |||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | if ($t eq $t0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | $t0 = escape_rtt($t0); | |||||||||||||||||||||||||||||||||||||||||
| 133 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | $t0 = escape_rtt("$t0 \$= $t"); | |||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | $l = "$ctxt$t0".(defined($rest) ? "\t$rest" : ''); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | $ctxt = ''; | |||||||||||||||||||||||||||||||||||||||||
| 138 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 139 | ##-- prolix mode | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | $l = escape_rtt($t0)."\t".$_; | |||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | ##-- ensure header before first word | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | if (!$wrote_rtt_header) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | $fh->print("%%\$RTT:COMPACT=", ($compact ? 1 : 0), "\n"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | $wrote_rtt_header = 1; | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | ▶ | $l .= "\n" if ($l !~ /\R\z/); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | $fh->print($l); | |||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | ++$i; | |||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | $pos = $off + $len; | |||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | if ($pos < bytes::length($$bufr)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | $ctxt = escape_rtt(bytes::substr($$bufr,$pos,bytes::length($$bufr)-$pos)); | |||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | ▶ | utf8::decode($ctxt) if ($buf_is_utf8); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | $fh->print("%%\$c=$ctxt\n") if ($ctxt ne ''); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | $ttio->close(); | |||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | ## $ta = $ta->fromRttFile($filename_or_fh,%opts) | ||||||||||||||||||||||||||||||||||||||||||
| 164 | ## + parses @$tta{qw(buf lines off len) from $filename_or_fh | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 1 | ▶ | BEGIN { *load = *loadRtt = *loadRttFile = *loadNativeFile = *loadNativeFh = \&fromRttFile; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | sub fromRttFile { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fromRttFile: not called CC=13 SCAR=50.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | my ($ta,$file,%opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | ▶ | $ta = $ta->new() if (!ref($ta)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | ▶ | my $ttio = Lingua::TT::IO->fromFile($file,%opts) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $ta || $ta
MC/DC0%0/2
ref $ta || $ta
ref $ta
$ta
Truth table0%0/3
A || B
A= ref $taB= $ta
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | or die((ref($ta)||$ta)."::fromRttFile(): open failed for '$file': $!"); | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | my $fh = $ttio->{fh}; | |||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | 1 | ▶ | no bytes; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | $ta->clear(); | |||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | my $bufr = \$ta->{buf}; | |||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | my $offr = \$ta->{off}; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | my $lenr = \$ta->{len}; | |||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | my $lines = $ta->{lines}; | |||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | my ($pos,$i) = (0,0); | |||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | my ($raw,$rest); | |||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | my $compact = $opts{compact}; | |||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | my ($ctxt,$t,$rt); | |||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | ▶ | while (defined($_=<$fh>)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | chomp; | |||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | ▶ | if (/^%%\$RTT:COMPACT=(.*)$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | $compact = $1; | |||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 189 | elsif (/^%%\$c=(.*)$/) { | ||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | $raw = unescape_rtt($1); | |||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | $$bufr .= $raw; | |||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | $pos += bytes::length($raw); | |||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||
| 194 | ▶ | elsif (/^%%/ || /^$/) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
/^%%/ or /^$/
MC/DC0%0/2
/^%%/ or /^$/
/^%%/
/^$/
Truth table0%0/3
A or B
A= /^%%/B= /^$/
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | push(@$lines, $_); | |||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | vec($$offr,$i,32) = $pos; | |||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | vec($$lenr,$i,32) = 0; | |||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | ++$i; | |||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||
| 200 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | ($raw,$rest) = split(/\t/,$_,2); | |||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | $raw = unescape_rtt($raw); | |||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | ▶ | if ($compact) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | ##-- compact mode: decode word-text and prepend it to $rest | ||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | ▶ | if ($raw =~ s/^(\s+)//) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | $ctxt = $1; | |||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | $$bufr .= $ctxt; | |||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | $pos += bytes::length($ctxt); | |||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | ▶ | if (($rt=$raw) =~ /^(.*) \$= (.*)$/s) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | ($raw,$t) = ($1,escape_rtt($2)); | |||||||||||||||||||||||||||||||||||||||||
| 213 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | $t = escape_rtt($raw); | |||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | ▶ | $rest = $t . (defined($rest) ? "\t$rest" : ''); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | vec($$offr, $i, 32) = $pos; | |||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | vec($$lenr, $i, 32) = bytes::length($raw); | |||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | $$bufr .= $raw; | |||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | push(@$lines, $rest); | |||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | ++$i; | |||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | $pos += bytes::length($raw); | |||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | $ttio->close(); | |||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | return $ta; | |||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | ##-------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 232 | ## Methods: I/O: TT (+ offsets) | ||||||||||||||||||||||||||||||||||||||||||
| 233 | ## + to be used in conjunction with TextBuffer I/O methods | ||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | ## $ta = $ta->parseOffsetLines() | ||||||||||||||||||||||||||||||||||||||||||
| 236 | ## + parses @$ta{qw(off len)} from $ta->{lines} | ||||||||||||||||||||||||||||||||||||||||||
| 237 | ## + destructively alters $ta->{lines} | ||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | ▶ | sub parseOffsetLines { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parseOffsetLines: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | my $ta = shift; | |||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | my $offr = \$ta->{off}; | |||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | my $lenr = \$ta->{len}; | |||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | $$offr = ''; | |||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | $$lenr = ''; | |||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | my ($pos,$i) = (0,0); | |||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | my ($woff,$wlen); | |||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | foreach (@{$ta->{lines}}) { | |||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | ▶ | if (!/^%%/ && s/^([^\t]*)\t([0-9]+) ([0-9]+)/$1/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not /^%%/ and s/^([^\t]*)\t([0-9]+) ([0-9]+)/$1/
MC/DC0%0/2
not /^%%/ and s/^([^\t]*)\t([0-9]+) ([0-9]+)/$1/
not /^%%/
s/^([^\t]*)\t([0-9]+) ([0-9]+)/$1/
Truth table0%0/3
not(A) and B
A= not /^%%/B= s/^([^\t]*)\t([0-9]+) ([0-9]+)/$1/
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | ($woff,$wlen) = ($2,$3); | |||||||||||||||||||||||||||||||||||||||||
| 249 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | ($woff,$wlen) = ($pos,0); | |||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||
| 252 | } continue { | ||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | vec($$offr,$i,32) = $woff; | |||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | vec($$lenr,$i,32) = $wlen; | |||||||||||||||||||||||||||||||||||||||||
| 255 | 0 | $pos = $woff+$wlen; | |||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | ++$i; | |||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | return $ta; | |||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | ## $ta = $CLASS_OR_OBJECT->fromTTFile($filename_or_fh,%opts) | ||||||||||||||||||||||||||||||||||||||||||
| 262 | ## + parses $ta->{doc} from file | ||||||||||||||||||||||||||||||||||||||||||
| 263 | 1 | ▶ | BEGIN { *loadTT = *loadTTFile = \&fromTTFile; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | ▶ | sub fromTTFile { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fromTTFile: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | my ($ta,$file,%opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | ▶ | $ta = $ta->new() if (!ref($ta)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | ▶ | my $ttio = Lingua::TT::IO->fromFile($file,%opts) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $ta || $ta
MC/DC0%0/2
ref $ta || $ta
ref $ta
$ta
Truth table0%0/3
A || B
A= ref $taB= $ta
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | or die((ref($ta)||$ta)."::fromTTFile(): open failed for '$file': $!"); | ||||||||||||||||||||||||||||||||||||||||||
| 269 | 0 | $ta->{lines} = $ttio->getLines(); | |||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | $ttio->close(); | |||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | return $ta->parseOffsetLines(); | |||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | ## $ta = $ta->toTTFile($filename_or_fh,%opts) | ||||||||||||||||||||||||||||||||||||||||||
| 275 | ## + saves $ta to file (with offset+len pairs) | ||||||||||||||||||||||||||||||||||||||||||
| 276 | 1 | ▶ | BEGIN { *saveTT = *saveTTFile = \&toTTFile; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | ▶ | sub toTTFile { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
toTTFile: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | my ($ta,$file,%opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | ▶ | my $ttio = Lingua::TT::IO->toFile($file,%opts) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $ta || $ta
MC/DC0%0/2
ref $ta || $ta
ref $ta
$ta
Truth table0%0/3
A || B
A= ref $taB= $ta
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | or die((ref($ta)||$ta)."::toTTFile(): open failed for '$file': $!"); | ||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | my $fh = $ttio->{fh}; | |||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | my $lines = $ta->{lines}; | |||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | my $offr = \$ta->{off}; | |||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | my $lenr = \$ta->{len}; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | my ($l,$w,$rest); | |||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | my $i=0; | |||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | foreach (@$lines) { | |||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | ▶ | if (/^$/ || /^%%/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
/^$/ or /^%%/
MC/DC0%0/2
/^$/ or /^%%/
/^$/
/^%%/
Truth table0%0/3
A or B
A= /^$/B= /^%%/
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | $l = $_; | |||||||||||||||||||||||||||||||||||||||||
| 290 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | ($w,$rest) = split(/\t/,$_,2); | |||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | ▶ | $l = "$w\t".vec($$offr,$i,32)." ".vec($$lenr,$i,32).(defined($rest) ? "\t$rest" : ''); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | ▶ | $l .= "\n" if ($l !~ /\R\z/); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | } continue { | ||||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | $fh->print($l); | |||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | ++$i; | |||||||||||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | $ttio->close(); | |||||||||||||||||||||||||||||||||||||||||
| 300 | 0 | return $ta; | |||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | ##-------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 304 | ## Methods: I/O: text-buffer | ||||||||||||||||||||||||||||||||||||||||||
| 305 | ## + to be used in conjunction with TTFile I/O methods | ||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||
| 307 | ## $ta = $ta->loadTextFile($filename_or_fh,%opts) | ||||||||||||||||||||||||||||||||||||||||||
| 308 | ## + %opts: | ||||||||||||||||||||||||||||||||||||||||||
| 309 | ## raw => $bool, ##-- set to avoid utf8 flag on buf | ||||||||||||||||||||||||||||||||||||||||||
| 310 | 1 | ▶ | BEGIN { *loadBuffer = \&loadTextFile; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | ▶ | sub loadTextFile { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
loadTextFile: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | my ($ta,$file,%opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 313 | 0 | ▶ | my $fh = ref($file) ? $file : IO::File->new("<$file"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | ▶ | die((ref($ta)||$ta),"::loadTextFile(): open failed for '$file': $!") if (!$fh); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $ta || $ta
MC/DC0%0/2
ref $ta || $ta
ref $ta
$ta
Truth table0%0/3
A || B
A= ref $taB= $ta
| |||||||||||||||||||||||||||||||||||||||||||
| 315 | 0 | ▶ | if ($opts{encoding}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | binmode($fh,":encoding($opts{encoding})"); | |||||||||||||||||||||||||||||||||||||||||
| 317 | } elsif ($opts{raw}) { | ||||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | binmode($fh,':raw'); | |||||||||||||||||||||||||||||||||||||||||
| 319 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | binmode($fh,':utf8'); | |||||||||||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | local $/=undef; | |||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | $ta->{buf} = <$fh>; | |||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | $fh->close(); | |||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | return $ta; | |||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | ## $ta = $ta->saveTextFile($filename_or_fh,%opts) | ||||||||||||||||||||||||||||||||||||||||||
| 329 | ## + %opts: | ||||||||||||||||||||||||||||||||||||||||||
| 330 | ## raw => $bool, ##-- set to avoid utf8 flag on buf | ||||||||||||||||||||||||||||||||||||||||||
| 331 | 1 | ▶ | BEGIN { *saveBuffer = \&saveTextFile; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | ▶ | sub saveTextFile { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
saveTextFile: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | my ($ta,$file,%opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | ▶ | my $fh = ref($file) ? $file : IO::File->new(">$file"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 335 | 0 | ▶ | die((ref($ta)|$ta),"::saveTextFile(): open failed for '$file': $!") if (!$fh); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | ▶ | if ($opts{encoding}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | binmode($fh,":encoding($opts{encoding})"); | |||||||||||||||||||||||||||||||||||||||||
| 338 | } elsif ($opts{raw}) { | ||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | binmode($fh,':raw'); | |||||||||||||||||||||||||||||||||||||||||
| 340 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | ▶ | binmode($fh,':utf8') if (utf8::is_utf8($ta->{buf})); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||||||||||||
| 343 | 0 | $fh->print($ta->{buf}); | |||||||||||||||||||||||||||||||||||||||||
| 344 | 0 | $fh->close(); | |||||||||||||||||||||||||||||||||||||||||
| 345 | 0 | return $ta; | |||||||||||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | ##============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 351 | ## Footer | ||||||||||||||||||||||||||||||||||||||||||
| 352 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | __END__ | ||||||||||||||||||||||||||||||||||||||||||