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