Data.String.NonEmpty.Internal - purescript-strings - Pursuit (original) (raw)
Package
Repository
While most of the code in this module is safe, this module does export a few partial functions and the NonEmptyString
constructor. While the partial functions are obvious from the Partial
constraint in their type signature, the NonEmptyString
constructor can be overlooked when searching for issues in one's code. See the constructor's documentation for more information.
#fromString Source
fromString :: String -> Maybe NonEmptyString
Creates a NonEmptyString
from a String
, returning Nothing
if the input is empty.
fromString "" = Nothing
fromString "hello" = Just (NES.unsafeFromString "hello")
#appendString Source
appendString :: NonEmptyString -> String -> NonEmptyString
Appends a string to this non-empty string. Since one of the strings is non-empty we know the result will be too.
appendString (NonEmptyString "Hello") " world" == NonEmptyString "Hello world"
appendString (NonEmptyString "Hello") "" == NonEmptyString "Hello"
#prependString Source
prependString :: String -> NonEmptyString -> NonEmptyString
Prepends a string to this non-empty string. Since one of the strings is non-empty we know the result will be too.
prependString "be" (NonEmptyString "fore") == NonEmptyString "before"
prependString "" (NonEmptyString "fore") == NonEmptyString "fore"
#stripPrefix Source
stripPrefix :: Pattern -> NonEmptyString -> Maybe NonEmptyString
If the string starts with the given prefix, return the portion of the string left after removing it. If the prefix does not match or there is no remainder, the result will be Nothing
.
stripPrefix (Pattern "http:") (NonEmptyString "http://purescript.org") == Just (NonEmptyString "//purescript.org")
stripPrefix (Pattern "http:") (NonEmptyString "https://purescript.org") == Nothing
stripPrefix (Pattern "Hello!") (NonEmptyString "Hello!") == Nothing
#stripSuffix Source
stripSuffix :: Pattern -> NonEmptyString -> Maybe NonEmptyString
If the string ends with the given suffix, return the portion of the string left after removing it. If the suffix does not match or there is no remainder, the result will be Nothing
.
stripSuffix (Pattern ".exe") (NonEmptyString "purs.exe") == Just (NonEmptyString "purs")
stripSuffix (Pattern ".exe") (NonEmptyString "purs") == Nothing
stripSuffix (Pattern "Hello!") (NonEmptyString "Hello!") == Nothing
#contains Source
contains :: Pattern -> NonEmptyString -> Boolean
Checks whether the pattern appears in the given string.
contains (Pattern "needle") (NonEmptyString "haystack with needle") == true
contains (Pattern "needle") (NonEmptyString "haystack") == false
#localeCompare Source
localeCompare :: NonEmptyString -> NonEmptyString -> Ordering
Compare two strings in a locale-aware fashion. This is in contrast to the Ord
instance on String
which treats strings as arrays of code units:
NonEmptyString "ä" `localeCompare` NonEmptyString "b" == LT
NonEmptyString "ä" `compare` NonEmptyString "b" == GT
#joinWith Source
joinWith :: forall f. Foldable f => String -> f NonEmptyString -> String
Joins the strings in a container together as a new string, inserting the first argument as separator between them. The result is not guaranteed to be non-empty.
joinWith ", " [NonEmptyString "apple", NonEmptyString "banana"] == "apple, banana"
joinWith ", " [] == ""
#join1With Source
join1With :: forall f. Foldable1 f => String -> f NonEmptyString -> NonEmptyString
Joins non-empty strings in a non-empty container together as a new non-empty string, inserting a possibly empty string as separator between them. The result is guaranteed to be non-empty.
-- array syntax is used for demonstration here, it would need to be a real `Foldable1`
join1With ", " [NonEmptyString "apple", NonEmptyString "banana"] == NonEmptyString "apple, banana"
join1With "" [NonEmptyString "apple", NonEmptyString "banana"] == NonEmptyString "applebanana"
#joinWith1 Source
joinWith1 :: forall f. Foldable1 f => NonEmptyString -> f String -> NonEmptyString
Joins possibly empty strings in a non-empty container together as a new non-empty string, inserting a non-empty string as a separator between them. The result is guaranteed to be non-empty.
-- array syntax is used for demonstration here, it would need to be a real `Foldable1`
joinWith1 (NonEmptyString ", ") ["apple", "banana"] == NonEmptyString "apple, banana"
joinWith1 (NonEmptyString "/") ["a", "b", "", "c", ""] == NonEmptyString "a/b//c/"