Revision b009f682 src/Ganeti/Utils.hs

b/src/Ganeti/Utils.hs
46 46
  , rStripSpace
47 47
  , newUUID
48 48
  , clockTimeToString
49
  , chompPrefix
49 50
  ) where
50 51

  
51 52
import Data.Char (toUpper, isAlphaNum, isDigit, isSpace)
......
292 293
-- | Convert a ClockTime into a (seconds-only) timestamp.
293 294
clockTimeToString :: ClockTime -> String
294 295
clockTimeToString (TOD t _) = show t
296

  
297
{-| Strip a prefix from a string, allowing the last character of the prefix
298
(which is assumed to be a separator) to be absent from the string if the string
299
terminates there.
300

  
301
>>> chompPrefix "foo:bar:" "a:b:c"
302
Nothing
303

  
304
>>> chompPrefix "foo:bar:" "foo:bar:baz"
305
Just "baz"
306

  
307
>>> chompPrefix "foo:bar:" "foo:bar:"
308
Just ""
309

  
310
>>> chompPrefix "foo:bar:" "foo:bar"
311
Just ""
312

  
313
>>> chompPrefix "foo:bar:" "foo:barbaz"
314
Nothing
315
-}
316
chompPrefix :: String -> String -> Maybe String
317
chompPrefix pfx str =
318
  if pfx `isPrefixOf` str || str == init pfx
319
    then Just $ drop (length pfx) str
320
    else Nothing

Also available in: Unified diff