Compat5005.pm 2.7 KB

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