add initial prototype with a few commands
[project/uqmi.git] / data / lib / JSON / backportPP / Compat5005.pm
1 package # This is JSON::backportPP
2 JSON::backportPP5005;
3
4 use 5.005;
5 use strict;
6
7 my @properties;
8
9 $JSON::PP5005::VERSION = '1.10';
10
11 BEGIN {
12
13 sub utf8::is_utf8 {
14 0; # It is considered that UTF8 flag off for Perl 5.005.
15 }
16
17 sub utf8::upgrade {
18 }
19
20 sub utf8::downgrade {
21 1; # must always return true.
22 }
23
24 sub utf8::encode {
25 }
26
27 sub utf8::decode {
28 }
29
30 *JSON::PP::JSON_PP_encode_ascii = \&_encode_ascii;
31 *JSON::PP::JSON_PP_encode_latin1 = \&_encode_latin1;
32 *JSON::PP::JSON_PP_decode_surrogates = \&_decode_surrogates;
33 *JSON::PP::JSON_PP_decode_unicode = \&_decode_unicode;
34
35 # missing in B module.
36 sub B::SVp_IOK () { 0x01000000; }
37 sub B::SVp_NOK () { 0x02000000; }
38 sub B::SVp_POK () { 0x04000000; }
39
40 $INC{'bytes.pm'} = 1; # dummy
41 }
42
43
44
45 sub _encode_ascii {
46 join('', map { $_ <= 127 ? chr($_) : sprintf('\u%04x', $_) } unpack('C*', $_[0]) );
47 }
48
49
50 sub _encode_latin1 {
51 join('', map { chr($_) } unpack('C*', $_[0]) );
52 }
53
54
55 sub _decode_surrogates { # from http://homepage1.nifty.com/nomenclator/unicode/ucs_utf.htm
56 my $uni = 0x10000 + (hex($_[0]) - 0xD800) * 0x400 + (hex($_[1]) - 0xDC00); # from perlunicode
57 my $bit = unpack('B32', pack('N', $uni));
58
59 if ( $bit =~ /^00000000000(...)(......)(......)(......)$/ ) {
60 my ($w, $x, $y, $z) = ($1, $2, $3, $4);
61 return pack('B*', sprintf('11110%s10%s10%s10%s', $w, $x, $y, $z));
62 }
63 else {
64 Carp::croak("Invalid surrogate pair");
65 }
66 }
67
68
69 sub _decode_unicode {
70 my ($u) = @_;
71 my ($utf8bit);
72
73 if ( $u =~ /^00([89a-f][0-9a-f])$/i ) { # 0x80-0xff
74 return pack( 'H2', $1 );
75 }
76
77 my $bit = unpack("B*", pack("H*", $u));
78
79 if ( $bit =~ /^00000(.....)(......)$/ ) {
80 $utf8bit = sprintf('110%s10%s', $1, $2);
81 }
82 elsif ( $bit =~ /^(....)(......)(......)$/ ) {
83 $utf8bit = sprintf('1110%s10%s10%s', $1, $2, $3);
84 }
85 else {
86 Carp::croak("Invalid escaped unicode");
87 }
88
89 return pack('B*', $utf8bit);
90 }
91
92
93 sub JSON::PP::incr_text {
94 $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new;
95
96 if ( $_[0]->{_incr_parser}->{incr_parsing} ) {
97 Carp::croak("incr_text can not be called when the incremental parser already started parsing");
98 }
99
100 $_[0]->{_incr_parser}->{incr_text} = $_[1] if ( @_ > 1 );
101 $_[0]->{_incr_parser}->{incr_text};
102 }
103
104
105 1;
106 __END__
107
108 =pod
109
110 =head1 NAME
111
112 JSON::PP5005 - Helper module in using JSON::PP in Perl 5.005
113
114 =head1 DESCRIPTION
115
116 JSON::PP calls internally.
117
118 =head1 AUTHOR
119
120 Makamaka Hannyaharamitu, E<lt>makamaka[at]cpan.orgE<gt>
121
122
123 =head1 COPYRIGHT AND LICENSE
124
125 Copyright 2007-2010 by Makamaka Hannyaharamitu
126
127 This library is free software; you can redistribute it and/or modify
128 it under the same terms as Perl itself.
129
130 =cut
131