| 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
Condition33%1/3
$] < "5.006" and not defined &warnings::import
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $_[0] and not $_[0] =~ /\ABATsh::MB\z/
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/
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 8 | ▶ | $enc = 'auto' unless defined $enc && $enc ne ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $enc and $enc ne ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 8 | my $canon = $ALIAS{lc $enc}; | |||||||||||||||||||||||||||||||||||||||||
| 156 | 8 | ▶ | unless (defined $canon) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $_[0] and not $_[0] =~ /\ABATsh::MB\z/
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/
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | 548 | ▶ | return 'none' unless defined $src; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 548 | ▶ | return 'none' unless $src =~ /[\x80-\xff]/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | 2 | ▶ | return _valid_utf8($src) ? 'utf8' : 'cp932'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | 14 | my $o = ord(substr($s, $i, 1)); | |||||||||||||||||||||||||||||||||||||||||
| 207 | 14 | ▶ | if ($o < 0x80) { $i++ } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 208 | ▶ | elsif ($o >= 0xC2 && $o <= 0xDF) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$o >= 194 and $o <= 223
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
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | return 0 unless _cont($s, $i + 1, 1); $i += 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||
| 211 | elsif ($o == 0xE0) { | ||||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | return 0 unless _rng($s, $i+1, 0xA0, 0xBF) && _cont($s, $i+2, 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
_rng($s, $i + 1, 160, 191) and _cont($s, $i + 2, 1)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | $i += 3; | |||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||
| 215 | ▶ | elsif ($o >= 0xE1 && $o <= 0xEF) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$o >= 225 and $o <= 239
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
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 2 | ▶ | return 0 unless _cont($s, $i + 1, 2); $i += 3; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | elsif ($o == 0xF0) { | ||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | ▶ | return 0 unless _rng($s, $i+1, 0x90, 0xBF) && _cont($s, $i+2, 2); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
_rng($s, $i + 1, 144, 191) and _cont($s, $i + 2, 2)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | $i += 4; | |||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||
| 222 | ▶ | elsif ($o >= 0xF1 && $o <= 0xF4) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$o >= 241 and $o <= 244
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
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | ▶ | return 0 unless _cont($s, $i + 1, 3); $i += 4; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | 4 | my $o = ord(substr($s, $i + $k, 1)); | |||||||||||||||||||||||||||||||||||||||||
| 235 | 4 | ▶ | return 0 unless $o >= 0x80 && $o <= 0xBF; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$o >= 128 and $o <= 191
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | my $o = ord(substr($s, $i, 1)); | |||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | ▶ | return ($o >= $lo && $o <= $hi) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$o >= $lo and $o <= $hi
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $_[0] and not $_[0] =~ /\ABATsh::MB\z/
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/
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | 564 | ▶ | if ($ENCODING eq 'auto') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 548 | my $det = detect($src); | |||||||||||||||||||||||||||||||||||||||||
| 257 | 548 | ▶ | _activate($det) if exists $ENC_TABLE{$det}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$BATsh::MB::ACTIVE and defined $s
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
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | 42 | ▶ | return $s unless $s =~ /[\x01\x81-\xfe]/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$BATsh::MB::ACTIVE and defined $s
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
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | 68 | ▶ | return $s unless index($s, "\x01") >= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 290 | 27 | $s =~ s/\x01(\x01|[$LEAD][\xc0-\xfe])/ | |||||||||||||||||||||||||||||||||||||||||
| 291 | 58 | ▶ | $1 eq "\x01" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | 13 | ▶ | return length($s) unless $ACTIVE; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 319 | 9 | ▶ | unless ($ACTIVE) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 320 | 7 | ▶ | return defined $len ? substr($s, $off, $len) : substr($s, $off); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 325 | 2 | ▶ | $off = 0 if $off < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 326 | 2 | ▶ | return '' if $off >= $n; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 327 | 2 | ▶ | my $end = defined $len ? $off + $len : $n; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 328 | 2 | ▶ | $end = $n if $end > $n; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 329 | 2 | ▶ | return '' if $end <= $off; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 342 | 12 | my $b = substr($s, $i, 1); | |||||||||||||||||||||||||||||||||||||||||
| 343 | 12 | ▶ | if ($b =~ /[$LEAD]/ && $i + 1 < $n | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$b =~ /[$BATsh::MB::LEAD]/ and $i + 1 < $n and substr($s, $i + 1, 1) =~ /[$BATsh::MB::TRAIL]/
$b =~ /[$BATsh::MB::LEAD]/ and $i + 1 < $n
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]/
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $_[0] and not $_[0] =~ /\ABATsh::MB\z/
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/
| |||||||||||||||||||||||||||||||||||||||||||
| 363 | 554 | ▶ | return $s unless defined $s; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 364 | 554 | $s =~ s/\A\xef\xbb\xbf//; | |||||||||||||||||||||||||||||||||||||||||
| 365 | 554 | return $s; | |||||||||||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||
| 368 | 64 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||
| 370 | __END__ | ||||||||||||||||||||||||||||||||||||||||||