Changeset 4865

Show
Ignore:
Timestamp:
06/20/05 06:39:30 (4 years ago)
Author:
mugwump
svk:copy_cache_prev:
6641
Message:

Implement 3-arg split (rx-only for now), update test

Files:
3 modified

Legend:

Unmodified
Added
Removed
  • src/Pugs/Prim.hs

    r4785 r4865  
    964964op3 "splice" = \x y z -> do 
    965965    op4 "splice" x y z (VList []) 
     966op3 "split" = op3Split 
    966967op3 "Any::new" = \t n _ -> do 
    967968    typ     <- fromVal t 
     
    982983    return obj 
    983984op3 other = \_ _ _ -> fail ("Unimplemented 3-ary op: " ++ other) 
     985 
     986op3Split :: Val -> Val -> Val -> Eval Val 
     987op3Split x y z = do 
     988    val <- fromVal x 
     989    str <- fromVal y 
     990    limit <- fromVal z 
     991    case val of 
     992        VRule rx -> do 
     993            chunks <- rxSplit_n rx str limit 
     994            return $ VList chunks 
     995        _ -> do 
     996            delim <- fromVal val 
     997            return $ split' delim str 
     998    where 
     999    split' :: VStr -> VStr -> Val 
     1000    split' [] xs = VList $ map (VStr . (:[])) xs 
     1001    split' glue xs = VList $ map VStr $ split glue xs 
    9841002 
    9851003-- |Implementation of 4-arity primitive operators and functions. 
     
    14351453\\n   List      pre     split   safe   (Str, Str)\ 
    14361454\\n   List      pre     split   safe   (Rule, Str)\ 
     1455\\n   List      pre     split   safe   (Rule, Str, Int)\ 
    14371456\\n   Str       spre    =       safe   (Any)\ 
    14381457\\n   List      spre    =       safe   (Any)\ 
  • src/Pugs/Prim/Match.hs

    r4114 r4865  
    33 
    44module Pugs.Prim.Match ( 
    5     op2Match, rxSplit, matchFromMR 
     5    op2Match, rxSplit, rxSplit_n, matchFromMR 
    66) where 
    77import Pugs.Internals 
     
    175175            rest <- rxSplit rx after 
    176176            return $ (VStr before:matchSubPos match) ++ rest 
     177 
     178-- duplicated for now, pending über-Haskell-fu 
     179 
     180rxSplit_n :: VRule -> String -> Int -> Eval [Val] 
     181rxSplit_n _ [] n = return [] 
     182rxSplit_n rx str n = do 
     183    match <- str `doMatch` rx 
     184    if or [ ( n == 1 ), ( not (matchOk match) ) ] then return [VStr str] else do 
     185    if matchFrom match == matchTo match 
     186        then do 
     187            let (c:cs) = str 
     188            rest <- rxSplit_n rx (cs) (n-1) 
     189            return (VStr [c]:rest) 
     190        else do 
     191            let before = genericTake (matchFrom match) str 
     192                after  = genericDrop (matchTo match) str 
     193            rest <- rxSplit_n rx after (n-1) 
     194            return $ (VStr before:matchSubPos match) ++ rest 
  • t/builtins/strings/split.t

    r4864 r4865  
    55 
    66# XXX - this needs to be updated when Str.split(Str) works again 
    7 plan 54; 
     7plan 64; 
    88 
    99# split on an empty string 
     
    8080try { 
    8181split_test split(rx:perl5{\s+}, "Hello World    Goodbye   Mars", 3), 
    82            [ qw/Hello World/, "Goodbye   Mars" ], 
     82           ( qw/Hello World/, "Goodbye   Mars" ), 
    8383           q(split rx:perl5{\s+}, Str, limit); 
    8484}; 
     
    8989try { 
    9090split_test split(" ", "Hello World    Goodbye   Mars", 3), 
    91            [ qw/Hello World/, "   Goodbye   Mars" ], 
     91           ( qw/Hello World/, "   Goodbye   Mars" ), 
    9292           q(split " ", Str, limit); 
    9393};