module Manatee.Toolkit.General.String where
import Data.Char
import Data.List (isSuffixOf)
import Control.Arrow
searchForwardWord :: String -> (String, String)
searchForwardWord "" = ("", "")
searchForwardWord (x:xs)
| isAlphaNum x
= let (word, rest) = searchForwardWord xs
in (x : word, rest)
| otherwise
= ("", x : xs)
searchBackwardWord :: String -> (String, String)
searchBackwardWord str =
(reverse *** reverse) $ searchForwardWord (reverse str)
isBlankString :: String -> Bool
isBlankString [] = True
isBlankString (x:xs)
| isSpace x
= isBlankString xs
| otherwise
= False
stringToInt :: Int -> String -> Int
stringToInt base digits
= sign * (foldl acc 0 $ concatMap digToInt digits1)
where
splitSign ('-' : ds) = ((1), ds)
splitSign ('+' : ds) = ( 1 , ds)
splitSign ds = ( 1 , ds)
(sign, digits1) = splitSign digits
digToInt c
| c >= '0' && c <= '9'
= [ord c ord '0']
| c >= 'A' && c <= 'Z'
= [ord c ord 'A' + 10]
| c >= 'a' && c <= 'z'
= [ord c ord 'a' + 10]
| otherwise
= []
acc i1 i0
= i1 * base + i0
hexStringToInt :: String -> Int
hexStringToInt = stringToInt 16
dropSuffix :: [String] -> String -> String
dropSuffix [] str = str
dropSuffix (x:xs) str
| x `isSuffixOf` str
= take (length str length x) str
| otherwise
= dropSuffix xs str
stripFormat :: String -> Int -> String
stripFormat str limit
| limit < 0
= str
| length str <= limit
= str
| otherwise
= take limit str ++ "..."
unlinesExceptLast :: [String] -> String
unlinesExceptLast [] = ""
unlinesExceptLast list = init $ unlines list