<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=koi8-r">
<META content="MSHTML 6.00.6000.16640" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>Че-то я не понял. А вот это тогда
зачем:</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV>
<LI>$flag = utf8::is_utf8(STRING)
<P>(Since Perl 5.8.1) Test whether STRING is in UTF-8 internally. Functionally
the same as Encode::is_utf8().</P></LI>
<LI>$flag = utf8::valid(STRING)
<P>[INTERNAL] Test whether STRING is in a consistent state regarding UTF-8. Will
return true is well-formed UTF-8 and has the UTF-8 flag on <B>or</B> if string
is held as bytes (both these states are 'consistent'). Main reason for this
routine is to allow Perl's testsuite to check that operations have left strings
in a consistent state. You most probably want to use utf8::is_utf8()
instead.</P></LI></DIV>
<BLOCKQUOTE
style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV style="FONT: 10pt arial">----- Original Message ----- </DIV>
<DIV
style="BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: black"><B>From:</B>
<A title=dsimonov@gmail.com href="mailto:dsimonov@gmail.com">Dmitry
Simonov</A> </DIV>
<DIV style="FONT: 10pt arial"><B>To:</B> <A title=moscow-pm@pm.org
href="mailto:moscow-pm@pm.org">Moscow.pm group</A> </DIV>
<DIV style="FONT: 10pt arial"><B>Sent:</B> Wednesday, June 04, 2008 2:02
PM</DIV>
<DIV style="FONT: 10pt arial"><B>Subject:</B> [Moscow.pm] UTF8 ещё раз</DIV>
<DIV><BR></DIV>Озадачился тут вопросом - определить вручную, является ли
входной поток utf8 или не является. Вот в результате получился такой
код:<BR><BR>my $is_this_utf8 = -1;<BR># Обрабатываем входной
поток<BR>while (<$input_file_handle>) {<BR>
chomp;<BR> my $string = $_;<BR> my
@strring_arr = split(//, $string);<BR> my $utf_flag =
0;<BR> next if ($is_this_utf8>0);<BR>
for my $_char (@strring_arr) {<BR>
last if ($is_this_utf8>-1);<BR>
my $char = ord($_char);<BR> # начало
последовательности utf8<BR> if (
$utf_flag==0 && ( 1<<7 | 1<<6 | $char ) == $char )
{<BR>
$utf_flag=1;<BR>
}<BR> # продолжение
последовательности utf8<BR> elsif (
$utf_flag > 0 && $utf_flag<6 && ( 1<<7 | $char )
== $char && ( 1<<6 | $char ) != $char )
{<BR>
$utf_flag++;<BR>
$is_this_utf8 = 1;<BR>
}<BR> # начало новой
последовательности utf8, после предыдущей
последовательности<BR> elsif (
$utf_flag>1 && ( 1<<7 | 1<<6 | $char ) == $char )
{<BR>
$utf_flag=1;<BR>
#$is_this_utf8 = 1;<BR>
}<BR> # символ c опущенными
лидирующими битами<BR> elsif (
$utf_flag==0 && ( 1<<7 | 1<<6 | $char ) != $char )
{<BR>
$utf_flag=0;<BR>
}<BR> # символ c опущенным
лидирующим битом<BR> elsif (
$utf_flag==0 && ( 1<<7 | $char ) != $char )
{<BR>
$utf_flag=0;<BR>
}<BR> # символ после первых двух
байт utf8-последовательности c опущенным лидирующим
битом<BR> elsif ( $utf_flag>1
&& ( 1<<7 | $char ) != $char )
{<BR>
$utf_flag=0;<BR>
}<BR> else
{<BR>
$utf_flag =
0;<BR>
$is_this_utf8 = 0;<BR>
}<BR> };<BR>}<BR><BR><BR>Понятно, что эта конструкция
подлежит упрощению, но хотелось бы понять - насколько сама идея может
считаться грациозной и изящной :)<BR><BR>С уважением ко всем, Д.С.<BR>
<P>
<HR>
<P></P>--<BR>Moscow.pm mailing list<BR>moscow-pm@pm.org |
http://moscow.pm.org<BR></BLOCKQUOTE></BODY></HTML>