Bump version to 1.9900 for new version numbering scheme
[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
bb8ef151 8our $VERSION = '1.9900';
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
a486d5ad 33 _inline_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 {',
53a4677c 47 'my ' . $inv . ' = shift;',
48 $self->_inline_curried_arguments,
49 'if (@_ == 1 || @_ == 2) {',
1e2c801e 50 $self->_inline_reader_core($inv, $slot_access),
53a4677c 51 '}',
52 'elsif (@_ == 3) {',
1e2c801e 53 $self->_inline_writer_core($inv, $slot_access),
53a4677c 54 '}',
55 'else {',
56 $self->_inline_check_argument_count,
57 '}',
58 '}',
59 );
e7724627 60}
61
1e2c801e 62sub _minimum_arguments { 1 }
63sub _maximum_arguments { 3 }
e7724627 64
65sub _inline_process_arguments {
53a4677c 66 my $self = shift;
67 my ($inv, $slot_access) = @_;
e7724627 68
53a4677c 69 return (
70 'my $offset = shift;',
71 'my $length = @_ ? shift : length ' . $slot_access . ';',
72 'my $replacement = shift;',
73 );
e7724627 74}
75
76sub _inline_check_arguments {
53a4677c 77 my $self = shift;
78 my ($for_writer) = @_;
79
80 my @code = (
81 'if ($offset !~ /^-?\d+$/) {',
82 $self->_inline_throw_error(
83 '"The first argument passed to substr must be an integer"'
84 ) . ';',
85 '}',
86 'if ($length !~ /^-?\d+$/) {',
87 $self->_inline_throw_error(
88 '"The second argument passed to substr must be an integer"'
89 ) . ';',
90 '}',
91 );
e7724627 92
93 if ($for_writer) {
53a4677c 94 push @code, (
95 'if (!Moose::Util::_STRINGLIKE0($replacement)) {',
96 $self->_inline_throw_error(
97 '"The third argument passed to substr must be a string"'
98 ) . ';',
99 '}',
100 );
e7724627 101 }
102
53a4677c 103 return @code;
e7724627 104}
105
106sub _potential_value {
53a4677c 107 my $self = shift;
108 my ($slot_access) = @_;
e7724627 109
53a4677c 110 return '(do { '
111 . 'my $potential = ' . $slot_access . '; '
112 . '@return = substr $potential, $offset, $length, $replacement; '
113 . '$potential; '
114 . '})';
e7724627 115}
116
a486d5ad 117sub _inline_optimized_set_new_value {
53a4677c 118 my $self = shift;
119 my ($inv, $new, $slot_access) = @_;
e7724627 120
53a4677c 121 return '@return = substr ' . $slot_access . ', '
a486d5ad 122 . '$offset, $length, $replacement;';
e7724627 123}
124
125sub _return_value {
53a4677c 126 my $self = shift;
127 my ($slot_access, $for_writer) = @_;
e7724627 128
7f5ec80d 129 return '$return[0]' if $for_writer;
e7724627 130
53a4677c 131 return 'substr ' . $slot_access . ', $offset, $length';
e7724627 132}
133
8b9641b8 134no Moose::Role;
135
e7724627 1361;