Use dzil Authority plugin - remove $AUTHORITY from code
[gitmo/Moose.git] / lib / Moose / Meta / Method / Accessor / Native / String / substr.pm
CommitLineData
e7724627 1package Moose::Meta::Method::Accessor::Native::String::substr;
2
3use strict;
4use warnings;
5
88e88a7b 6use Moose::Util ();
7
8b9641b8 8use Moose::Role;
9
10with 'Moose::Meta::Method::Accessor::Native::Reader' => {
11 -excludes => [
12 qw( _generate_method
13 _minimum_arguments
14 _maximum_arguments
15 _inline_process_arguments
16 _inline_check_arguments
17 _return_value
18 )
19 ]
20 },
21 'Moose::Meta::Method::Accessor::Native::Writer' => {
22 -excludes => [
23 qw(
24 _generate_method
25 _minimum_arguments
26 _maximum_arguments
27 _inline_process_arguments
28 _inline_check_arguments
a486d5ad 29 _inline_optimized_set_new_value
8b9641b8 30 _return_value
31 )
32 ]
33 };
e7724627 34
35sub _generate_method {
36 my $self = shift;
37
53a4677c 38 my $inv = '$self';
1e2c801e 39 my $slot_access = $self->_get_value($inv);
e7724627 40
53a4677c 41 return (
42 'sub {',
53a4677c 43 'my ' . $inv . ' = shift;',
44 $self->_inline_curried_arguments,
45 'if (@_ == 1 || @_ == 2) {',
1e2c801e 46 $self->_inline_reader_core($inv, $slot_access),
53a4677c 47 '}',
48 'elsif (@_ == 3) {',
1e2c801e 49 $self->_inline_writer_core($inv, $slot_access),
53a4677c 50 '}',
51 'else {',
52 $self->_inline_check_argument_count,
53 '}',
54 '}',
55 );
e7724627 56}
57
1e2c801e 58sub _minimum_arguments { 1 }
59sub _maximum_arguments { 3 }
e7724627 60
61sub _inline_process_arguments {
53a4677c 62 my $self = shift;
63 my ($inv, $slot_access) = @_;
e7724627 64
53a4677c 65 return (
66 'my $offset = shift;',
67 'my $length = @_ ? shift : length ' . $slot_access . ';',
68 'my $replacement = shift;',
69 );
e7724627 70}
71
72sub _inline_check_arguments {
53a4677c 73 my $self = shift;
74 my ($for_writer) = @_;
75
76 my @code = (
77 'if ($offset !~ /^-?\d+$/) {',
78 $self->_inline_throw_error(
79 '"The first argument passed to substr must be an integer"'
80 ) . ';',
81 '}',
82 'if ($length !~ /^-?\d+$/) {',
83 $self->_inline_throw_error(
84 '"The second argument passed to substr must be an integer"'
85 ) . ';',
86 '}',
87 );
e7724627 88
89 if ($for_writer) {
53a4677c 90 push @code, (
91 'if (!Moose::Util::_STRINGLIKE0($replacement)) {',
92 $self->_inline_throw_error(
93 '"The third argument passed to substr must be a string"'
94 ) . ';',
95 '}',
96 );
e7724627 97 }
98
53a4677c 99 return @code;
e7724627 100}
101
102sub _potential_value {
53a4677c 103 my $self = shift;
104 my ($slot_access) = @_;
e7724627 105
53a4677c 106 return '(do { '
107 . 'my $potential = ' . $slot_access . '; '
108 . '@return = substr $potential, $offset, $length, $replacement; '
109 . '$potential; '
110 . '})';
e7724627 111}
112
a486d5ad 113sub _inline_optimized_set_new_value {
53a4677c 114 my $self = shift;
115 my ($inv, $new, $slot_access) = @_;
e7724627 116
53a4677c 117 return '@return = substr ' . $slot_access . ', '
a486d5ad 118 . '$offset, $length, $replacement;';
e7724627 119}
120
121sub _return_value {
53a4677c 122 my $self = shift;
123 my ($slot_access, $for_writer) = @_;
e7724627 124
7f5ec80d 125 return '$return[0]' if $for_writer;
e7724627 126
53a4677c 127 return 'substr ' . $slot_access . ', $offset, $length';
e7724627 128}
129
8b9641b8 130no Moose::Role;
131
e7724627 1321;