File: Long.pm

package info (click to toggle)
libtext-bidi-perl 2.18-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,304 kB
  • sloc: ansic: 3,358; perl: 1,064; makefile: 32
file content (106 lines) | stat: -rw-r--r-- 2,184 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# Created: Tue 27 Aug 2013 06:12:39 PM IDT
# Last Changed: Tue 28 Jun 2022 09:50:42 PM IDT

use 5.10.0;
use warnings;
use integer;
use strict;

package Text::Bidi::Array::Long;
# ABSTRACT: Dual-life long arrays
$Text::Bidi::Array::Long::VERSION = '2.18';

use Carp;

use Text::Bidi::Array;
use base qw(Text::Bidi::Array);


BEGIN {
# fribidi uses native endianness, vec uses N (big-endian)

    use Config;

    if ( $Config{'byteorder'} % 10 == 1 ) {
        # big-endian
        *big_to_native = sub { wantarray ? @_ : $_[0] };
        *native_to_big = sub { wantarray ? @_ : $_[0] };
    } else {
        *big_to_native = sub { unpack('L*', pack('N*', @_)) };
        *native_to_big = sub { unpack('N*', pack('L*', @_)) };
    }
}

sub pack {
    shift;
    pack('L*', @_)
}

sub STORE {
    my ( $self, $i, $v ) = @_;
    vec($self->{'data'}, $i, 32) = native_to_big($v)
}

sub FETCH {
    my ( $self, $i ) = @_;
    big_to_native(vec($self->{'data'}, $i, 32))
}

sub FETCHSIZE {
    (length($_[0]->{'data'})+3)/4
}

sub STORESIZE {
    my ($self, $s) = @_;
    if ($self->FETCHSIZE >= $s ) {
        substr($self->{'data'}, $s * 4) = '';
    } else {
        $self->STORE($s - 1, 0);
    }
}

1;

__END__

=pod

=head1 NAME

Text::Bidi::Array::Long - Dual-life long arrays

=head1 VERSION

version 2.18

=head1 SYNOPSIS

    use Text::Bidi::Array::Long;
    my $a = new Text::Bidi::Array::Long "abc";
    say $a->[0]; # says 6513249 (possibly)
    say $a->[1]; # says 0
    say $$a; # says abc
    say "$a"; # also says abc

=head1 DESCRIPTION

This is an derived class of L<Text::Bidi::Array> designed to hold C<long> 
arrays. See L<Text::Bidi::Array> for details on usage of this class. Each 
element of the array representation corresponds to 4 octets in the string 
representation. The 4 octets are packed in the endianness of the native 
machine.

=for Pod::Coverage native_to_big big_to_native

=head1 AUTHOR

Moshe Kamensky <kamensky@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2015 by Moshe Kamensky.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut