lib/Serge/Util.pm

filter: Statementstmt 51.9% 256 / 493 Branchbran 37.6% 67 / 178 Conditioncond 11.1% 5 / 45 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 50.6% 38 / 75 Podpod 0.0% 0 / 61 totaltotal 41.5% 366 / 882 SCAR 75.6
CC
107
Cov
46%
read_and_normalize_file
87.3
read_and_normalize_file
87.3
culture_from_lang
40.6
CRAP 1928.9
« lib/Serge/Importer.pm ↑ Summary lib/Serge/Engine/Plugin/transform.pm »
1 package Serge::Util;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our @ISA = qw(Exporter);
8  
9 2 our @EXPORT = qw(
10 combine_and
11 combine_or
12 abspath
13 normalize_path
14 get_flag_pos
15 is_flag_set
16 set_flag
17 remove_flag
18 generate_key
19 generate_hash
20 locale_from_lang
21 subst_macros
22 normalize_strref
23 escape_strref
24 unescape_strref
25 encode
26 po_is_msgid_plural
27 po_serialize_msgid
28 po_serialize_msgstr
29 glue_plural_string
30 split_plural_string
31 po_wrap
32 read_and_normalize_file
33 file_mtime
34 );
35  
36 2 our @EXPORT_OK = qw(
37 culture_from_lang
38 full_locale_from_lang
39 langname
40 locale_android
41 locale_iphone
42 langname_iphone
43 subst_env_var
44 subst_macros_strref
45 xml_escape_strref
46 xml_unescape_strref
47 wrap
48 set_flags
49 remove_flags
50 );
51  
52 2 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
53 2 ▶use Encode qw(encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
54 2 ▶use Encode::Guess;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
55 2 ▶use File::Spec::Functions qw(rel2abs);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 2 ▶use Serge::Util::LangID;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 2 our $PO_LINE_LENGTH = 76; # used to wrap long lines in .po files, value is the same as in Pootle
60  
61 2 our $UNIT_SEPARATOR = "\x1f"; # ASCII "Unit Separator" character, used to delimit plural forms in a string
62  
63 # A hash of legacy locale names used in Mac/iPhone resource folder naming.
64 # For other languages, a locale code will be used (e.g. 'fi' or 'pt_BR')
65  
66 1 my $APPLE_LEGACY_LOCALE_NAMES = {
67 'en' => 'English',
68 'fr' => 'French',
69 'de' => 'German',
70 'it' => 'Italian',
71 'ja' => 'Japanese',
72 'pl' => 'Polish',
73 'ru' => 'Russian',
74 'es' => 'Spanish',
75 'sv' => 'Swedish',
76 };
77  
78 # A hash of locale name that need to be changed specifically for iPhone
79  
80 1 my $APPLE_IPHONE_LOCALE_REWRITE = {
81 'pt' => 'pt_PT',
82 'pt_BR' => 'pt',
83 };
84  
85 # return 1 if all items in the provided array evaluate to true; otherwise return 0
86 sub combine_and {
87 264 ▶ my $result = scalar(@_) > 0;
Subroutine combine_and: called CC=3 SCAR=16.2
Subroutine combine_and: not called CC=3 SCAR=16.2
Pod undocumented
Pod undocumented
88 264 ▶ map { $result &&= $_ } @_;
Condition16%1/6
$result &&= $_
!ll&&!rl&&r
00264
$result &&= $_
!ll&&!rl&&r
000
MC/DC0%0/2
$result &&= $_
$result $_
MC/DC0%0/2
$result &&= $_
$result $_
Truth table33%1/3
A &&= B
A= $resultB= $_
ABresult
0X0
100
111
Truth table0%0/3
A &&= B
A= $resultB= $_
ABresult
0X0
100
111
89 264 ▶ return $result ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
$result ? : 256 8 264
$result ? : 0 0 0
90 }
91  
92 # return 1 if any item in the provided array evaluates to true; otherwise return 0
93 sub combine_or {
94 54 ▶ my $result = 0;
Subroutine combine_or: called CC=3 SCAR=19.8
Subroutine combine_or: not called CC=3 SCAR=19.8
Pod undocumented
Pod undocumented
95 54 ▶ map { $result ||= $_ } @_;
Condition0%0/6
$result ||= $_
l!l&&r!l&&!r
000
$result ||= $_
l!l&&r!l&&!r
000
MC/DC0%0/2
$result ||= $_
$result $_
MC/DC0%0/2
$result ||= $_
$result $_
Truth table0%0/3
A ||= B
A= $resultB= $_
ABresult
1X1
011
000
Truth table0%0/3
A ||= B
A= $resultB= $_
ABresult
1X1
011
000
96 54 ▶ return $result ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
$result ? : 0 54 54
$result ? : 0 0 0
97 }
98  
99 sub abspath {
100 21 ▶ my ($root, $path) = @_;
Subroutine abspath: called CC=1 SCAR=1.2
Subroutine abspath: not called CC=1 SCAR=1.2
Pod undocumented
Pod undocumented
101 21 $path = rel2abs($path, $root);
102 21 $path =~ s/\\/\//sg; # always use forward slash
103 21 return $path;
104 }
105  
106 sub normalize_path {
107 0 ▶ my ($path) = @_;
Subroutine normalize_path: not called CC=1 SCAR=6.9
Subroutine normalize_path: not called CC=1 SCAR=6.9
Pod undocumented
Pod undocumented
108 0 $path =~ s/\\/\//g; # use forward slashes
109 0 $path =~ s/\/\//\//g; # remove double slashes
110 0 $path =~ s/\/+$//g; # remove slashes at the end of the path
111 0 return $path;
112 }
113  
114 sub get_flag_pos {
115 435 ▶ my ($flagsref, $name) = @_;
Subroutine get_flag_pos: called CC=3 SCAR=14.2
Subroutine get_flag_pos: not called CC=3 SCAR=14.2
Pod undocumented
Pod undocumented
116  
117 435 my $i = -1;
118 435 foreach my $flag (@$flagsref) {
119 124 $i++;
120 124 ▶ return $i if ($flag eq $name);
Branch: 2 branches
BranchTrueFalseTotal
if $flag eq $name 29 95 124
if $flag eq $name 0 0 0
121 }
122  
123 406 return -1;
124 }
125  
126 sub is_flag_set {
127 435 ▶ return (get_flag_pos(@_) >= 0);
Subroutine is_flag_set: called CC=1 SCAR=1.2
Subroutine is_flag_set: not called CC=1 SCAR=1.2
Pod undocumented
Pod undocumented
128 }
129  
130 sub set_flag {
131 27 ▶ my ($flagsref, $name) = @_;
Subroutine set_flag: called CC=2 SCAR=9.2
Subroutine set_flag: not called CC=2 SCAR=9.2
Pod undocumented
Pod undocumented
132 27 ▶ push @$flagsref, $name unless is_flag_set($flagsref, $name);
Branch: 2 branches
BranchTrueFalseTotal
unless is_flag_set($flagsref, $name) 17 10 27
unless is_flag_set($flagsref, $name) 0 0 0
133 }
134  
135 sub set_flags {
136 7 ▶ my ($flagsref, @list) = @_;
Subroutine set_flags: called CC=1 SCAR=0.0
Pod undocumented
137 7 map { set_flag($flagsref, $_) } @list;
138 }
139  
140 sub remove_flag {
141 0 ▶ my ($flagsref, $name) = @_;
Subroutine remove_flag: not called CC=2 SCAR=17.9
Subroutine remove_flag: not called CC=2 SCAR=17.9
Pod undocumented
Pod undocumented
142 0 my $i = get_flag_pos($flagsref, $name);
143 0 ▶ return if $i == -1;
Branch: 2 branches
BranchTrueFalseTotal
if $i == -1 0 0 0
if $i == -1 0 0 0
144 0 splice @$flagsref, $i, 1;
145 }
146  
147 sub remove_flags {
148 0 ▶ my ($flagsref, @list) = @_;
Subroutine remove_flags: not called CC=1 SCAR=6.9
Pod undocumented
149 0 map { remove_flag($flagsref, $_) } @list;
150 }
151  
152 sub generate_key {
153 368 ▶ my ($str, $context) = @_;
Subroutine generate_key: called CC=1 SCAR=1.2
Subroutine generate_key: not called CC=1 SCAR=1.2
Pod undocumented
Pod undocumented
154  
155 368 return md5_hex(encode_utf8($str."\001".$context));
156 }
157  
158 sub generate_hash {
159 93 ▶ return md5_hex(encode_utf8(join("\001", @_)));
Subroutine generate_hash: called CC=1 SCAR=1.2
Subroutine generate_hash: not called CC=1 SCAR=1.2
Pod undocumented
Pod undocumented
160 }
161  
162 sub locale_from_lang {
163 314 ▶ my $locale = shift;
Subroutine locale_from_lang: called CC=1 SCAR=2.0
Subroutine locale_from_lang: not called CC=1 SCAR=2.0
Pod undocumented
Pod undocumented
164 314 $locale =~ s/(-.+?)(-.+)?$/uc($1).$2/e; # convert e.g. 'pt-br-Whatever' to 'pt-BR-Whatever'
165 314 $locale =~ s/-/_/g;
166 314 return $locale;
167 }
168  
169 sub culture_from_lang {
170 306 ▶ my $culture = shift;
Subroutine culture_from_lang: called CC=24 SCAR=37.0
Pod undocumented
171 306 $culture =~ s/(-.+?)(-.+)?$/uc($1).$2/e; # convert e.g. 'pt-br-Whatever' to 'pt-BR-Whatever'
172 306 ▶ if ($culture =~ m/^\w+$/) { # just language
Branch: 1 branch
BranchTrueFalseTotal
if ($culture =~ /^\w+$/) 306 0 306
173 306 my $suffix = $culture;
174 306 ▶ $suffix = 'US' if ($suffix eq 'en'); # en-US
Branch: 1 branch
BranchTrueFalseTotal
if $suffix eq "en" 102 204 306
175 306 ▶ $suffix = 'JP' if ($suffix eq 'ja'); # ja-JP
Branch: 1 branch
BranchTrueFalseTotal
if $suffix eq "ja" 0 306 306
176 306 ▶ $suffix = 'SE' if ($suffix eq 'sv'); # sv-SE
Branch: 1 branch
BranchTrueFalseTotal
if $suffix eq "sv" 0 306 306
177 306 ▶ $suffix = 'CN' if ($suffix eq 'zh'); # zh-CN // Simplified Chinese
Branch: 1 branch
BranchTrueFalseTotal
if $suffix eq "zh" 0 306 306
178 306 ▶ $suffix = 'KR' if ($suffix eq 'ko'); # ko-KR
Branch: 1 branch
BranchTrueFalseTotal
if $suffix eq "ko" 0 306 306
179 306 ▶ $suffix = 'DK' if ($suffix eq 'da'); # da-DK
Branch: 1 branch
BranchTrueFalseTotal
if $suffix eq "da" 0 306 306
180 306 ▶ $suffix = 'RS' if ($suffix eq 'sr'); # sr-RS // Serbian (Cyrillic)
Branch: 1 branch
BranchTrueFalseTotal
if $suffix eq "sr" 0 306 306
181 306 ▶ $suffix = 'RS' if ($suffix eq 'sh'); # sh-RS // Serbian (Latin)
Branch: 1 branch
BranchTrueFalseTotal
if $suffix eq "sh" 0 306 306
182 306 ▶ $suffix = 'GR' if ($suffix eq 'el'); # el-GR
Branch: 1 branch
BranchTrueFalseTotal
if $suffix eq "el" 0 306 306
183 306 ▶ $suffix = 'CZ' if ($suffix eq 'cs'); # cs-CZ
Branch: 1 branch
BranchTrueFalseTotal
if $suffix eq "cs" 0 306 306
184 306 ▶ $suffix = 'NO' if ($suffix eq 'nb'); # nb-NO // Norwegian Bokmal
Branch: 1 branch
BranchTrueFalseTotal
if $suffix eq "nb" 0 306 306
185 306 ▶ $suffix = 'NO' if ($suffix eq 'nn'); # nn-NO // Norwegian Nynorsk
Branch: 1 branch
BranchTrueFalseTotal
if $suffix eq "nn" 0 306 306
186 306 ▶ $suffix = 'ES' if ($suffix eq 'ca'); # ca-ES
Branch: 1 branch
BranchTrueFalseTotal
if $suffix eq "ca" 0 306 306
187 306 ▶ $suffix = 'EE' if ($suffix eq 'et'); # et-EE // Estonian
Branch: 1 branch
BranchTrueFalseTotal
if $suffix eq "et" 0 306 306
188 306 ▶ $suffix = 'GE' if ($suffix eq 'ka'); # ka-GE // Georgian
Branch: 1 branch
BranchTrueFalseTotal
if $suffix eq "ka" 0 306 306
189 306 ▶ $suffix = 'IN' if ($suffix eq 'te'); # te-IN // Telugu
Branch: 1 branch
BranchTrueFalseTotal
if $suffix eq "te" 0 306 306
190 306 ▶ $suffix = 'IL' if ($suffix eq 'he'); # he-IL // Hebrew
Branch: 1 branch
BranchTrueFalseTotal
if $suffix eq "he" 0 306 306
191 306 ▶ $suffix = 'AE' if ($suffix eq 'ar'); # ar-AE // Arabic (U.A.E.)
Branch: 1 branch
BranchTrueFalseTotal
if $suffix eq "ar" 0 306 306
192 306 ▶ $suffix = 'ES' if ($suffix eq 'gl'); # gl-ES // Galician
Branch: 1 branch
BranchTrueFalseTotal
if $suffix eq "gl" 0 306 306
193 306 ▶ $suffix = 'UA' if ($suffix eq 'uk'); # uk-UA // Ukrainian
Branch: 1 branch
BranchTrueFalseTotal
if $suffix eq "uk" 0 306 306
194 306 ▶ $suffix = 'VN' if ($suffix eq 'vi'); # vi-VN // Vietnamese
Branch: 1 branch
BranchTrueFalseTotal
if $suffix eq "vi" 0 306 306
195 306 ▶ $suffix = 'MY' if ($suffix eq 'ms'); # ms-MY // Malay
Branch: 1 branch
BranchTrueFalseTotal
if $suffix eq "ms" 0 306 306
196 306 $culture .= '-'.uc($suffix); # convert e.g. 'ru' to 'ru-RU'
197 }
198 306 return $culture;
199 }
200  
201 sub full_locale_from_lang {
202 102 ▶ my $culture = culture_from_lang(shift);
Subroutine full_locale_from_lang: called CC=1 SCAR=0.0
Pod undocumented
203 102 $culture =~ s/-/_/g;
204 102 return $culture;
205 }
206  
207 sub langname {
208 102 ▶ my $lang = shift;
Subroutine langname: called CC=2 SCAR=7.2
Pod undocumented
209  
210 return exists $APPLE_LEGACY_LOCALE_NAMES->{$lang} ?
211 102 ▶ $APPLE_LEGACY_LOCALE_NAMES->{$lang} : locale_from_lang($lang);
Branch: 1 branch
BranchTrueFalseTotal
exists $APPLE_LEGACY_LOCALE_NAMES->{$lang} ? : 0 102 102
212 }
213  
214 sub locale_android {
215 0 ▶ my $locale = shift;
Subroutine locale_android: not called CC=1 SCAR=6.9
Pod undocumented
216 0 $locale =~ s/-(.+?)(-.+)?$/'-r'.uc($1).$2/e; # convert e.g. 'pt-br' to 'pt-rBR'
217 0 return $locale;
218 }
219  
220 sub locale_iphone {
221 0 ▶ my $locale = locale_from_lang(shift);
Subroutine locale_iphone: not called CC=2 SCAR=17.9
Pod undocumented
222 return exists $APPLE_IPHONE_LOCALE_REWRITE->{$locale} ?
223 0 ▶ $APPLE_IPHONE_LOCALE_REWRITE->{$locale} : $locale;
Branch: 1 branch
BranchTrueFalseTotal
exists $APPLE_IPHONE_LOCALE_REWRITE->{$locale} ? : 0 0 0
224 }
225  
226 sub langname_iphone {
227 0 ▶ my $langname = langname(shift);
Subroutine langname_iphone: not called CC=2 SCAR=17.9
Pod undocumented
228 return exists $APPLE_IPHONE_LOCALE_REWRITE->{$langname} ?
229 0 ▶ $APPLE_IPHONE_LOCALE_REWRITE->{$langname} : $langname;
Branch: 1 branch
BranchTrueFalseTotal
exists $APPLE_IPHONE_LOCALE_REWRITE->{$langname} ? : 0 0 0
230 }
231  
232 sub subst_env_var {
233 0 ▶ my $name = shift;
Subroutine subst_env_var: not called CC=2 SCAR=17.9
Pod undocumented
234 0 ▶ die "'$name' environment variable not defined\n" unless exists $ENV{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ENV{$name} 0 0 0
235 0 return $ENV{$name};
236 }
237  
238 sub subst_macros_strref {
239 154 ▶ my ($strref, $file, $lang, $source_lang) = @_;
Subroutine subst_macros_strref: called CC=14 SCAR=30.5
Pod undocumented
240  
241 154 $$strref =~ s/%ENV:(\w+)%/subst_env_var($1)/ge;
242  
243 154 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 105 49 154
244 105 my $path = '';
245 105 my $fname = $file;
246 105 ▶ if ($fname =~ m/^(.+[\\\/])(.+?)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fname =~ m[^(.+[\\/])(.+?)$]) 0 105 105
247 0 $path = $1;
248 0 $fname = $2;
249 }
250  
251 # splitting the filename using the leftmost dot
252  
253 105 my $lname = $fname;
254 105 my $lext;
255 105 ▶ if ($lname =~ m/^(.+?)\.(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lname =~ /^(.+?)\.(.+)$/) 105 0 105
256 105 $lname = $1;
257 105 $lext = $2;
258 }
259  
260 # splitting the filename using the rightmost dot
261  
262 105 my $name = $fname;
263 105 my $ext;
264 105 ▶ if ($name =~ m/^(.+)\.(.+?)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^(.+)\.(.+?)$/) 105 0 105
265 105 $name = $1;
266 105 $ext = $2;
267 }
268  
269 105 $$strref =~ s/%FILE%/$file/ge; # full file name with path
270 105 $$strref =~ s/%PATH%/$path/ge; # just directory with trailing delimiter
271 105 $$strref =~ s/%NAME%/$name/ge; # file name with no path and no extension
272 105 $$strref =~ s/%EXT%/$ext/ge; # extension
273 105 $$strref =~ s/%LNAME%/$lname/ge;
274 105 $$strref =~ s/%LEXT%/$lext/ge;
275  
276 # stripping the locale/culture-specific suffix from %NAME% and %LNAME%
277 # e.g. myfile_en => myfile (if the source language is 'en')
278  
279 105 ▶ if ($source_lang) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source_lang) 102 3 105
280 102 my $source_locale = locale_from_lang($source_lang);
281 102 my $source_culture = culture_from_lang($source_lang);
282  
283 102 my $name_nolocale = $name;
284 102 $name_nolocale =~ s/_(\Q$source_locale\E|\Q$source_culture\E)$//i;
285 102 $$strref =~ s/%NAME:NOLOCALE%/$name_nolocale/ge;
286  
287 102 my $lname_nolocale = $lname;
288 102 $lname_nolocale =~ s/_(\Q$source_locale\E|\Q$source_culture\E)$//i;
289 102 $$strref =~ s/%LNAME:NOLOCALE%/$lname_nolocale/ge;
290 }
291  
292 # now split the name further into name and extension parts
293 # (to be able to address files with double extensions like `file.ext1.ext2')
294  
295 105 $lname = $name;
296 105 $lext = '';
297 105 ▶ if ($lname =~ m/^(.+?)\.(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lname =~ /^(.+?)\.(.+)$/) 0 105 105
298 0 $lname = $1;
299 0 $lext = $2;
300 }
301  
302 105 $ext = '';
303 105 ▶ if ($name =~ m/^(.+)\.(.+?)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^(.+)\.(.+?)$/) 0 105 105
304 0 $name = $1;
305 0 $ext = $2;
306 }
307  
308 105 $$strref =~ s/%NAME:NAME%/$name/ge;
309 105 $$strref =~ s/%NAME:EXT%/$ext/ge;
310 105 $$strref =~ s/%NAME:LNAME%/$lname/ge;
311 105 $$strref =~ s/%NAME:LEXT%/$lext/ge;
312 }
313  
314 154 ▶ if ($lang) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang) 102 52 154
315 102 my $locale = locale_from_lang($lang);
316 102 my $full_locale = full_locale_from_lang($lang);
317 102 my $culture = culture_from_lang($lang);
318 102 my $langname = langname($lang);
319  
320 102 $$strref =~ s/%LANG%/$lang/ge;
321 102 $$strref =~ s/%LOCALE%/$locale/ge;
322 102 $$strref =~ s/%LOCALE:ANDROID%/locale_android($lang)/ge;
323 102 $$strref =~ s/%LOCALE:IPHONE%/locale_iphone($lang)/ge;
324 102 $$strref =~ s/%LOCALE:LC%/lc($locale)/ge;
325 102 $$strref =~ s/%LOCALE:UC%/uc($locale)/ge;
326 102 $$strref =~ s/%LOCALE:FULL%/$full_locale/ge;
327 102 $$strref =~ s/%LOCALE:FULL:LC%/lc($full_locale)/ge;
328 102 $$strref =~ s/%LOCALE:FULL:UC%/uc($full_locale)/ge;
329 102 $$strref =~ s/%CULTURE%/$culture/ge;
330 102 $$strref =~ s/%LANGNAME%/$langname/g;
331 102 $$strref =~ s/%LANGNAME:IPHONE%/langname_iphone($lang)/g;
332  
333 102 my $alias = $Serge::Util::LangID::alias{$lang};
334 102 ▶ $lang = $alias if $alias;
Branch: 1 branch
BranchTrueFalseTotal
if $alias 0 102 102
335 102 ▶ my $h = $Serge::Util::LangID::map{$lang} || $Serge::Util::LangID::map{''};
Condition33%1/3
$Serge::Util::LangID::map{$lang} || $Serge::Util::LangID::map{''}
l!l&&r!l&&!r
01020
MC/DC0%0/2
$Serge::Util::LangID::map{$lang} || $Serge::Util::LangID::map{''}
$Serge::Util::LangID::map{$lang} $Serge::Util::LangID::map{''}
Truth table33%1/3
A || B
A= $Serge::Util::LangID::map{$lang}B= $Serge::Util::LangID::map{''}
ABresult
1X1
011
000
336 102 ▶ if ($h) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h) 102 0 102
337 102 my $langid_dec = $h->{code};
338 102 ▶ my $langid = sprintf('%04x', $langid_dec) if ($langid_dec > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $langid_dec > 0 0 102 102
339 102 my $codepage = $h->{cp};
340 102 ▶ $codepage = $Serge::Util::LangID::default_codepage unless $codepage;
Branch: 1 branch
BranchTrueFalseTotal
unless $codepage 0 102 102
341  
342 102 $$strref =~ s/%LANGID:DEC%/$langid_dec/ge;
343 102 $$strref =~ s/%LANGID%/$langid/ge;
344 102 $$strref =~ s/%LANGCONST%/$h->{lang}/ge;
345 102 $$strref =~ s/%SUBLANGCONST%/$h->{sublang}/ge;
346 102 $$strref =~ s/%AFXTARGCONST%/$h->{afx}/ge;
347 102 $$strref =~ s/%CODEPAGE%/$codepage/ge;
348 }
349 }
350 }
351  
352 sub subst_macros {
353 151 ▶ my ($str, $file, $lang, $source_lang) = @_;
Subroutine subst_macros: called CC=1 SCAR=1.2
Subroutine subst_macros: not called CC=1 SCAR=1.2
Pod undocumented
Pod undocumented
354 151 subst_macros_strref(\$str, $file, $lang, $source_lang);
355 151 return $str;
356 }
357  
358 sub normalize_strref {
359 0 ▶ my $ref = shift;
Subroutine normalize_strref: not called CC=1 SCAR=6.9
Subroutine normalize_strref: not called CC=1 SCAR=6.9
Pod undocumented
Pod undocumented
360 0 $$ref =~ s/\s/ /sg; # normalize all whitespace to regular space characters
361 0 $$ref =~ s/[ ]{2,}/ /g; # normalize whitespace
362 0 $$ref =~ s/^[ ]//g; # remove leading whitespace
363 0 $$ref =~ s/[ ]$//g; # remove trailing whitespace
364 }
365  
366 # .PO-specific string escaping
367 # see gettext po parser for a list of control sequences
368 # https://github.com/autotools-mirror/gettext/blob/master/gettext-tools/src/po-lex.c#L749-L856
369 sub escape_strref {
370 80 ▶ my $ref = shift;
Subroutine escape_strref: called CC=1 SCAR=1.2
Subroutine escape_strref: not called CC=1 SCAR=1.2
Pod undocumented
Pod undocumented
371 80 $$ref =~ s/\\/\x1/g; # convert backslashes to a temporary symbol
372 80 $$ref =~ s/\"/\\"/g;
373 80 $$ref =~ s/\n/\\n/sg; # newline
374 80 $$ref =~ s/\t/\\t/g; # horizontal tab
375 80 $$ref =~ s/\x8/\\b/g; # 0x08 (bs) backspace
376 80 $$ref =~ s/\r/\\r/sg; # carriage return
377 80 $$ref =~ s/\xC/\\f/g; # 0x0C (np) formfeed
378 80 $$ref =~ s/\xB/\\v/g; # 0x0B (vt) vertical tab
379 80 $$ref =~ s/\x7/\\a/g; # 0x07 (bel) bel character
380 80 $$ref =~ s/\x1/\\\\/g; # restore backslashes (and escape them)
381 }
382  
383 # .PO-specific string unescaping
384 sub unescape_strref {
385 0 ▶ my $ref = shift;
Subroutine unescape_strref: not called CC=1 SCAR=6.9
Subroutine unescape_strref: not called CC=1 SCAR=6.9
Pod undocumented
Pod undocumented
386 0 $$ref =~ s/\\\\/\x1/g;
387 0 $$ref =~ s/\\"/\"/g;
388 0 $$ref =~ s/\\n/\n/sg;
389 0 $$ref =~ s/\\t/\t/g;
390 0 $$ref =~ s/\\b/\x8/g;
391 0 $$ref =~ s/\\r/\r/sg;
392 0 $$ref =~ s/\\f/\xC/g;
393 0 $$ref =~ s/\\v/\xB/g;
394 0 $$ref =~ s/\\a/\x7/g;
395 0 $$ref =~ s/\x1/\\/g;
396 }
397  
398 sub xml_escape_strref { # escape XML-unsafe chars
399 0 ▶ my ($strref, $noquotes, $apos) = @_;
Subroutine xml_escape_strref: not called CC=3 SCAR=24.8
Pod undocumented
400 0 $$strref =~ s/\&/&/g;
401 0 ▶ $$strref =~ s/\'/'/g if $apos;
Branch: 1 branch
BranchTrueFalseTotal
if $apos 0 0 0
402 0 ▶ $$strref =~ s/\"/"/g unless $noquotes;
Branch: 1 branch
BranchTrueFalseTotal
unless $noquotes 0 0 0
403 0 $$strref =~ s/\</&lt;/g;
404 0 $$strref =~ s/\>/&gt;/g;
405 }
406  
407 sub xml_unescape_strref { # unescape XML-unsafe chars
408 0 ▶ my ($strref, $noquotes, $apos) = @_;
Subroutine xml_unescape_strref: not called CC=3 SCAR=24.8
Pod undocumented
409 0 $$strref =~ s/&gt;/\>/g;
410 0 $$strref =~ s/&lt;/\</g;
411 0 ▶ $$strref =~ s/&quot;/\"/g unless $noquotes;
Branch: 1 branch
BranchTrueFalseTotal
unless $noquotes 0 0 0
412 0 ▶ $$strref =~ s/&apos;/'/g if $apos;
Branch: 1 branch
BranchTrueFalseTotal
if $apos 0 0 0
413 0 $$strref =~ s/\&amp;/&/g;
414 }
415  
416 sub encode {
417 8 ▶ my ($encoding, $str) = @_;
Subroutine encode: called CC=2 SCAR=13.0
Subroutine encode: not called CC=2 SCAR=13.0
Pod undocumented
Pod undocumented
418  
419 8 ▶ if (uc($encoding) eq 'JAVA') {
Branch: 2 branches
BranchTrueFalseTotal
if (uc $encoding eq 'JAVA') { } 0 8 8
if (uc $encoding eq 'JAVA') { } 0 0 0
420 0 $str =~ s/([^\000-\177])/sprintf "\\u%04x", ord($1)/ge;
421 0 return $str;
422 } else {
423 8 return Encode::encode($encoding, $str);
424 }
425 }
426  
427 sub po_is_msgid_plural {
428 54 ▶ my ($s) = @_;
Subroutine po_is_msgid_plural: called CC=1 SCAR=1.2
Subroutine po_is_msgid_plural: not called CC=1 SCAR=1.2
Pod undocumented
Pod undocumented
429 54 return !!($s =~ m/$UNIT_SEPARATOR/);
430 }
431  
432 sub po_serialize_msgid {
433 54 ▶ my ($s) = @_;
Subroutine po_serialize_msgid: called CC=3 SCAR=16.5
Subroutine po_serialize_msgid: not called CC=3 SCAR=16.5
Pod undocumented
Pod undocumented
434 54 my @out;
435 54 my @plurals = split_plural_string($s);
436 54 ▶ die "More than two plural forms not supported for msgid" if @plurals > 2;
Branch: 2 branches
BranchTrueFalseTotal
if @plurals > 2 0 54 54
if @plurals > 2 0 0 0
437 54 push @out, "msgid ".po_wrap($plurals[0]);
438 54 ▶ if (@plurals > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@plurals > 1) 0 54 54
if (@plurals > 1) 0 0 0
439 0 push @out, "msgid_plural ".po_wrap($plurals[1]);
440 }
441 54 return @out;
442 }
443  
444 sub po_serialize_msgstr {
445 54 ▶ my ($s, $n_plurals) = @_;
Subroutine po_serialize_msgstr: called CC=5 SCAR=28.5
Subroutine po_serialize_msgstr: not called CC=5 SCAR=28.5
Pod undocumented
Pod undocumented
446 54 my @out;
447 54 my @plurals = split_plural_string($s);
448 54 ▶ if ($n_plurals || @plurals > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($n_plurals or @plurals > 1) { } 0 54 54
if ($n_plurals or @plurals > 1) { } 0 0 0
Condition16%1/6
$n_plurals or @plurals > 1
l!l&&r!l&&!r
0054
$n_plurals or @plurals > 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$n_plurals or @plurals > 1
$n_plurals @plurals > 1
MC/DC0%0/2
$n_plurals or @plurals > 1
$n_plurals @plurals > 1
Truth table33%1/3
A or B
A= $n_pluralsB= @plurals > 1
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $n_pluralsB= @plurals > 1
ABresult
1X1
011
000
449 0 ▶ for (my $i = 0; $i < @plurals || $i < $n_plurals; $i++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i < @plurals or $i < $n_plurals) 0 0 0
if ($i < @plurals or $i < $n_plurals) 0 0 0
Condition0%0/6
$i < @plurals or $i < $n_plurals
l!l&&r!l&&!r
000
$i < @plurals or $i < $n_plurals
l!l&&r!l&&!r
000
MC/DC0%0/2
$i < @plurals or $i < $n_plurals
$i < @plurals $i < $n_plurals
MC/DC0%0/2
$i < @plurals or $i < $n_plurals
$i < @plurals $i < $n_plurals
Truth table0%0/3
A or B
A= $i < @pluralsB= $i < $n_plurals
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $i < @pluralsB= $i < $n_plurals
ABresult
1X1
011
000
450 0 $s = $plurals[$i];
451 0 push @out, "msgstr[$i] ".po_wrap($s);
452 }
453 } else {
454 54 push @out, "msgstr ".po_wrap($s);
455 }
456 54 return @out;
457 }
458  
459 sub glue_plural_string {
460 0 ▶ my @variants = @_;
Subroutine glue_plural_string: not called CC=1 SCAR=6.9
Subroutine glue_plural_string: not called CC=1 SCAR=6.9
Pod undocumented
Pod undocumented
461 0 my $s = join($Serge::Util::UNIT_SEPARATOR, @variants);
462 # trim unit separators at the end of the resulting string
463 0 $s =~ s/[$UNIT_SEPARATOR]+$//sg;
464 0 return $s;
465 }
466  
467 sub split_plural_string {
468 108 ▶ return split(/$UNIT_SEPARATOR/, shift);
Subroutine split_plural_string: called CC=1 SCAR=1.2
Subroutine split_plural_string: not called CC=1 SCAR=1.2
Pod undocumented
Pod undocumented
469 }
470  
471 sub po_wrap {
472 108 ▶ my ($s) = @_;
Subroutine po_wrap: called CC=2 SCAR=10.0
Subroutine po_wrap: not called CC=2 SCAR=10.0
Pod undocumented
Pod undocumented
473  
474 108 my @lines = wrap($s, $PO_LINE_LENGTH);
475  
476 # Add empty string as the first line to indicate multi-line entry in .po
477  
478 108 ▶ unshift(@lines, '') if scalar(@lines) > 1;
Branch: 2 branches
BranchTrueFalseTotal
if scalar @lines > 1 0 108 108
if scalar @lines > 1 0 0 0
479  
480 108 map { escape_strref(\$_) } @lines;
481  
482 108 return '"'.join(qq|"\n"|, @lines).'"';
483 }
484  
485 sub wrap {
486 108 ▶ my ($s, $length) = @_;
Subroutine wrap: called CC=7 SCAR=20.7
Pod undocumented
487 108 ▶ die "length should be a positive integer" unless $length > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $length > 0 0 108 108
488  
489 # Wrap by '\n' explicitly
490  
491 108 ▶ if ($s =~ m{^(.*?(?:\\n|\n))(.+)$}s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /^(.*?(?:\\n|\n))(.+)$/s) 0 108 108
492 0 my $a = $1; # if $1 and $2 are used directly, this won't work
493 0 my $b = $2;
494 0 return wrap($a, $length), wrap($b, $length);
495 }
496  
497 # The following regexp was taken from the Translate Toolkit, file textwrap.py
498  
499 108 my @a = split(/(\s+|[^\s\w]*\w+[a-zA-Z]-(?=\w+[a-zA-Z])|(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))/, $s);
500  
501 108 my @lines;
502 108 my $line = '';
503 108 ▶ while (scalar(@a) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @a > 0) 262 108 370
504  
505 # Take next chunk
506  
507 262 my $chunk = shift @a;
508  
509 # Treat whitespace chunks as zero-width to avoid starting the line with whitespace
510  
511 262 ▶ my $chunk_length = ($chunk =~ m/^\s*$/) ? 0 : length($chunk);
Branch: 1 branch
BranchTrueFalseTotal
$chunk =~ /^\s*$/ ? : 91 171 262
512  
513 262 ▶ if (length($line) + $chunk_length > $length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($line) + $chunk_length > $length) { } 0 262 262
514 0 push @lines, $line;
515  
516 # We do not handle the situation when chunk by itself is bigger than $length.
517 # We can optionally hard-break such chunks into sub-chunks of exact $length
518 # (this might be an option later)
519  
520 0 $line = $chunk;
521 } else {
522 262 $line .= $chunk;
523 }
524 }
525 108 ▶ push @lines, $line if $line ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $line ne "" 80 28 108
526  
527 108 return @lines;
528 }
529  
530 sub read_and_normalize_file {
531 16 ▶ my ($fname) = @_;
Subroutine read_and_normalize_file: called CC=13 SCAR=44.7
Subroutine read_and_normalize_file: not called CC=13 SCAR=44.7
Pod undocumented
Pod undocumented
532  
533 # Reading the entire file
534  
535 16 ▶ open(SRC, $fname) || die "Can't read [$fname]: $!";
Branch: 2 branches
BranchTrueFalseTotal
open SRC, $fname or die "Can't read [$fname]: $!" 0 16 16
open SRC, $fname or die "Can't read [$fname]: $!" 0 0 0
536 16 binmode(SRC);
537 16 my $data = join('', <SRC>);
538 16 close(SRC);
539  
540 16 my $decoder = Encode::Guess->guess($data);
541 16 ▶ if (ref($decoder)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $decoder) { } 16 0 16
if (ref $decoder) { } 0 0 0
542 16 my $enc = uc($decoder->name);
543  
544 # remove BOM
545 # (not sure why this was done, as BOM is apparently needed for at least UTF-16 decoding;
546 # so I disabled BOM removal for UTF-16 for now)
547 16 ▶ $data =~ s/^\xFF\xFE//s if ($enc eq 'UTF-16LE');
Branch: 2 branches
BranchTrueFalseTotal
if $enc eq "UTF-16LE" 0 16 16
if $enc eq "UTF-16LE" 0 0 0
548 #$data =~ s/^\xFE\xFF//s if (($enc eq 'UTF-16BE') || ($enc eq 'UTF-16'));
549 16 ▶ $data =~ s/^\xFF\xFE\x00\x00//s if ($enc eq 'UTF-32LE');
Branch: 2 branches
BranchTrueFalseTotal
if $enc eq "UTF-32LE" 0 16 16
if $enc eq "UTF-32LE" 0 0 0
550 16 ▶ $data =~ s/^\x00\x00\xFE\xFF//s if (($enc eq 'UTF-32BE') || ($enc eq 'UTF-32'));
Branch: 2 branches
BranchTrueFalseTotal
if $enc eq "UTF-32BE" or $enc eq "UTF-32" 0 16 16
if $enc eq "UTF-32BE" or $enc eq "UTF-32" 0 0 0
Condition16%1/6
$enc eq "UTF-32BE" or $enc eq "UTF-32"
l!l&&r!l&&!r
0016
$enc eq "UTF-32BE" or $enc eq "UTF-32"
l!l&&r!l&&!r
000
MC/DC0%0/2
$enc eq "UTF-32BE" or $enc eq "UTF-32"
$enc eq "UTF-32BE" $enc eq "UTF-32"
MC/DC0%0/2
$enc eq "UTF-32BE" or $enc eq "UTF-32"
$enc eq "UTF-32BE" $enc eq "UTF-32"
Truth table33%1/3
A or B
A= $enc eq "UTF-32BE"B= $enc eq "UTF-32"
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $enc eq "UTF-32BE"B= $enc eq "UTF-32"
ABresult
1X1
011
000
551 16 ▶ $data =~ s/^\xEF\xBB\xBF//s if (($enc eq 'UTF-8') || ($enc eq 'UTF8'));
Branch: 2 branches
BranchTrueFalseTotal
if $enc eq "UTF-8" or $enc eq "UTF8" 0 16 16
if $enc eq "UTF-8" or $enc eq "UTF8" 0 0 0
Condition16%1/6
$enc eq "UTF-8" or $enc eq "UTF8"
l!l&&r!l&&!r
0016
$enc eq "UTF-8" or $enc eq "UTF8"
l!l&&r!l&&!r
000
MC/DC0%0/2
$enc eq "UTF-8" or $enc eq "UTF8"
$enc eq "UTF-8" $enc eq "UTF8"
MC/DC0%0/2
$enc eq "UTF-8" or $enc eq "UTF8"
$enc eq "UTF-8" $enc eq "UTF8"
Truth table33%1/3
A or B
A= $enc eq "UTF-8"B= $enc eq "UTF8"
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $enc eq "UTF-8"B= $enc eq "UTF8"
ABresult
1X1
011
000
552  
553 16 $data = $decoder->decode($data);
554 } else {
555 0 ▶ if ($data =~ m/^<\?xml\s+(.+?)\?>/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data =~ /^<\?xml\s+(.+?)\?>/i) { } 0 0 0
if ($data =~ /^<\?xml\s+(.+?)\?>/i) { } 0 0 0
556 0 my $attrs = $1;
557 0 ▶ if ($attrs =~ m/encoding=['"](.+?)['"]/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($attrs =~ /encoding=['"](.+?)['"]/i) 0 0 0
if ($attrs =~ /encoding=['"](.+?)['"]/i) 0 0 0
558 0 my $enc = uc($1);
559 #print "\t\tEncoding (from XML header): $enc\n";
560  
561 # remove BOM
562 0 ▶ $data =~ s/^\xEF\xBB\xBF//s if (($enc eq 'UTF-8') || ($enc eq 'UTF8'));
Branch: 2 branches
BranchTrueFalseTotal
if $enc eq "UTF-8" or $enc eq "UTF8" 0 0 0
if $enc eq "UTF-8" or $enc eq "UTF8" 0 0 0
Condition0%0/6
$enc eq "UTF-8" or $enc eq "UTF8"
l!l&&r!l&&!r
000
$enc eq "UTF-8" or $enc eq "UTF8"
l!l&&r!l&&!r
000
MC/DC0%0/2
$enc eq "UTF-8" or $enc eq "UTF8"
$enc eq "UTF-8" $enc eq "UTF8"
MC/DC0%0/2
$enc eq "UTF-8" or $enc eq "UTF8"
$enc eq "UTF-8" $enc eq "UTF8"
Truth table0%0/3
A or B
A= $enc eq "UTF-8"B= $enc eq "UTF8"
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $enc eq "UTF-8"B= $enc eq "UTF8"
ABresult
1X1
011
000
563  
564 0 $data = decode($enc, $data);
565 }
566 } else {
567 #print "\t\tEncoding (default): ASCII\n"; # $decoder holds the error string
568 }
569 }
570  
571 16 $data =~ s/\r\n/\n/sg; # normalize line-feeds
572  
573 16 return $data;
574 }
575  
576 sub file_mtime {
577 0 ▶ my ($fname) = @_;
Subroutine file_mtime: not called CC=1 SCAR=1.2
Subroutine file_mtime: not called CC=1 SCAR=1.2
Pod undocumented
Pod undocumented
578  
579 0 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
580 $atime, $mtime, $ctime, $blksize, $blocks) = stat($fname);
581  
582 0 return $mtime;
583 }
584  
585 1 1;
« lib/Serge/Importer.pm ↑ Summary lib/Serge/Engine/Plugin/transform.pm »