From 693f1a23099ff9368c22ba6717cf86a4a8ca3af5 Mon Sep 17 00:00:00 2001 From: Harry Garrood Date: Fri, 10 Jul 2015 18:18:27 +0100 Subject: [PATCH] Add stripPrefix --- docs/Data/String.md | 11 +++++++++++ src/Data/String.purs | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/docs/Data/String.md b/docs/Data/String.md index 839ea08..d894dd2 100644 --- a/docs/Data/String.md +++ b/docs/Data/String.md @@ -78,6 +78,17 @@ dropWhile :: (Char -> Boolean) -> String -> String Returns the suffix remaining after `takeWhile`. +#### `stripPrefix` + +``` purescript +stripPrefix :: String -> String -> Maybe String +``` + +If the string starts with the given prefix, return the portion of the +string left after removing it, as a Just value. Otherwise, return Nothing. +* `stripPrefix "http:" "http://purescript.org" == Just "//purescript.org" +* `stripPrefix "http:" "https://purescript.org" == Nothing + #### `fromCharArray` ``` purescript diff --git a/src/Data/String.purs b/src/Data/String.purs index 3f737f2..2e62a7e 100644 --- a/src/Data/String.purs +++ b/src/Data/String.purs @@ -23,6 +23,7 @@ module Data.String , takeWhile , drop , dropWhile + , stripPrefix , split , toCharArray , toLower @@ -94,6 +95,16 @@ takeWhile p s = take (count p s) s dropWhile :: (Char -> Boolean) -> String -> String dropWhile p s = drop (count p s) s +-- | If the string starts with the given prefix, return the portion of the +-- | string left after removing it, as a Just value. Otherwise, return Nothing. +-- | * `stripPrefix "http:" "http://purescript.org" == Just "//purescript.org" +-- | * `stripPrefix "http:" "https://purescript.org" == Nothing +stripPrefix :: String -> String -> Maybe String +stripPrefix prefix str = + case indexOf prefix str of + Just 0 -> Just $ drop (length prefix) str + _ -> Nothing + -- | Converts an array of characters into a string. foreign import fromCharArray :: Array Char -> String