strings-1.0.0: Functions for working with strings, including Text, ByteString, etc.

Safe HaskellTrustworthy

Data.Strings

Description

Functions for working with strings, including Text, ByteString, etc.

This module aims at offering a consistent interface across all the available string types. It currently offers instances for the ordinary Haskell String type, Text, lazy Text, ByteString, and lazy ByteString.

If especially provides functions for some types, which are otherwise not available nativly (such as breakOnSubstring is not available for the lazy Text type, is offered by sBreak and strBreak).

Synopsis

Documentation

text :: String -> TextSource

Create a Text object form an ordinary Haskell String.

lazyText :: String -> TextSource

Create a lazy Text object from an ordinary Haskell String.

bytes :: String -> ByteStringSource

Create a ByteString object form an ordinary Haskell String. This function will encode a String using the UTF-8 character encoding.

lazyBytes :: String -> ByteStringSource

Create a lazy ByteString object from an ordinary Haskell String. This function will encode a String using the UTF-8 character encoding.

class (Eq a, Strings a) => Str a whereSource

The Str class provides functions for working with arbitrary Strings. It is basically the same interface as provided by the Strings class. However, every input string is a Haskell String here, thus easing the usage of different string types with native Haskell String literals.

For example strAppend suffix works with any string type for which an instance of Str is defined. In order to maximize the ease of use of this library, the functions are prefixed with str.

The complexity and efficiency of these functions depends on the underlying string type being used.

Methods

strNull :: a -> BoolSource

Check whether the given string is empty or not. null generalised.

strLen :: a -> IntSource

length generalised.

strHead :: a -> CharSource

head generalised. This function is undefined if strNull would return True.

strLast :: a -> CharSource

last generalised. This function is undefined if strNull would return True.

strInit :: a -> aSource

init generalised. This function is undefined if strNull would return True.

strTail :: a -> aSource

tail generalised. This function is undefined if strNull would return True.

strTake :: Int -> a -> aSource

take generalised.

strDrop :: Int -> a -> aSource

drop generalised.

strReplaceSource

Arguments

:: String

Needle.

-> String

Replacement.

-> a

Haystack.

-> a

Result: Haystack with Needle replaced by Replacement.

Replace a substring with another string.

strBreak :: String -> a -> (a, a)Source

Breaks the string on the first occurence of the given substring.

 strBreak "xx" "1x2xx3xx4" = ("1x2", "xx3xx4")

strSplit :: String -> a -> (a, a)Source

Like strBreak, but the string to break on is excluded from the result.

 strSplit "xx" "1x2xx3xx4" = ("1x2", "3xx4")

strSplitAll :: String -> a -> [a]Source

Split a string into multiple fragments, separated by the given substring.

 strSplitAll "xx" "1x2xx3xx4" = ["1x2", "3", "4"]

strConcat :: [a] -> aSource

concat generalised.

strJoin :: String -> [a] -> aSource

Glue together multiple strings by a given Haskell String.

 strJoin x = concat . intersperse x

strAppend :: String -> a -> aSource

Appends the given Haskell String to the string. ++ generalised.

 strAppend " world" "hello" = "hello world"

strTrim :: a -> aSource

Strips white space characters off both ends of the string.

strPadLeft :: Char -> Int -> a -> aSource

Appends the given character n times to the left, such that the resulting string has the given length.

 strPadLeft '0' 8 "4711" == "00004711"

strPadRight :: Char -> Int -> a -> aSource

Appends the given character n times to the right, such that the resulting string has the given length.

 strPadRight '0' 8 "4711" == "47110000"

strPadBoth :: Char -> Int -> a -> aSource

Appends the given character n times to both sides, such that the resulting string has the given length.

 strPadBoth '0' 8 "4711" == "00471100"

strReverse :: a -> aSource

Reverse the string.

strEqSource

Arguments

:: String

The Haskell String.

-> a

The string.

-> Bool 

Check if the given Haskell String equals the string.

strStartsWith :: a -> String -> BoolSource

Check if the string starts with the given Haskell String.

strEndsWith :: a -> String -> BoolSource

Check if the string ends with the given Haskell String.

fromString :: String -> aSource

Create a string from a Haskell String.

fromUnicodeString :: String -> aSource

Create a string from a Haskell String. If the string does not support unicode, the Haskell String is encoded using UTF-8.

toString :: a -> StringSource

Convert the string into a Haskell String.

toWord8 :: a -> [Word8]Source

Convert the string into a list of bytes.

class Strings a whereSource

The goal of this class is to offer the same interface for various types of strings (ByteString, Text, Haskell String, etc.). If a certain type offers a native implementation for a given function, Strings uses it. If not, a default implementation is given.

All of these functions are prefixed with s to avoid nameclashes with existing functions from the prelude.

The complexity and efficiency of these functions depends on the underlying string type being used.

Methods

sNull :: a -> BoolSource

Check whether the given string is empty or not. null generalised.

sEmpty :: aSource

The empty string.

sLen :: a -> IntSource

length generalised.

sHead :: a -> CharSource

head generalised. This function is undefined if strNull would return True.

sLast :: a -> CharSource

last generalised. This function is undefined if strNull would return True.

sInit :: a -> aSource

init generalised. This function is undefined if strNull would return True.

sTail :: a -> aSource

tail generalised. This function is undefined if strNull would return True.

sTake :: Int -> a -> aSource

take generalised.

sDrop :: Int -> a -> aSource

drop generalised.

sTakeWhile :: (Char -> Bool) -> a -> aSource

takeWhile generalised.

sDropWhile :: (Char -> Bool) -> a -> aSource

dropWhile generalised.

sReplace :: a -> a -> a -> aSource

Replace a substring with another string.

sBreak :: a -> a -> (a, a)Source

Breaks the string on the first occurence of the given substring.

 strBreak "xx" "1x2xx3xx4" = ("1x2", "xx3xx4")

sSplit :: a -> a -> (a, a, Bool)Source

Like sBreak, but the string to break on is excluded from the result.

 strSplit "xx" "1x2xx3xx4" = ("1x2", "3xx4")

sSplitAll :: a -> a -> [a]Source

Split a string into multiple fragments, separated by the given substring.

 strSplitAll "xx" "1x2xx3xx4" = ["1x2", "3", "4"]

sStartsWith :: a -> a -> BoolSource

Check if the string starts with the given string.

sEndsWith :: a -> a -> BoolSource

Check if the string ends with the given string.

sConcat :: [a] -> aSource

Concatenate all the strings in the list to a single string.

sTrim :: a -> aSource

Strips white space characters off both ends of the string.

sPadLeft :: Char -> Int -> a -> aSource

Appends the given character n times to the left, such that the resulting string has the given length.

 strPadLeft '0' 8 "4711" == "00004711"

sPadRight :: Char -> Int -> a -> aSource

Appends the given character n times to the right, such that the resulting string has the given length.

 strPadRight '0' 8 "4711" == "47110000"

sPadBoth :: Char -> Int -> a -> aSource

Appends the given character n times to both sides, such that the resulting string has the given length.

 strPadBoth '0' 8 "4711" == "00471100"

sReverse :: a -> aSource

sFromString :: String -> aSource

Create a string from a Haskell String.

sFromUnicodeString :: String -> aSource

Create a string from a Haskell String. If the string does not support unicode, the Haskell String is encoded using UTF-8.

sToString :: a -> StringSource

Convert the string into a Haskell String.

sToWord8 :: a -> [Word8]Source

Convert the string into a list of bytes.