root/trunk/extlib/JSON/PP5005.pm @ 3531

Revision 3531, 3.8 kB (checked in by fumiakiy, 9 months ago)

Merged sockfish to trunk. "svn merge -r3114:3527 http://code.sixapart.com/svn/movabletype/branches/sockfish/ ."

  • Property svn:keywords set to Id Revision
Line 
1package JSON::PP5005;
2
3use 5.005;
4use strict;
5
6my @properties;
7
8$JSON::PP5005::VERSION = '1.07';
9
10BEGIN {
11    *JSON::PP::JSON_PP_encode_ascii      = \&_encode_ascii;
12    *JSON::PP::JSON_PP_encode_latin1     = \&_encode_latin1;
13    *JSON::PP::JSON_PP_decode_surrogates = \&_decode_surrogates;
14    *JSON::PP::JSON_PP_decode_unicode    = \&_decode_unicode;
15
16
17    sub utf8::is_utf8 {
18        0; # It is considered that UTF8 flag off for Perl 5.005.
19    }
20
21    sub utf8::upgrade {
22    }
23
24    sub utf8::downgrade {
25        1; # must always return true.
26    }
27
28    sub utf8::encode  {
29    }
30
31    sub utf8::decode {
32    }
33
34    # missing in B module.
35    sub B::SVf_IOK () { 0x00010000; }
36    sub B::SVf_NOK () { 0x00020000; }
37    sub B::SVf_POK () { 0x00040000; }
38    sub B::SVp_IOK () { 0x01000000; }
39    sub B::SVp_NOK () { 0x02000000; }
40
41    $INC{'bytes.pm'} = 1; # dummy
42}
43
44
45
46sub _encode_ascii {
47    join('', map { $_ <= 127 ? chr($_) : sprintf('\u%04x', $_) } unpack('C*', $_[0]) );
48}
49
50
51sub _encode_latin1 {
52    join('', map { chr($_) } unpack('C*', $_[0]) );
53}
54
55
56sub _decode_surrogates { # from http://homepage1.nifty.com/nomenclator/unicode/ucs_utf.htm
57    my $uni = 0x10000 + (hex($_[0]) - 0xD800) * 0x400 + (hex($_[1]) - 0xDC00); # from perlunicode
58    my $bit = unpack('B32', pack('N', $uni));
59
60    if ( $bit =~ /^00000000000(...)(......)(......)(......)$/ ) {
61        my ($w, $x, $y, $z) = ($1, $2, $3, $4);
62        return pack('B*', sprintf('11110%s10%s10%s10%s', $w, $x, $y, $z));
63    }
64    else {
65        Carp::croak("Invalid surrogate pair");
66    }
67}
68
69
70sub _decode_unicode {
71    my ($u) = @_;
72    my ($utf8bit);
73    my $bit = unpack("B*", pack("H*", $u));
74
75    if ( $bit =~ /^00000(.....)(......)$/ ) {
76        $utf8bit = sprintf('110%s10%s', $1, $2);
77    }
78    elsif ( $bit =~ /^(....)(......)(......)$/ ) {
79        $utf8bit = sprintf('1110%s10%s10%s', $1, $2, $3);
80    }
81    else {
82        Carp::croak("Invalid escaped unicode");
83    }
84
85    return pack('B*', $utf8bit);
86}
87
88
89sub _is_valid_utf8 {
90    my $str = $_[0];
91    my $is_utf8;
92
93    while ($str =~ /(?:
94          (
95             [\x00-\x7F]
96            |[\xC2-\xDF][\x80-\xBF]
97            |[\xE0][\xA0-\xBF][\x80-\xBF]
98            |[\xE1-\xEC][\x80-\xBF][\x80-\xBF]
99            |[\xED][\x80-\x9F][\x80-\xBF]
100            |[\xEE-\xEF][\x80-\xBF][\x80-\xBF]
101            |[\xF0][\x90-\xBF][\x80-\xBF][\x80-\xBF]
102            |[\xF1-\xF3][\x80-\xBF][\x80-\xBF][\x80-\xBF]
103            |[\xF4][\x80-\x8F][\x80-\xBF][\x80-\xBF]
104          )
105        | (.)
106    )/xg)
107    {
108        if (defined $1) {
109            $is_utf8 = 1 if (!defined $is_utf8);
110        }
111        else {
112            $is_utf8 = 0 if (!defined $is_utf8);
113            if ($is_utf8) { # eventually, not utf8
114                return;
115            }
116        }
117    }
118
119    return $is_utf8;
120}
121
122
123sub JSON::PP::incr_parse {
124    local $Carp::CarpLevel = 1;
125    ( $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new )->incr_parse( @_ );
126}
127
128
129sub JSON::PP::incr_text {
130    $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new;
131
132    if ( $_[0]->{_incr_parser}->{incr_parsing} ) {
133        Carp::croak("incr_text can not be called when the incremental parser already started parsing");
134    }
135
136    $_[0]->{_incr_parser}->{incr_text} = $_[1] if ( @_ > 1 );
137    $_[0]->{_incr_parser}->{incr_text};
138}
139
140
141sub JSON::PP::incr_skip {
142    ( $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new )->incr_skip;
143}
144
145
146sub JSON::PP::incr_reset {
147    ( $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new )->incr_reset;
148}
149
150
1511;
152__END__
153
154=pod
155
156=head1 NAME
157
158JSON::PP5005 - Helper module in using JSON::PP in Perl 5.005
159
160=head1 DESCRIPTION
161
162JSON::PP calls internally.
163
164=head1 AUTHOR
165
166Makamaka Hannyaharamitu, E<lt>makamaka[at]cpan.orgE<gt>
167
168
169=head1 COPYRIGHT AND LICENSE
170
171Copyright 2007-2008 by Makamaka Hannyaharamitu
172
173This library is free software; you can redistribute it and/or modify
174it under the same terms as Perl itself.
175
176=cut
177
Note: See TracBrowser for help on using the browser.