Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / XML / SAX / PurePerl / Reader / String.pm
1 # $Id: String.pm,v 1.6 2008-08-04 03:35:44 grant Exp $
2
3 package XML::SAX::PurePerl::Reader::String;
4
5 use strict;
6 use vars qw(@ISA);
7
8 use XML::SAX::PurePerl::Reader qw(
9     LINE
10     COLUMN
11     BUFFER
12     ENCODING
13     EOF
14 );
15
16 @ISA = ('XML::SAX::PurePerl::Reader');
17
18 use constant DISCARDED  => 8;
19 use constant STRING     => 9;
20 use constant USED       => 10;
21 use constant CHUNK_SIZE => 2048;
22
23 sub new {
24     my $class = shift;
25     my $string = shift;
26     my @parts;
27     @parts[BUFFER, EOF, LINE, COLUMN, DISCARDED, STRING, USED] =
28         ('',   0,   1,    0,       0, $string, 0);
29     return bless \@parts, $class;
30 }
31
32 sub read_more () {
33     my $self = shift;
34     if ($self->[USED] >= length($self->[STRING])) {
35         $self->[EOF]++;
36         return 0;
37     }
38     my $bytes = CHUNK_SIZE;
39     if ($bytes > (length($self->[STRING]) - $self->[USED])) {
40        $bytes = (length($self->[STRING]) - $self->[USED]);
41     }
42     $self->[BUFFER] .= substr($self->[STRING], $self->[USED], $bytes);
43     $self->[USED] += $bytes;
44     return 1;
45  }
46
47
48 sub move_along {
49     my($self, $bytes) = @_;
50     my $discarded = substr($self->[BUFFER], 0, $bytes, '');
51     $self->[DISCARDED] += length($discarded);
52     
53     # Wish I could skip this lot - tells us where we are in the file
54     my $lines = $discarded =~ tr/\n//;
55     $self->[LINE] += $lines;
56     if ($lines) {
57         $discarded =~ /\n([^\n]*)$/;
58         $self->[COLUMN] = length($1);
59     }
60     else {
61         $self->[COLUMN] += $_[0];
62     }
63 }
64
65 sub set_encoding {
66     my $self = shift;
67     my ($encoding) = @_;
68
69     XML::SAX::PurePerl::Reader::switch_encoding_string($self->[BUFFER], $encoding, "utf-8");
70     $self->[ENCODING] = $encoding;
71 }
72
73 sub bytepos {
74     my $self = shift;
75     $self->[DISCARDED];
76 }
77
78 1;