Module

Data.String.NonEmpty

Package
purescript-strings
Repository
purescript/purescript-strings

Non-empty strings.

Please note that the examples in this documentation use a notation like NonEmptyString "..." for demonstration purposes, NonEmptyString cannot be created directly like that, as we can't prove the string is non-empty to the compiler at compile-time.

#NonEmptyString Source

newtype NonEmptyString

A string that is known not to be empty.

Instances

#NonEmptyReplacement Source

newtype NonEmptyReplacement

A newtype used in cases to specify a non-empty replacement for a pattern.

Constructors

Instances

#fromString Source

fromString :: String -> Maybe NonEmptyString

Creates a NonEmptyString from a String, returning Nothing if the input is empty.

fromString "" = Nothing
fromString "hello" = Just (NonEmptyString "hello")

#unsafeFromString Source

unsafeFromString :: Partial => String -> NonEmptyString

A partial version of fromString.

#fromCharArray Source

fromCharArray :: Array Char -> Maybe NonEmptyString

Creates a NonEmptyString from a character array String, returning Nothing if the input is empty.

fromCharArray [] = Nothing
fromCharArray ['a', 'b', 'c'] = Just (NonEmptyString "abc")

#singleton Source

singleton :: Char -> NonEmptyString

Creates a NonEmptyString from a character.

#cons Source

cons :: Char -> String -> NonEmptyString

Creates a NonEmptyString from a string by prepending a character.

cons 'a' "bc" = NonEmptyString "abc"
cons 'a' "" = NonEmptyString "a"

#snoc Source

snoc :: Char -> String -> NonEmptyString

Creates a NonEmptyString from a string by appending a character.

snoc 'c' "ab" = NonEmptyString "abc"
snoc 'a' "" = NonEmptyString "a"

#fromFoldable1 Source

fromFoldable1 :: forall f. Foldable1 f => f Char -> NonEmptyString

Creates a NonEmptyString from a Foldable1 container carrying characters.

#toString Source

toString :: NonEmptyString -> String

Converts a NonEmptyString back into a standard String.

#toCharArray Source

toCharArray :: NonEmptyString -> Array Char

Converts the NonEmptyString into an array of characters.

toCharArray (NonEmptyString "Hello☺\n") == ['H','e','l','l','o','☺','\n']

#charAt Source

charAt :: Int -> NonEmptyString -> Maybe Char

Returns the character at the given index, if the index is within bounds.

charAt 2 (NonEmptyString "Hello") == Just 'l'
charAt 10 (NonEmptyString "Hello") == Nothing

#charCodeAt Source

charCodeAt :: Int -> NonEmptyString -> Maybe Int

Returns the numeric Unicode value of the character at the given index, if the index is within bounds.

charCodeAt 2 (NonEmptyString "5 €") == Just 0x20AC
charCodeAt 10 (NonEmptyString "5 €") == Nothing

#toChar Source

toChar :: NonEmptyString -> Maybe Char

Converts the NonEmptyString to a character, if the length of the string is exactly 1.

toChar "H" == Just 'H'
toChar "Hi" == Nothing

#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"

#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

#indexOf Source

indexOf :: Pattern -> NonEmptyString -> Maybe Int

Returns the index of the first occurrence of the pattern in the given string. Returns Nothing if there is no match.

indexOf (Pattern "c") (NonEmptyString "abcdc") == Just 2
indexOf (Pattern "c") (NonEmptyString "aaa") == Nothing

#indexOf' Source

indexOf' :: Pattern -> Int -> NonEmptyString -> Maybe Int

Returns the index of the first occurrence of the pattern in the given string, starting at the specified index. Returns Nothing if there is no match.

indexOf' (Pattern "a") 2 (NonEmptyString "ababa") == Just 2
indexOf' (Pattern "a") 3 (NonEmptyString "ababa") == Just 4

#lastIndexOf Source

lastIndexOf :: Pattern -> NonEmptyString -> Maybe Int

Returns the index of the last occurrence of the pattern in the given string. Returns Nothing if there is no match.

lastIndexOf (Pattern "c") (NonEmptyString "abcdc") == Just 4
lastIndexOf (Pattern "c") (NonEmptyString "aaa") == Nothing

#lastIndexOf' Source

lastIndexOf' :: Pattern -> Int -> NonEmptyString -> Maybe Int

Returns the index of the last occurrence of the pattern in the given string, starting at the specified index and searching backwards towards the beginning of the string. Returns Nothing if there is no match.

lastIndexOf' (Pattern "a") 1 (NonEmptyString "ababa") == Just 0
lastIndexOf' (Pattern "a") 3 (NonEmptyString "ababa") == Just 2
lastIndexOf' (Pattern "a") 4 (NonEmptyString "ababa") == Just 4

#uncons Source

