lib/BATsh/MB.pm

filter: Statementstmt 87.7% 122 / 139 Branchbran 63.4% 66 / 104 Conditioncond 35.2% 18 / 51 MC/DCmcdc 9.0% 3 / 33 Subroutinesub 91.3% 21 / 23 Podpod 60.0% 6 / 10 totaltotal 65.5% 236 / 360 CC 71 SCAR 29.1
CC
71
Cov
70%
_valid_utf8
67.5
_rng
20.0
mb_substr
11.0
CRAP 18.4
« lib/BATsh.pm ↑ Summary lib/BATsh/Env.pm »
1 package BATsh::MB;
2 ######################################################################
3 #
4 # BATsh::MB - Multibyte (DBCS) script guard for BATsh
5 #
6 # https://metacpan.org/dist/BATsh
7 #
8 # Copyright (c) 2026 INABA Hitoshi <ina.cpan@gmail.com>
9 #
10 ######################################################################
11 #
12 # THE PROBLEM (the classic "0x5C problem" of CP932 aka DAME-MOJI)
13 #
14 # In double-byte character sets such as CP932 (Shift_JIS as used on
15 # Japanese Windows), the SECOND byte of a two-byte character may fall
16 # in the ASCII range 0x40-0x7E. That range contains shell
17 # metacharacters:
18 #
19 # \ 0x5C escape character / path separator (SO, HYOU, NOH, ...)
20 # | 0x7C pipeline separator (PO, BO, ...)
21 # ` 0x60 command substitution (CHI, ...)
22 # ^ 0x5E cmd.exe escape character (DA, ...)
23 # { 0x7B } 0x7D [ 0x5B ] 0x5D ~ 0x7E @ 0x40
24 # A-Z a-z (corrupted by uc/lc on variable names and values)
25 #
26 # A byte-oriented parser that scans for these characters will tear a
27 # two-byte character in half: "ECHO <SO>FUTO" loses a byte to caret
28 # unescaping, "echo <PO>INTO | cmd" splits the pipeline in the middle
29 # of a character, and so on.
30 #
31 # THE SOLUTION (guard transform)
32 #
33 # Rather than teaching every scanner in BATsh::CMD / BATsh::SH about
34 # lead and trail bytes, the script text is passed through a reversible
35 # GUARD TRANSFORM on input:
36 #
37 # LEAD TRAIL (TRAIL in the dangerous 0x40-0x7E range)
38 # is rewritten to
39 # \x01 LEAD (TRAIL+0x80) (three bytes, no ASCII metacharacters)
40 #
41 # a literal \x01 byte (never present in real scripts) is rewritten
42 # to \x01\x01 so that the transform is bijective.
43 #
44 # Two-byte characters whose trail byte is already >= 0x80 are left
45 # unchanged (they contain no ASCII bytes and are harmless). After the
46 # transform, NO byte of any multibyte character is an ASCII
47 # metacharacter, so every existing byte-oriented scanner in BATsh is
48 # automatically DBCS-safe. The inverse transform (decode) is applied
49 # at the output boundaries: print, external command execution,
50 # filesystem calls, and %ENV export.
51 #
52 # Because uc()/lc() never modify bytes >= 0x80 and the guard moves all
53 # ASCII trail bytes up into 0xC0-0xFE, case conversion of guarded text
54 # (variable-name uppercasing in BATsh::Env, ${VAR^^}, ...) is also
55 # safe as a side effect.
56 #
57 # SUPPORTED ENCODINGS
58 #
59 # cp932 (aliases: sjis, shiftjis, shift_jis, shift-jis, 932)
60 # gbk (aliases: cp936, 936)
61 # uhc (aliases: cp949, ksc5601, 949)
62 # big5 (aliases: cp950, 950)
63 # utf8 (aliases: utf-8) -- pass-through: UTF-8 trail bytes are
64 # all >= 0x80 and need no guarding
65 # none (aliases: ascii, us-ascii, binary) -- pass-through
66 # auto -- detect from the script source (see detect() below)
67 #
68 ######################################################################
69  
70 64 ▶use 5.00503;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 64 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 0 ▶BEGIN { if ($] < 5.006 && !defined(&warnings::import)) { $INC{'warnings.pm'} = 'stub'; eval 'package warnings; sub import {}' } }
Subroutine BEGIN: called CC=3 SCAR=16.5
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.006" and not defined &warnings::import) 0 64 64
Condition33%1/3
$] < "5.006" and not defined &warnings::import
!ll&&!rl&&r
6400
MC/DC0%0/2
$] < "5.006" and not defined &warnings::import
$] < "5.006" not defined &warnings::import
Truth table33%1/3
A and not(B)
A= $] < "5.006"B= not defined &warnings::import
ABresult
0X0
100
111
73 64 ▶use warnings; local $^W = 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 64 ▶BEGIN { pop @INC if $INC[-1] eq '.' }
Subroutine BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $INC[-1] eq "." 0 64 64
75  
76 64 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
77 64 $VERSION = '0.11';
78 64 $VERSION = $VERSION;
79  
80 64 ▶use vars qw($ENCODING $ACTIVE $ACTIVE_ENC $LEAD $TRAIL $DANGER);
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 # Requested encoding ('auto' until told otherwise) and the currently
83 # ACTIVE guard. Under 'auto' the guard is switched on when a DBCS
84 # source is detected and is then STICKY: a later pure-ASCII run_string
85 # does not switch it off, because Env may still hold guarded values
86 # from the earlier script. Only an explicit set_encoding() call
87 # deactivates the guard.
88 64 $ENCODING = 'auto';
89 64 $ACTIVE = 0;
90 64 $ACTIVE_ENC = '';
91  
92 # Per-encoding byte classes (as character-class body strings).
93 # lead : first byte of a two-byte character
94 # trail : any valid second byte
95 # danger : second bytes that are also ASCII metacharacter candidates
96 64 my %ENC_TABLE = (
97 'cp932' => {
98 lead => "\x81-\x9f\xe0-\xfc",
99 trail => "\x40-\x7e\x80-\xfc",
100 danger => "\x40-\x7e",
101 },
102 'gbk' => {
103 lead => "\x81-\xfe",
104 trail => "\x40-\x7e\x80-\xfe",
105 danger => "\x40-\x7e",
106 },
107 'uhc' => {
108 lead => "\x81-\xfe",
109 trail => "\x41-\x5a\x61-\x7a\x81-\xfe",
110 danger => "\x41-\x5a\x61-\x7a",
111 },
112 'big5' => {
113 lead => "\x81-\xfe",
114 trail => "\x40-\x7e\xa1-\xfe",
115 danger => "\x40-\x7e",
116 },
117 );
118  
119 64 my %ALIAS = (
120 'cp932' => 'cp932',
121 'sjis' => 'cp932',
122 'shiftjis' => 'cp932',
123 'shift_jis' => 'cp932',
124 'shift-jis' => 'cp932',
125 '932' => 'cp932',
126 'gbk' => 'gbk',
127 'cp936' => 'gbk',
128 '936' => 'gbk',
129 'uhc' => 'uhc',
130 'cp949' => 'uhc',
131 'ksc5601' => 'uhc',
132 '949' => 'uhc',
133 'big5' => 'big5',
134 'cp950' => 'big5',
135 '950' => 'big5',
136 'utf8' => 'utf8',
137 'utf-8' => 'utf8',
138 'none' => 'none',
139 'ascii' => 'none',
140 'us-ascii' => 'none',
141 'binary' => 'none',
142 'auto' => 'auto',
143 );
144  
145 # ----------------------------------------------------------------
146 # set_encoding: select the requested encoding
147 # 'auto' detect per script (sticky activation)
148 # a DBCS name activate the guard now
149 # 'utf8'/'none' deactivate the guard now
150 # Returns the canonical name; dies on an unknown encoding.
151 # ----------------------------------------------------------------
152 8 ▶sub set_encoding {
Subroutine set_encoding: called CC=8 SCAR=21.6
Pod documented
153 8 ▶ my $enc = defined $_[0] && $_[0] !~ /\ABATsh::MB\z/ ? $_[0] : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && !($_[0] =~ /\ABATsh::MB\z/) ? : 8 0 8
Condition33%1/3
defined $_[0] and not $_[0] =~ /\ABATsh::MB\z/
!ll&&!rl&&r
008
MC/DC0%0/2
defined $_[0] and not $_[0] =~ /\ABATsh::MB\z/
defined $_[0] not $_[0] =~ /\ABATsh::MB\z/
Truth table33%1/3
A and not(B)
A= defined $_[0]B= not $_[0] =~ /\ABATsh::MB\z/
ABresult
0X0
100
111
154 8 ▶ $enc = 'auto' unless defined $enc && $enc ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $enc and $enc ne "" 0 8 8
Condition33%1/3
defined $enc and $enc ne ""
!ll&&!rl&&r
008
MC/DC0%0/2
defined $enc and $enc ne ""
defined $enc $enc ne ""
Truth table33%1/3
A and B
A= defined $encB= $enc ne ""
ABresult
0X0
100
111
155 8 my $canon = $ALIAS{lc $enc};
156 8 ▶ unless (defined $canon) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $canon) 1 7 8
157 1 require Carp;
158 1 Carp::croak("BATsh::MB: unknown encoding '$enc' (supported: " .
159 "cp932 sjis gbk uhc big5 utf8 none auto)");
160 }
161 7 $ENCODING = $canon;
162 7 ▶ if ($canon eq 'auto') { } # sticky
Branch: 2 branches
BranchTrueFalseTotal
if ($canon eq 'auto') { } 2 5 7
elsif (exists $ENC_TABLE{$canon}) { } 3 2 5
163 3 elsif (exists $ENC_TABLE{$canon}) { _activate($canon) }
164 2 else { _deactivate() } # utf8/none
165 7 return $canon;
166 }
167  
168 0 ▶sub encoding { return $ENCODING }
Subroutine encoding: not called CC=1 SCAR=6.9
Pod undocumented
169 556 ▶sub active { return $ACTIVE ? $ACTIVE_ENC : '' }
Subroutine active: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$BATsh::MB::ACTIVE ? : 18 538 556
170  
171 4 ▶sub _activate {
Subroutine _activate: called CC=1 SCAR=0.0
172 4 my ($canon) = @_;
173 4 my $t = $ENC_TABLE{$canon};
174 4 $LEAD = $t->{lead};
175 4 $TRAIL = $t->{trail};
176 4 $DANGER = $t->{danger};
177 4 $ACTIVE = 1;
178 4 $ACTIVE_ENC = $canon;
179 }
180  
181 2 ▶sub _deactivate {
Subroutine _deactivate: called CC=1 SCAR=0.0
182 2 $ACTIVE = 0;
183 2 $ACTIVE_ENC = '';
184 }
185  
186 # ----------------------------------------------------------------
187 # detect: guess the encoding of a chunk of script source (bytes)
188 # no bytes >= 0x80 -> 'none'
189 # well-formed UTF-8 -> 'utf8'
190 # otherwise -> 'cp932'
191 # The UTF-8 test is structural (RFC 3629 sequence forms), written
192 # byte-wise so it runs identically from Perl 5.005_03 onward.
193 # ----------------------------------------------------------------
194 548 ▶sub detect {
Subroutine detect: called CC=6 SCAR=19.0
Pod documented
195 548 ▶ my $src = defined $_[0] && $_[0] !~ /\ABATsh::MB\z/ ? $_[0] : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && !($_[0] =~ /\ABATsh::MB\z/) ? : 548 0 548
Condition33%1/3
defined $_[0] and not $_[0] =~ /\ABATsh::MB\z/
!ll&&!rl&&r
00548
MC/DC0%0/2
defined $_[0] and not $_[0] =~ /\ABATsh::MB\z/
defined $_[0] not $_[0] =~ /\ABATsh::MB\z/
Truth table33%1/3
A and not(B)
A= defined $_[0]B= not $_[0] =~ /\ABATsh::MB\z/
ABresult
0X0
100
111
196 548 ▶ return 'none' unless defined $src;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $src 0 548 548
197 548 ▶ return 'none' unless $src =~ /[\x80-\xff]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $src =~ /[\x80-\xff]/ 546 2 548
198 2 ▶ return _valid_utf8($src) ? 'utf8' : 'cp932';
Branch: 1 branch
BranchTrueFalseTotal
_valid_utf8($src) ? : 1 1 2
199 }
200  
201 2 ▶sub _valid_utf8 {
Subroutine _valid_utf8: called CC=18 SCAR=42.1
202 2 my ($s) = @_;
203 2 my $i = 0;
204 2 my $n = length($s);
205 2 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 14 1 15
206 14 my $o = ord(substr($s, $i, 1));
207 14 ▶ if ($o < 0x80) { $i++ }
Branch: 6 branches
BranchTrueFalseTotal
if ($o < 128) { } 11 3 14
elsif ($o >= 194 and $o <= 223) { } 0 3 3
elsif ($o == 224) { } 0 3 3
elsif ($o >= 225 and $o <= 239) { } 2 1 3
elsif ($o == 240) { } 0 1 1
elsif ($o >= 241 and $o <= 244) { } 0 1 1
208 ▶ elsif ($o >= 0xC2 && $o <= 0xDF) {
Condition66%2/3
$o >= 194 and $o <= 223
!ll&&!rl&&r
120
MC/DC0%0/2
$o >= 194 and $o <= 223
$o >= 194 $o <= 223
Truth table66%2/3
A and B
A= $o >= 194B= $o <= 223
ABresult
0X0
100
111
209 0 ▶ return 0 unless _cont($s, $i + 1, 1); $i += 2;
Branch: 1 branch
BranchTrueFalseTotal
unless _cont($s, $i + 1, 1) 0 0 0
210 }
211 elsif ($o == 0xE0) {
212 0 ▶ return 0 unless _rng($s, $i+1, 0xA0, 0xBF) && _cont($s, $i+2, 1);
Branch: 1 branch
BranchTrueFalseTotal
unless _rng($s, $i + 1, 160, 191) and _cont($s, $i + 2, 1) 0 0 0
Condition0%0/3
_rng($s, $i + 1, 160, 191) and _cont($s, $i + 2, 1)
!ll&&!rl&&r
000
MC/DC0%0/2
_rng($s, $i + 1, 160, 191) and _cont($s, $i + 2, 1)
_rng($s, $i + 1, 160, 191) _cont($s, $i + 2, 1)
Truth table0%0/3
A and B
A= _rng($s, $i + 1, 160, 191)B= _cont($s, $i + 2, 1)
ABresult
0X0
100
111
213 0 $i += 3;
214 }
215 ▶ elsif ($o >= 0xE1 && $o <= 0xEF) {
Condition66%2/3
$o >= 225 and $o <= 239
!ll&&!rl&&r
102
MC/DC50%1/2
$o >= 225 and $o <= 239
$o >= 225 $o <= 239
Truth table66%2/3
A and B
A= $o >= 225B= $o <= 239
ABresult
0X0
100
111
216 2 ▶ return 0 unless _cont($s, $i + 1, 2); $i += 3;
Branch: 1 branch
BranchTrueFalseTotal
unless _cont($s, $i + 1, 2) 0 2 2
217 }
218 elsif ($o == 0xF0) {
219 0 ▶ return 0 unless _rng($s, $i+1, 0x90, 0xBF) && _cont($s, $i+2, 2);
Branch: 1 branch
BranchTrueFalseTotal
unless _rng($s, $i + 1, 144, 191) and _cont($s, $i + 2, 2) 0 0 0
Condition0%0/3
_rng($s, $i + 1, 144, 191) and _cont($s, $i + 2, 2)
!ll&&!rl&&r
000
MC/DC0%0/2
_rng($s, $i + 1, 144, 191) and _cont($s, $i + 2, 2)
_rng($s, $i + 1, 144, 191) _cont($s, $i + 2, 2)
Truth table0%0/3
A and B
A= _rng($s, $i + 1, 144, 191)B= _cont($s, $i + 2, 2)
ABresult
0X0
100
111
220 0 $i += 4;
221 }
222 ▶ elsif ($o >= 0xF1 && $o <= 0xF4) {
Condition33%1/3
$o >= 241 and $o <= 244
!ll&&!rl&&r
100
MC/DC0%0/2
$o >= 241 and $o <= 244
$o >= 241 $o <= 244
Truth table33%1/3
A and B
A= $o >= 241B= $o <= 244
ABresult
0X0
100
111
223 0 ▶ return 0 unless _cont($s, $i + 1, 3); $i += 4;
Branch: 1 branch
BranchTrueFalseTotal
unless _cont($s, $i + 1, 3) 0 0 0
224 }
225 1 else { return 0 }
226 }
227 1 return 1;
228 }
229  
230 2 ▶sub _cont {
Subroutine _cont: called CC=5 SCAR=17.5
231 2 my ($s, $i, $count) = @_;
232 2 for my $k (0 .. $count - 1) {
233 4 ▶ return 0 if $i + $k >= length($s);
Branch: 1 branch
BranchTrueFalseTotal
if $i + $k >= length $s 0 4 4
234 4 my $o = ord(substr($s, $i + $k, 1));
235 4 ▶ return 0 unless $o >= 0x80 && $o <= 0xBF;
Branch: 1 branch
BranchTrueFalseTotal
unless $o >= 128 and $o <= 191 0 4 4
Condition33%1/3
$o >= 128 and $o <= 191
!ll&&!rl&&r
004
MC/DC0%0/2
$o >= 128 and $o <= 191
$o >= 128 $o <= 191
Truth table33%1/3
A and B
A= $o >= 128B= $o <= 191
ABresult
0X0
100
111
236 }
237 2 return 1;
238 }
239  
240 0 ▶sub _rng {
Subroutine _rng: not called CC=4 SCAR=30.0
241 0 my ($s, $i, $lo, $hi) = @_;
242 0 ▶ return 0 if $i >= length($s);
Branch: 1 branch
BranchTrueFalseTotal
if $i >= length $s 0 0 0
243 0 my $o = ord(substr($s, $i, 1));
244 0 ▶ return ($o >= $lo && $o <= $hi) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$o >= $lo && $o <= $hi ? : 0 0 0
Condition0%0/3
$o >= $lo and $o <= $hi
!ll&&!rl&&r
000
MC/DC0%0/2
$o >= $lo and $o <= $hi
$o >= $lo $o <= $hi
Truth table0%0/3
A and B
A= $o >= $loB= $o <= $hi
ABresult
0X0
100
111
245 }
246  
247 # ----------------------------------------------------------------
248 # activate_for: called by BATsh with the raw script source before it
249 # is executed. Under 'auto', detection may switch the guard ON (and
250 # it then stays on -- see the stickiness note above). Under an
251 # explicit encoding the guard state is already fixed by set_encoding.
252 # ----------------------------------------------------------------
253 564 ▶sub activate_for {
Subroutine activate_for: called CC=5 SCAR=16.6
Pod undocumented
254 564 ▶ my $src = defined $_[0] && $_[0] !~ /\ABATsh::MB\z/ ? $_[0] : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && !($_[0] =~ /\ABATsh::MB\z/) ? : 564 0 564
Condition33%1/3
defined $_[0] and not $_[0] =~ /\ABATsh::MB\z/
!ll&&!rl&&r
00564
MC/DC0%0/2
defined $_[0] and not $_[0] =~ /\ABATsh::MB\z/
defined $_[0] not $_[0] =~ /\ABATsh::MB\z/
Truth table33%1/3
A and not(B)
A= defined $_[0]B= not $_[0] =~ /\ABATsh::MB\z/
ABresult
0X0
100
111
255 564 ▶ if ($ENCODING eq 'auto') {
Branch: 1 branch
BranchTrueFalseTotal
if ($BATsh::MB::ENCODING eq "auto") 548 16 564
256 548 my $det = detect($src);
257 548 ▶ _activate($det) if exists $ENC_TABLE{$det};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENC_TABLE{$det} 1 547 548
258 }
259 564 return $ACTIVE;
260 }
261  
262 # ----------------------------------------------------------------
263 # enc: apply the guard transform (raw DBCS bytes -> guarded form)
264 # dec: remove the guard transform (guarded form -> raw DBCS bytes)
265 # Both are identity functions while the guard is inactive, so that
266 # ASCII and UTF-8 operation is entirely unaffected.
267 # ----------------------------------------------------------------
268 360 ▶sub enc {
Subroutine enc: called CC=5 SCAR=16.2
Pod documented
269 360 my ($s) = @_;
270 360 ▶ return $s unless $ACTIVE && defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless $BATsh::MB::ACTIVE and defined $s 318 42 360
Condition66%2/3
$BATsh::MB::ACTIVE and defined $s
!ll&&!rl&&r
318042
MC/DC50%1/2
$BATsh::MB::ACTIVE and defined $s
$BATsh::MB::ACTIVE defined $s
Truth table66%2/3
A and B
A= $BATsh::MB::ACTIVEB= defined $s
ABresult
0X0
100
111
271 42 ▶ return $s unless $s =~ /[\x01\x81-\xfe]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $s =~ /[\x01\x81-\xfe]/ 16 26 42
272 26 $s =~ s/\x01/\x01\x01/g;
273 # Deliberately NOT /o: the classes change when the active encoding
274 # changes, so the substitution must be recompiled per call.
275 26 $s =~ s/([$LEAD])([$TRAIL])/
276 53 do {
277 53 my ($lb, $tb) = ($1, $2);
278 53 ▶ $tb =~ m<\A[$DANGER]\z>
Branch: 1 branch
BranchTrueFalseTotal
$tb =~ /\A[$BATsh::MB::DANGER]\z/ ? : 53 0 53
279 ? "\x01" . $lb . chr(ord($tb) + 0x80)
280 : $lb . $tb
281 }
282 /gex;
283 26 return $s;
284 }
285  
286 2618 ▶sub dec {
Subroutine dec: called CC=5 SCAR=16.1
Pod documented
287 2618 my ($s) = @_;
288 2618 ▶ return $s unless $ACTIVE && defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless $BATsh::MB::ACTIVE and defined $s 2550 68 2618
Condition66%2/3
$BATsh::MB::ACTIVE and defined $s
!ll&&!rl&&r
2550068
MC/DC50%1/2
$BATsh::MB::ACTIVE and defined $s
$BATsh::MB::ACTIVE defined $s
Truth table66%2/3
A and B
A= $BATsh::MB::ACTIVEB= defined $s
ABresult
0X0
100
111
289 68 ▶ return $s unless index($s, "\x01") >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless index($s, "\1") != -1 41 27 68
290 27 $s =~ s/\x01(\x01|[$LEAD][\xc0-\xfe])/
291 58 ▶ $1 eq "\x01"
Branch: 1 branch
BranchTrueFalseTotal
$1 eq "\cA" ? : 1 57 58
292 ? "\x01"
293 : substr($1, 0, 1) . chr(ord(substr($1, 1, 1)) - 0x80)
294 /gex;
295 27 return $s;
296 }
297  
298 # ----------------------------------------------------------------
299 # Character-oriented helpers, operating on GUARDED strings.
300 # While the guard is inactive they fall back to byte semantics, so
301 # existing ASCII behaviour is preserved bit-for-bit.
302 #
303 # mb_length($guarded) -> number of characters
304 # mb_substr($guarded, $off, $len) -> guarded substring by characters
305 # $off may be negative (counts back from the end, like substr);
306 # $len may be omitted (rest of string).
307 # ----------------------------------------------------------------
308 13 ▶sub mb_length {
Subroutine mb_length: called CC=3 SCAR=11.0
Pod documented
309 13 my ($s) = @_;
310 13 ▶ return 0 unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 13 13
311 13 ▶ return length($s) unless $ACTIVE;
Branch: 1 branch
BranchTrueFalseTotal
unless $BATsh::MB::ACTIVE 11 2 13
312 2 my $chars = _mb_chars(dec($s));
313 2 return scalar @{$chars};
314 }
315  
316 9 ▶sub mb_substr {
Subroutine mb_substr: called CC=10 SCAR=23.9
Pod documented
317 9 my ($s, $off, $len) = @_;
318 9 ▶ return '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 9 9
319 9 ▶ unless ($ACTIVE) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($BATsh::MB::ACTIVE) 7 2 9
320 7 ▶ return defined $len ? substr($s, $off, $len) : substr($s, $off);
Branch: 1 branch
BranchTrueFalseTotal
defined $len ? : 5 2 7
321 }
322 2 my $chars = _mb_chars(dec($s));
323 2 my $n = scalar @{$chars};
324 2 ▶ $off = $n + $off if $off < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $off < 0 0 2 2
325 2 ▶ $off = 0 if $off < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $off < 0 0 2 2
326 2 ▶ return '' if $off >= $n;
Branch: 1 branch
BranchTrueFalseTotal
if $off >= $n 0 2 2
327 2 ▶ my $end = defined $len ? $off + $len : $n;
Branch: 1 branch
BranchTrueFalseTotal
defined $len ? : 2 0 2
328 2 ▶ $end = $n if $end > $n;
Branch: 1 branch
BranchTrueFalseTotal
if $end > $n 0 2 2
329 2 ▶ return '' if $end <= $off;
Branch: 1 branch
BranchTrueFalseTotal
if $end <= $off 0 2 2
330 2 return enc(join('', @{$chars}[$off .. $end - 1]));
331 }
332  
333 # Split RAW (decoded) DBCS bytes into characters, forward-scanning:
334 # a lead byte followed by a valid trail byte forms one two-byte
335 # character; anything else is a single byte.
336 4 ▶sub _mb_chars {
Subroutine _mb_chars: called CC=5 SCAR=17.6
337 4 my ($s) = @_;
338 4 my @c = ();
339 4 my $i = 0;
340 4 my $n = length($s);
341 4 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 12 4 16
342 12 my $b = substr($s, $i, 1);
343 12 ▶ if ($b =~ /[$LEAD]/ && $i + 1 < $n
Branch: 1 branch
BranchTrueFalseTotal
if ($b =~ /[$BATsh::MB::LEAD]/ and $i + 1 < $n and substr($s, $i + 1, 1) =~ /[$BATsh::MB::TRAIL]/) { } 12 0 12
Condition33%2/6
$b =~ /[$BATsh::MB::LEAD]/ and $i + 1 < $n and substr($s, $i + 1, 1) =~ /[$BATsh::MB::TRAIL]/
!ll&&!rl&&r
0012
$b =~ /[$BATsh::MB::LEAD]/ and $i + 1 < $n
!ll&&!rl&&r
0012
MC/DC0%0/3
$b =~ /[$BATsh::MB::LEAD]/ and $i + 1 < $n and substr($s, $i + 1, 1) =~ /[$BATsh::MB::TRAIL]/
$b =~ /[$BATsh::MB::LEAD]/ $i + 1 < $n substr($s, $i + 1, 1) =~ /[$BATsh::MB::TRAIL]/
Truth table25%1/4
A and B and C
A= $b =~ /[$BATsh::MB::LEAD]/B= $i + 1 < $nC= substr($s, $i + 1, 1) =~ /[$BATsh::MB::TRAIL]/
ABCresult
0XX0
10X0
1100
1111
344 && substr($s, $i + 1, 1) =~ /[$TRAIL]/) {
345 12 push @c, substr($s, $i, 2);
346 12 $i += 2;
347 }
348 else {
349 0 push @c, $b;
350 0 $i++;
351 }
352 }
353 4 return [ @c ];
354 }
355  
356 # ----------------------------------------------------------------
357 # strip_bom: remove a UTF-8 byte-order mark from the head of the
358 # first script line (harmless everywhere, required for editors that
359 # save UTF-8-with-BOM).
360 # ----------------------------------------------------------------
361 554 ▶sub strip_bom {
Subroutine strip_bom: called CC=4 SCAR=15.6
Pod undocumented
362 554 ▶ my $s = defined $_[0] && $_[0] !~ /\ABATsh::MB\z/ ? $_[0] : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && !($_[0] =~ /\ABATsh::MB\z/) ? : 554 0 554
Condition33%1/3
defined $_[0] and not $_[0] =~ /\ABATsh::MB\z/
!ll&&!rl&&r
00554
MC/DC0%0/2
defined $_[0] and not $_[0] =~ /\ABATsh::MB\z/
defined $_[0] not $_[0] =~ /\ABATsh::MB\z/
Truth table33%1/3
A and not(B)
A= defined $_[0]B= not $_[0] =~ /\ABATsh::MB\z/
ABresult
0X0
100
111
363 554 ▶ return $s unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 554 554
364 554 $s =~ s/\A\xef\xbb\xbf//;
365 554 return $s;
366 }
367  
368 64 1;
369  
370 __END__
« lib/BATsh.pm ↑ Summary lib/BATsh/Env.pm »