Changeset 16526

Show
Ignore:
Timestamp:
05/25/07 23:12:48 (18 months ago)
Author:
moritz
Message:

patch and test cases for zero or negative argument to 'x' and 'xx' operator
postet ond p6l by chas_owens++

Files:
2 modified

Legend:

Unmodified
Added
Removed
  • src/Pugs/Prim.hs

    r16488 r16526  
    924924    | otherwise         = VStr str 
    925925 
     926perlReplicate :: VInt -> a -> [a] 
     927perlReplicate i a = if i < 0  
     928    then genericReplicate 0 a  
     929    else genericReplicate i a 
     930 
    926931-- |Implementation of 2-arity primitive operators and functions 
    927932op2 :: String -> Val -> Val -> Eval Val 
     
    932937op2 "/"  = op2Divide 
    933938op2 "%"  = op2Modulus 
    934 op2 "x"  = op2Cast (\x y -> VStr . concat $ (y :: VInt) `genericReplicate` x) 
    935 op2 "xx" = op2Cast (\x y -> VList . concat $ (y :: VInt) `genericReplicate` x) 
     939op2 "x"  = op2Cast (\x y -> VStr . concat $ (y :: VInt) `perlReplicate` x) 
     940op2 "xx" = op2Cast (\x y -> VList . concat $ (y :: VInt) `perlReplicate` x) 
    936941op2 "+&" = op2Int (.&.) 
    937942op2 "+<" = op2Int shiftL 
  • t/operators/repeat.t

    r15255 r16526  
    99=cut 
    1010 
    11 plan 19; 
     11plan 23; 
    1212 
    1313#L<S03/Changes to Perl 5 operators/"x (which concatenates repetitions of a string to produce a single string"> 
     
    1717is(1 x 5, '11111', 'number repeat operator works on number and creates string'); 
    1818is('' x 6, '', 'repeating an empty string creates an empty string'); 
     19is('a' x 0, '', 'repeating zero times produces an empty string'); 
     20is('a' x -1, '', 'repeating negative times produces an empty string'); 
    1921 
    2022#L<S03/Changes to Perl 5 operators/"and xx (which creates a list of repetitions of a list or item)"> 
     
    2426is(+@foo, 10, 'list repeat operator created array of the right size'); 
    2527 
     28 
    2629lives_ok { my @foo2 = undef xx 2; }, 'can repeat undefs'; 
    2730my @foo3 = (1, 2) xx 2; 
     
    3033is(@foo3[2], 1, 'can repeat lists'); 
    3134is(@foo3[3], 2, 'can repeat lists'); 
     35 
     36my @foo4 = 'x' xx 0; 
     37is(+@foo4, 0, 'repeating zero times produces an empty list'); 
     38 
     39my @foo5 = 'x' xx -1; 
     40is(+@foo5, 0, 'repeating negative times produces an empty list'); 
     41 
    3242my @foo_2d = [1, 2] xx 2; # should create 2d 
    3343is(@foo_2d[1], [1, 2], 'can create 2d arrays', :todo<bug>); # creates a flat 1d array