Compat5006.pm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package # This is JSON::backportPP
  2. JSON::backportPP56;
  3. use 5.006;
  4. use strict;
  5. my @properties;
  6. $JSON::PP56::VERSION = '1.08';
  7. BEGIN {
  8. sub utf8::is_utf8 {
  9. my $len = length $_[0]; # char length
  10. {
  11. use bytes; # byte length;
  12. return $len != length $_[0]; # if !=, UTF8-flagged on.
  13. }
  14. }
  15. sub utf8::upgrade {
  16. ; # noop;
  17. }
  18. sub utf8::downgrade ($;$) {
  19. return 1 unless ( utf8::is_utf8( $_[0] ) );
  20. if ( _is_valid_utf8( $_[0] ) ) {
  21. my $downgrade;
  22. for my $c ( unpack( "U*", $_[0] ) ) {
  23. if ( $c < 256 ) {
  24. $downgrade .= pack("C", $c);
  25. }
  26. else {
  27. $downgrade .= pack("U", $c);
  28. }
  29. }
  30. $_[0] = $downgrade;
  31. return 1;
  32. }
  33. else {
  34. Carp::croak("Wide character in subroutine entry") unless ( $_[1] );
  35. 0;
  36. }
  37. }
  38. sub utf8::encode ($) { # UTF8 flag off
  39. if ( utf8::is_utf8( $_[0] ) ) {
  40. $_[0] = pack( "C*", unpack( "C*", $_[0] ) );
  41. }
  42. else {
  43. $_[0] = pack( "U*", unpack( "C*", $_[0] ) );
  44. $_[0] = pack( "C*", unpack( "C*", $_[0] ) );
  45. }
  46. }
  47. sub utf8::decode ($) { # UTF8 flag on
  48. if ( _is_valid_utf8( $_[0] ) ) {
  49. utf8::downgrade( $_[0] );
  50. $_[0] = pack( "U*", unpack( "U*", $_[0] ) );
  51. }
  52. }
  53. *JSON::PP::JSON_PP_encode_ascii = \&_encode_ascii;
  54. *JSON::PP::JSON_PP_encode_latin1 = \&_encode_latin1;
  55. *JSON::PP::JSON_PP_decode_surrogates = \&JSON::PP::_decode_surrogates;
  56. *JSON::PP::JSON_PP_decode_unicode = \&JSON::PP::_decode_unicode;
  57. unless ( defined &B::SVp_NOK ) { # missing in B module.
  58. eval q{ sub B::SVp_NOK () { 0x02000000; } };
  59. }
  60. }
  61. sub _encode_ascii {
  62. join('',
  63. map {
  64. $_ <= 127 ?
  65. chr($_) :
  66. $_ <= 65535 ?
  67. sprintf('\u%04x', $_) : sprintf('\u%x\u%x', JSON::PP::_encode_surrogates($_));
  68. } _unpack_emu($_[0])
  69. );
  70. }
  71. sub _encode_latin1 {
  72. join('',
  73. map {
  74. $_ <= 255 ?
  75. chr($_) :
  76. $_ <= 65535 ?
  77. sprintf('\u%04x', $_) : sprintf('\u%x\u%x', JSON::PP::_encode_surrogates($_));
  78. } _unpack_emu($_[0])
  79. );
  80. }
  81. sub _unpack_emu { # for Perl 5.6 unpack warnings
  82. return !utf8::is_utf8($_[0]) ? unpack('C*', $_[0])
  83. : _is_valid_utf8($_[0]) ? unpack('U*', $_[0])
  84. : unpack('C*', $_[0]);
  85. }
  86. sub _is_valid_utf8 {
  87. my $str = $_[0];
  88. my $is_utf8;
  89. while ($str =~ /(?:
  90. (
  91. [\x00-\x7F]
  92. |[\xC2-\xDF][\x80-\xBF]
  93. |[\xE0][\xA0-\xBF][\x80-\xBF]
  94. |[\xE1-\xEC][\x80-\xBF][\x80-\xBF]
  95. |[\xED][\x80-\x9F][\x80-\xBF]
  96. |[\xEE-\xEF][\x80-\xBF][\x80-\xBF]
  97. |[\xF0][\x90-\xBF][\x80-\xBF][\x80-\xBF]
  98. |[\xF1-\xF3][\x80-\xBF][\x80-\xBF][\x80-\xBF]
  99. |[\xF4][\x80-\x8F][\x80-\xBF][\x80-\xBF]
  100. )
  101. | (.)
  102. )/xg)
  103. {
  104. if (defined $1) {
  105. $is_utf8 = 1 if (!defined $is_utf8);
  106. }
  107. else {
  108. $is_utf8 = 0 if (!defined $is_utf8);
  109. if ($is_utf8) { # eventually, not utf8
  110. return;
  111. }
  112. }
  113. }
  114. return $is_utf8;
  115. }
  116. 1;
  117. __END__
  118. =pod
  119. =head1 NAME
  120. JSON::PP56 - Helper module in using JSON::PP in Perl 5.6
  121. =head1 DESCRIPTION
  122. JSON::PP calls internally.
  123. =head1 AUTHOR
  124. Makamaka Hannyaharamitu, E<lt>makamaka[at]cpan.orgE<gt>
  125. =head1 COPYRIGHT AND LICENSE
  126. Copyright 2007-2009 by Makamaka Hannyaharamitu
  127. This library is free software; you can redistribute it and/or modify
  128. it under the same terms as Perl itself.
  129. =cut