7
0
forked from pool/perl-XML-SAX
Files
perl-XML-SAX/perl-XML-SAX-0.96-utf8.diff
Dirk Stoecker 003b06095b Accepting request 710373 from home:pmonrealgonzalez:branches:devel:languages:perl
- update to 1.02
   see /usr/share/doc/packages/perl-XML-SAX/Changes
  1.02  14 Jun 2019  Grant McLean
      - Spelling fixes (patch from Ville Skyttä)
      - Add repo location to metadata (patches from Ville Skytta & Martin McGrath)
      - Reorganise module files under lib/XML
      - Regenerate MANIFEST using 'make manifest' to include missing test files
- Updated URL to MetaCPAN
- Rebased perl-XML-SAX-0.96-utf8.diff

OBS-URL: https://build.opensuse.org/request/show/710373
OBS-URL: https://build.opensuse.org/package/show/devel:languages:perl/perl-XML-SAX?expand=0&rev=22
2019-06-17 17:16:56 +00:00

58 lines
1.7 KiB
Diff

--- lib/XML/SAX/PurePerl/Reader/Stream.pm
+++ lib/XML/SAX/PurePerl/Reader/Stream.pm
@@ -70,6 +70,54 @@
my $self = shift;
my ($encoding) = @_;
# warn("set encoding to: $encoding\n");
+
+ # make sure that the buffer used to detect the encoding
+ # does not end in the middle of a utf8 sequence
+ if ($encoding eq 'UTF-8' &&
+ !$self->[EOF] &&
+ !utf8::is_utf8($self->[BUFFER]) && # make sure we do not do it twice
+ length($self->[BUFFER]) > 5) {
+
+ my $x = reverse(substr($self->[BUFFER], -5));
+ my $y = 0;
+
+ # skip the all the bytes at the end of buffer
+ # starting with bits 10 (continuation bytes of utf8 sequence)
+ while ($x ne "" && (ord($x) & 0xc0) == 0x80) {
+ $y--;
+ $x = substr($x, 1);
+ }
+
+ # if $x is ascii character, do nothing
+ # otherwise we must take a look how many
+ # continuation bytes we need
+ if ((ord($x) & 0xc0) == 0xc0) {
+ $x = ord($x);
+ if (($x & 0xe0) == 0xc0) { # the sequence contains one more byte
+ $y++;
+ } elsif (($x & 0xf0) == 0xe0) { # ...2 bytes
+ $y += 2;
+ } elsif (($x & 0xf8) == 0xf0) { # ...3 bytes
+ $y += 3;
+ } elsif (($x & 0xfc) == 0xf8) { # ...4 bytes
+ $y += 4;
+ } elsif (($x & 0xfe) == 0xfc) { # ...5 bytes
+ $y += 5;
+ }
+
+ # read the last sequence in the buffer completely, if needed
+ if ($y > 0) {
+ my $buf;
+ my $bytesread = read($self->[FH], $buf, $y);
+ if ($bytesread) {
+ $self->[BUFFER] .= $buf;
+ } elsif (defined($bytesread)) {
+ $self->[EOF]++;
+ }
+ }
+ }
+ }
+
XML::SAX::PurePerl::Reader::switch_encoding_stream($self->[FH], $encoding);
XML::SAX::PurePerl::Reader::switch_encoding_string($self->[BUFFER], $encoding);
$self->[ENCODING] = $encoding;