Module

Data.String.NonEmpty.CodeUnits

#fromCharArray

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

singleton :: Char -> NonEmptyString

Creates a NonEmptyString from a character.

#cons

cons :: Char -> String -> NonEmptyString

Creates a NonEmptyString from a string by prepending a character.

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

#snoc

snoc :: Char -> String -> NonEmptyString

Creates a NonEmptyString from a string by appending a character.

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

#fromFoldable1

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

Creates a NonEmptyString from a Foldable1 container carrying characters.

#toCharArray

toCharArray :: NonEmptyString -> Array Char

Converts the NonEmptyString into an array of characters.

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

#toNonEmptyCharArray

toNonEmptyCharArray :: NonEmptyString -> NonEmptyArray Char

Converts the NonEmptyString into a non-empty array of characters.

#charAt

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

#toChar

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

#indexOf

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'

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

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'

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

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

length :: NonEmptyString -> Int

Returns the number of characters the string is composed of.

length (NonEmptyString "Hello World") == 11

#take

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

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

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

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

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

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

Returns the suffix remaining after takeWhile.

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

#countPrefix

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

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

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

#splitAt

splitAt :: Int -> NonEmptyString -> { 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

Modules