can Flavio/Dave/somebody review this code?
I basically opted to copy over $args{$unit} to a different array before doing any processing, and refactored one bit of code.


All existing tests passed.

--d

Index: lib/DateTime/Event/Recurrence.pm
===================================================================
RCS file: /cvsroot/perl-date-time/modules/DateTime-Event-Recurrence/lib/DateTime/Event/Recurrence.pm,v
retrieving revision 1.85
diff -d -u -r1.85 Recurrence.pm
--- lib/DateTime/Event/Recurrence.pm 22 Sep 2004 16:19:23 -0000 1.85
+++ lib/DateTime/Event/Recurrence.pm 8 Jan 2005 00:54:16 -0000
@@ -619,6 +619,15 @@
return $set;
}


+sub _sort_positive_first
+{
+    my @sorted  = sort { $a <=> $b } @_;
+    # put positive values first
+    my @ret = grep { $_ >= 0 } @sorted;
+    push @ret, $_ for grep { $_ < 0 } @sorted;
+
+    return @ret;
+}

 # method( hours => 10 )
 # method( hours => 10, minutes => 30 )
@@ -676,8 +685,13 @@

             next unless exists $args{$unit};

-            $args{$unit} = [ $args{$unit} ]
-                unless ref( $args{$unit} ) eq 'ARRAY';
+            # copy the argument values before proceeding, so that we
+            # don't accidentally modify it.
+            if (ref($args{$unit}) eq 'ARRAY') {
+                $args{$unit} = [ @{$args{$unit}} ];
+            } else {
+                $args{$unit} = [ $args{$unit} ];
+            }

             # TODO: sort _after_ normalization

@@ -690,13 +704,7 @@
                         $_
                     } @{$args{$unit}};
             }
-            @{$args{$unit}} = sort { $a <=> $b } @{$args{$unit}};
-            # put positive values first
-            # warn "Arguments: $unit = @{$args{$unit}}";
-            my @tmp = grep { $_ >= 0 } @{$args{$unit}};
-            push @tmp, $_ for grep { $_ < 0 } @{$args{$unit}};
-            # print STDERR "$unit => @tmp\n";
-            @{$args{$unit}} = @tmp;
+            $args{$unit} = [ _sort_positive_first(@{$args{$unit}}) ];

$ical_string .= uc( ';' . $ical_name{$unit} . '=' . join(",", @{$args{$unit}} ) ) unless $unit eq 'nanoseconds';

@@ -755,14 +763,7 @@
                     }

# redo argument sort
-
- @{$args{$unit}} = sort { $a <=> $b } @{$args{$unit}};
- # put positive values first
- my @tmp = grep { $_ >= 0 } @{$args{$unit}};
- push @tmp, $_ for grep { $_ < 0 } @{$args{$unit}};
- # print STDERR "$unit => @tmp\n";
- @{$args{$unit}} = @tmp;
-
+ $args{$unit} = [ _sort_positive_first(@{$args{$unit}}) ];
}
else
{ # year day

Reply via email to