module Data.Strings (
text,
lazyText,
bytes,
lazyBytes,
charToByte,
byteToChar,
Str (..),
Strings (..)
) where
import Data.Int
import Data.Word
import Data.String hiding (fromString)
import qualified Data.String as S
import qualified Data.Char as C
import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString as B
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy.Char8 as LazyB8
import qualified Data.ByteString.Lazy as LazyB
import qualified Data.Text as T
import Data.Text (Text)
import qualified Data.Text.Lazy as LazyT
import qualified Data.List as L
import qualified Data.Text.Encoding as Enc
import qualified Data.Text.Lazy.Encoding as LazyEnc
isSpace x
| x == ' ' = True
| x == '\t' = True
| otherwise = False
isWhiteSpace x
| x == ' ' = True
| x == '\t' = True
| x == '\r' = True
| x == '\n' = True
| otherwise = False
charToByte :: Char -> Word8
charToByte = fromIntegral . C.ord
byteToChar :: Word8 -> Char
byteToChar = C.chr . fromIntegral
text :: String -> Text
text = S.fromString
lazyText :: String -> LazyT.Text
lazyText = S.fromString
bytes :: String -> ByteString
bytes = sFromUnicodeString
lazyBytes :: String -> LazyB.ByteString
lazyBytes = sFromUnicodeString
class (Eq a, Strings a) => Str a where
strNull :: a -> Bool
strLen :: a -> Int
strHead :: a -> Char
strLast :: a -> Char
strInit :: a -> a
strTail :: a -> a
strTake :: Int -> a -> a
strDrop :: Int -> a -> a
strReplace :: String
-> String
-> a
-> a
strBreak :: String -> a -> (a, a)
strSplit :: String -> a -> (a, a)
strSplitAll :: String -> a -> [a]
strToUpper :: a -> a
strToLower :: a -> a
strCapitalize :: a -> a
strMap :: (Char -> Char) -> a -> a
strConcat :: [a] -> a
strJoin :: String -> [a] -> a
strAppend :: String
-> a
-> a
strCons :: Char -> a -> a
strTrim :: a -> a
strPadLeft :: Char -> Int -> a -> a
strPadRight :: Char -> Int -> a -> a
strPadBoth :: Char -> Int -> a -> a
strReverse :: a -> a
strEq :: String
-> a
-> Bool
strStartsWith :: a -> String -> Bool
strEndsWith :: a -> String -> Bool
fromString :: String -> a
fromUnicodeString :: String -> a
toString :: a -> String
toWord8 :: a -> [Word8]
strNull = sNull
strLen = sLen
strHead = sHead
strLast = sLast
strInit = sInit
strTail = sTail
strTake = sTake
strDrop = sDrop
strReplace n r = sReplace (fromString n) (fromString r)
strBreak n = sBreak (fromString n)
strSplit d s = (a, b)
where (a, b, _) = sSplit (fromString d) s
strSplitAll n = sSplitAll (fromString n)
strToLower = strMap C.toLower
strToUpper = strMap C.toUpper
strCapitalize str = strCons (C.toUpper (strHead str)) (strTail str)
strCons = sCons
strMap = sMap
strConcat = sConcat
strJoin d = let d' = fromUnicodeString d in strConcat . L.intersperse d'
strAppend x xs = sConcat [xs, fromString x]
strTrim = sTrim
strPadLeft = sPadLeft
strPadRight = sPadRight
strPadBoth = sPadBoth
strReverse = sReverse
strStartsWith s pref = sStartsWith s (fromString pref)
strEndsWith s suff = sEndsWith s (fromString suff)
strEq s1 s2 = fromUnicodeString s1 == s2
fromString = sFromString
fromUnicodeString = sFromUnicodeString
toString = sToString
toWord8 = sToWord8
instance Str Text where
instance Str LazyT.Text where
instance Str ByteString where
instance Str LazyB.ByteString where
instance Str String where
class Strings a where
sNull :: a -> Bool
sEmpty :: a
sLen :: a -> Int
sHead :: a -> Char
sLast :: a -> Char
sInit :: a -> a
sTail :: a -> a
sTake :: Int -> a -> a
sDrop :: Int -> a -> a
sTakeWhile :: (Char -> Bool) -> a -> a
sDropWhile :: (Char -> Bool) -> a -> a
sReplace :: a -> a -> a -> a
sBreak :: a -> a -> (a, a)
sSplit :: a -> a -> (a, a, Bool)
sSplitAll :: a -> a -> [a]
sStartsWith :: a -> a -> Bool
sEndsWith :: a -> a -> Bool
sCons :: Char -> a -> a
sCapitalize :: a -> a
sMap :: (Char -> Char) -> a -> a
sConcat :: [a] -> a
sTrim :: a -> a
sPadLeft :: Char -> Int -> a -> a
sPadRight :: Char -> Int -> a -> a
sPadBoth :: Char -> Int -> a -> a
sReverse :: a -> a
sFromString :: String -> a
sFromUnicodeString :: String -> a
sToString :: a -> String
sToWord8 :: a -> [Word8]
sCapitalize xs
| sNull xs = xs
| otherwise = sCons (C.toUpper (sHead xs)) (sTail xs)
sBreak d src = search 0 src
where
search a b | a `seq` b `seq` False = undefined
search n s
| sNull s = (src, sEmpty)
| sStartsWith s d = (sTake n src, s)
| otherwise = search (n+1) (sTail s)
sReplace n r = sConcat . replace n r
where
replace n r h = if sNull b
then (if c then [a, r] else [a])
else a : r : replace n r b
where (a, b, c) = sSplit n h
sSplit d s = (a, sDrop (sLen d) b, not $ sNull b)
where (a, b) = sBreak d s
sSplitAll d s = if sNull b
then (if c then [a, b] else [a])
else a : sSplitAll d b
where (a, b, c) = sSplit d s
sPadLeft c n s = let len = sLen s
padLen = max 0 (n len)
padStr = sFromString $ replicate padLen c
in sConcat [padStr, s]
sPadRight c n s = let len = sLen s
padLen = max 0 (n len)
padStr = sFromString $ replicate padLen c
in sConcat [s, padStr]
sPadBoth c n s = let len = sLen s
padLen = (max 0 (n len))
padLenR = padLen `quot` 2
padLenL = padLen padLenR
padStrL = sFromString $ replicate padLenL c
padStrR = sFromString $ replicate padLenR c
in sConcat [padStrL, s, padStrR]
sFromString = sFromUnicodeString
sToWord8 = sToWord8 . sToString
instance Strings ByteString where
sNull = B.null
sEmpty = B.empty
sHead = toEnum . fromIntegral . B.head
sLast = toEnum . fromIntegral . B.last
sInit = B.init
sTail = B.tail
sTake = B.take
sTakeWhile f = B.takeWhile (f . chr)
sDrop = B.drop
sDropWhile f = B.dropWhile (f . chr)
sBreak = B.breakSubstring
sStartsWith = flip B.isPrefixOf
sEndsWith = flip B.isSuffixOf
sCons = B.cons . charToByte
sMap f = B.map (charToByte . f . byteToChar)
sConcat = B.concat
sLen = B.length
sTrim s = let (a, b) = B.span (isWhiteSpace . chr) s
(c, d) = B.spanEnd (isWhiteSpace . chr) b
in c
sReverse = B.reverse
sFromString = B8.pack
sFromUnicodeString = Enc.encodeUtf8 . T.pack
sToString = B8.unpack
sToWord8 = B.unpack
instance Strings LazyB.ByteString where
sNull = LazyB.null
sEmpty = LazyB.empty
sHead = toEnum . fromIntegral . LazyB.head
sLast = toEnum . fromIntegral . LazyB.last
sInit = LazyB.init
sTail = LazyB.tail
sTake = LazyB.take . fromIntegral
sTakeWhile f = LazyB.takeWhile (f . chr)
sDrop = LazyB.drop . fromIntegral
sDropWhile f = LazyB.dropWhile (f . chr)
sStartsWith = flip LazyB.isPrefixOf
sEndsWith = flip LazyB.isSuffixOf
sCons = LazyB.cons . charToByte
sMap f = LazyB.map (charToByte . f . byteToChar)
sConcat = LazyB.concat
sLen = fromIntegral . LazyB.length
sTrim = LazyB.fromChunks . (:[]) . sTrim . B.concat . LazyB.toChunks
sReverse = LazyB.reverse
sFromString = LazyB8.pack
sFromUnicodeString = LazyEnc.encodeUtf8 . LazyT.pack
sToString = LazyB8.unpack
sToWord8 = LazyB.unpack
instance Strings Text where
sNull = T.null
sEmpty = T.empty
sHead = T.head
sLast = T.last
sInit = T.init
sTail = T.tail
sTake = T.take
sTakeWhile = T.takeWhile
sDrop = T.drop
sDropWhile = T.dropWhile
sReplace = T.replace
sBreak = T.breakOn
sStartsWith = flip T.isPrefixOf
sEndsWith = flip T.isSuffixOf
sCons = T.cons
sMap = T.map
sConcat = T.concat
sLen = T.length
sTrim = T.strip
sPadLeft = flip T.justifyLeft
sPadRight = flip T.justifyRight
sPadBoth = flip T.center
sReverse = T.reverse
sFromUnicodeString = S.fromString
sFromString = S.fromString
sToString = T.unpack
sToWord8 = B.unpack . Enc.encodeUtf8
instance Strings LazyT.Text where
sNull = LazyT.null
sEmpty = LazyT.empty
sHead = LazyT.head
sLast = LazyT.last
sInit = LazyT.init
sTail = LazyT.tail
sTake = LazyT.take . fromIntegral
sTakeWhile = LazyT.takeWhile
sDrop = LazyT.drop . fromIntegral
sDropWhile = LazyT.dropWhile
sBreak = LazyT.breakOn
sStartsWith = flip LazyT.isPrefixOf
sEndsWith = flip LazyT.isSuffixOf
sCons = LazyT.cons
sMap = LazyT.map
sConcat = LazyT.concat
sLen = fromIntegral . LazyT.length
sTrim = LazyT.strip
sPadLeft = flip (LazyT.justifyLeft . fromIntegral)
sPadRight = flip (LazyT.justifyRight . fromIntegral)
sPadBoth = flip (LazyT.center . fromIntegral)
sReverse = LazyT.reverse
sFromUnicodeString = S.fromString
sFromString = S.fromString
sToString = LazyT.unpack
sToWord8 = LazyB.unpack . LazyEnc.encodeUtf8
instance Strings String where
sNull = null
sEmpty = []
sHead = head
sLast = last
sInit = init
sTail = tail
sTake = take
sTakeWhile = takeWhile
sDrop = drop
sDropWhile = dropWhile
sStartsWith = flip L.isPrefixOf
sEndsWith = flip L.isSuffixOf
sCapitalize (x:xs) = C.toUpper x : xs
sCapitalize _ = []
sCons = (:)
sMap = map
sConcat = concat
sLen = length
sTrim = T.unpack . T.strip . T.pack
sReverse = reverse
sFromString = id
sFromUnicodeString = id
sToString = id
sToWord8 = B.unpack . Enc.encodeUtf8 . T.pack
class Chars a where
ord :: Char -> a
chr :: a -> Char
instance Chars Int where
ord = fromIntegral . fromEnum
chr = toEnum . fromIntegral
instance Chars Word8 where
ord = fromIntegral . fromEnum
chr = toEnum . fromIntegral
instance Chars Word16 where
ord = fromIntegral . fromEnum
chr = toEnum . fromIntegral
instance Chars Word32 where
ord = fromIntegral . fromEnum
chr = toEnum . fromIntegral
instance Chars Word64 where
ord = fromIntegral . fromEnum
chr = toEnum . fromIntegral
instance Chars Int8 where
ord = fromIntegral . fromEnum
chr = toEnum . fromIntegral
instance Chars Int16 where
ord = fromIntegral . fromEnum
chr = toEnum . fromIntegral
instance Chars Int32 where
ord = fromIntegral . fromEnum
chr = toEnum . fromIntegral
instance Chars Int64 where
ord = fromIntegral . fromEnum
chr = toEnum . fromIntegral
instance Chars Integer where
ord = fromIntegral . fromEnum
chr = toEnum . fromIntegral