创建一个设置了UTF8标志但包含无效UTF8字节序列的perl字符串的好方法是什么?
有没有办法在perl字符串上设置UTF8标志而不对UTF-X转换执行本机编码(例如,当你调用时会发生这种情况utf8::upgrade)?
我需要这样做来追踪DBI驱动程序中可能存在的错误.
您可以通过黑客攻击字符串的内容来设置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);
}
Run Code Online (Sandbox Code Playgroud)
典型输出:
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
Run Code Online (Sandbox Code Playgroud)
这正是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.
Run Code Online (Sandbox Code Playgroud)
(_utf8_on除非你想要生成一个糟糕的标量,否则永远不要使用.)
您可以使用查看损坏
use Devel::Peek qw( Dump );
Dump($s);
Run Code Online (Sandbox Code Playgroud)
输出:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1298 次 |
| 最近记录: |