1# Created: Tue 27 Aug 2013 06:12:39 PM IDT 2# Last Changed: Mon 23 Sep 2013 09:00:25 AM IDT 3 4use 5.10.0; 5use warnings; 6use integer; 7use strict; 8 9package Text::Bidi::Array::Long; 10# ABSTRACT: Dual-life long arrays 11$Text::Bidi::Array::Long::VERSION = '2.15'; 12 13use Carp; 14 15use Text::Bidi::Array; 16use base qw(Text::Bidi::Array); 17 18 19BEGIN { 20# fribidi uses native endianness, vec uses N (big-endian) 21 22 use Config; 23 24 if ( $Config{'byteorder'} % 10 == 1 ) { 25 # big-endian 26 *big_to_native = sub { @_ }; 27 *native_to_big = sub { @_ }; 28 } else { 29 *big_to_native = sub { unpack('L*', pack('N*', @_)) }; 30 *native_to_big = sub { unpack('N*', pack('L*', @_)) }; 31 } 32} 33 34sub pack { 35 shift; 36 pack('L*', @_) 37} 38 39sub STORE { 40 my ( $self, $i, $v ) = @_; 41 vec($self->{'data'}, $i, 32) = native_to_big($v) 42} 43 44sub FETCH { 45 my ( $self, $i ) = @_; 46 big_to_native(vec($self->{'data'}, $i, 32)) 47} 48 49sub FETCHSIZE { 50 (length($_[0]->{'data'})+3)/4 51} 52 53sub STORESIZE { 54 my ($self, $s) = @_; 55 if ($self->FETCHSIZE >= $s ) { 56 substr($self->{'data'}, $s * 4) = ''; 57 } else { 58 $self->STORE($s - 1, 0); 59 } 60} 61 621; 63 64__END__ 65 66=pod 67 68=head1 NAME 69 70Text::Bidi::Array::Long - Dual-life long arrays 71 72=head1 VERSION 73 74version 2.15 75 76=head1 SYNOPSIS 77 78 use Text::Bidi::Array::Long; 79 my $a = new Text::Bidi::Array::Long "abc"; 80 say $a->[0]; # says 6513249 (possibly) 81 say $a->[1]; # says 0 82 say $$a; # says abc 83 say "$a"; # also says abc 84 85=head1 DESCRIPTION 86 87This is an derived class of L<Text::Bidi::Array> designed to hold C<long> 88arrays. See L<Text::Bidi::Array> for details on usage of this class. Each 89element of the array representation corresponds to 4 octets in the string 90representation. The 4 octets are packed in the endianness of the native 91machine. 92 93=for Pod::Coverage native_to_big big_to_native 94 95=head1 AUTHOR 96 97Moshe Kamensky <kamensky@cpan.org> 98 99=head1 COPYRIGHT AND LICENSE 100 101This software is copyright (c) 2015 by Moshe Kamensky. 102 103This is free software; you can redistribute it and/or modify it under 104the same terms as the Perl 5 programming language system itself. 105 106=cut 107