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