创建一个设置了UTF8标志但包含无效UTF8字节序列的perl字符串有什么好方法?
有没有办法在perl字符串上设置UTF8标志而不对UTF-X转换执行本机编码(例如,当你调用utf8::upgrade
时会发生这种情况)?
我需要这样做来追踪DBI驱动程序中可能存在的错误。
答案 0 :(得分:8)
您可以通过黑客攻击字符串的内容来设置UTF8标志的任意字节序列。
use Inline C;
use Devel::Peek;
utf8::upgrade( $str = "" );
Dump($str);
twiddle($str, "\x{BD}\x{BE}\x{BF}\x{C0}\x{C1}\x{C2}");
Dump($str);
__DATA__
__C__
/** append arbitrary bytes to a Perl scalar **/
void twiddle(SV *s, const char *t)
{
sv_catpv(s, t);
}
典型输出:
SV = PV(0x80029bb0) at 0x80072008
REFCNT = 1
FLAGS = (POK,pPOK,UTF8)
PV = 0x80155098 ""\0 [UTF8 ""]
CUR = 0
LEN = 12
SV = PV(0x80029bb0) at 0x80072008
REFCNT = 1
FLAGS = (POK,pPOK,UTF8)
PV = 0x80155098 "\275\276\277\300\301\302"\0Malformed UTF-8 character (unexpected continuation byte 0xbd, with no preceding start byte) in subroutine entry at ./invalidUTF.pl line 6.
Malformed UTF-8 character (unexpected continuation byte 0xbe, with no preceding start byte) in subroutine entry at ./invalidUTF.pl line 6.
Malformed UTF-8 character (unexpected continuation byte 0xbf, with no preceding start byte) in subroutine entry at ./invalidUTF.pl line 6.
Malformed UTF-8 character (unexpected non-continuation byte 0xc1, immediately after start byte 0xc0) in subroutine entry at ./invalidUTF.pl line 6.
Malformed UTF-8 character (unexpected non-continuation byte 0x00, immediately after start byte 0xc2) in subroutine entry at ./invalidUTF.pl line 6.
[UTF8 "\x{0}\x{0}\x{0}\x{0}\x{0}"]
CUR = 6
LEN = 12
答案 1 :(得分:7)
这正是Encode' _utf8_on
所做的。
use Encode qw( _utf8_on );
my $s = "abc\xC0def"; # String to use as raw buffer content.
utf8::downgrade($s); # Make sure each char is stored as a byte.
_utf8_on($s); # Set UTF8 flag.
(除非您想要生成错误的标量,否则切勿使用_utf8_on
。)
您可以使用
查看损坏情况use Devel::Peek qw( Dump );
Dump($s);
输出:
SV = PV(0x24899c) at 0x4a9294
REFCNT = 1
FLAGS = (PADMY,POK,pPOK,UTF8)
PV = 0x24ab04 "abc\300def"\0Malformed UTF-8 character (unexpected non-continuation byte 0x64, immediately after start byte 0xc0) in subroutine entry at script.pl line 9.
[UTF8 "abc\x{0}ef"]
CUR = 7
LEN = 12