uncons :: NonEmptyString -> { head :: Char, tail :: Maybe NonEmptyString }

Returns the first character and the rest of the string.

uncons "a" == { head: 'a', tail: Nothing }
uncons "Hello World" == { head: 'H', tail: Just (NonEmptyString "ello World") }

#length Source

length :: NonEmptyString -> Int

Returns the number of characters the string is composed of.

length (NonEmptyString "Hello World") == 11

#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

#replace Source

replace :: Pattern -> NonEmptyReplacement -> NonEmptyString -> NonEmptyString

Replaces the first occurence of the pattern with the replacement string.

replace (Pattern "<=") (NonEmptyReplacement "≤") (NonEmptyString "a <= b <= c") == NonEmptyString "a ≤ b <= c"

#replaceAll Source

replaceAll :: Pattern -> NonEmptyReplacement -> NonEmptyString -> NonEmptyString

Replaces all occurences of the pattern with the replacement string.

replaceAll (Pattern "<=") (NonEmptyReplacement "≤") (NonEmptyString "a <= b <= c") == NonEmptyString "a ≤ b ≤ c"

#take Source

take :: Int -> NonEmptyString -> Maybe NonEmptyString

Returns the first n characters of the string. Returns Nothing if n is less than 1.

take 5 (NonEmptyString "Hello World") == Just (NonEmptyString "Hello")
take 0 (NonEmptyString "Hello World") == Nothing

#takeRight Source

takeRight :: Int -> NonEmptyString -> Maybe NonEmptyString

Returns the last n characters of the string. Returns Nothing if n is less than 1.

take 5 (NonEmptyString "Hello World") == Just (NonEmptyString "World")
take 0 (NonEmptyString "Hello World") == Nothing

#takeWhile Source

takeWhile :: (Char -> Boolean) -> NonEmptyString -> Maybe NonEmptyString

Returns the longest prefix of characters that satisfy the predicate. Nothing is returned if there is no matching prefix.

takeWhile (_ /= ':') (NonEmptyString "http://purescript.org") == Just (NonEmptyString "http")
takeWhile (_ == 'a') (NonEmptyString "xyz") == Nothing

#drop Source

drop :: Int -> NonEmptyString -> Maybe NonEmptyString

Returns the string without the first n characters. Returns Nothing if more characters are dropped than the string is long.

drop 6 (NonEmptyString "Hello World") == Just (NonEmptyString "World")
drop 20 (NonEmptyString "Hello World") == Nothing

#dropRight Source

dropRight :: Int -> NonEmptyString -> Maybe NonEmptyString

Returns the string without the last n characters. Returns Nothing if more characters are dropped than the string is long.

dropRight 6 (NonEmptyString "Hello World") == Just (NonEmptyString "Hello")
dropRight 20 (NonEmptyString "Hello World") == Nothing

#dropWhile Source

dropWhile :: (Char -> Boolean) -> NonEmptyString -> Maybe NonEmptyString

Returns the suffix remaining after takeWhile.

dropWhile (_ /= '.') (NonEmptyString "Test.purs") == Just (NonEmptyString ".purs")

#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

#count Source

count :: (Char -> Boolean) -> NonEmptyString -> Int

Returns the number of contiguous characters at the beginning of the string for which the predicate holds.

count (_ /= 'o') (NonEmptyString "Hello World") == 4

#splitAt Source

splitAt :: Int -> NonEmptyString -> Maybe { after :: Maybe NonEmptyString, before :: Maybe NonEmptyString }

Returns the substrings of a split at the given index, if the index is within bounds.

splitAt 2 (NonEmptyString "Hello World") == Just { before: Just (NonEmptyString "He"), after: Just (NonEmptyString "llo World") }
splitAt 10 (NonEmptyString "Hi") == Nothing

#toLower Source

toLower :: NonEmptyString -> NonEmptyString

Returns the argument converted to lowercase.

toLower (NonEmptyString "hElLo") == NonEmptyString "hello"

#toUpper Source

toUpper :: NonEmptyString -> NonEmptyString

Returns the argument converted to uppercase.

toUpper (NonEmptyString "Hello") == NonEmptyString "HELLO"

#trim Source

trim :: NonEmptyString -> Maybe NonEmptyString

Removes whitespace from the beginning and end of a string, including whitespace characters and line terminators. If the string is entirely made up of whitespace the result will be Nothing.

trim (NonEmptyString "   Hello  \n World\n\t    ") == Just (NonEmptyString "Hello  \n World")
trim (NonEmptyString "   \n") == Nothing

#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/"

Re-exports from Data.String

#Pattern Source

newtype Pattern

A newtype used in cases where there is a string to be matched.

pursPattern = Pattern ".purs"
--can be used like this:
contains pursPattern "Test.purs"
   == true

Constructors

Instances