----------------------------------------------------------------------------- -- | -- Module : Data.String -- Copyright : (c) 2020-2021 EMQ Technologies Co., Ltd. -- License : BSD-style (see the LICENSE file) -- -- Maintainer : Feng Lee, feng@emqx.io -- Yang M, yangm@emqx.io -- Stability : experimental -- Portability : portable -- -- The UTF-8 String datatype. -- ----------------------------------------------------------------------------- module Data.String where import Data.Maybe (Maybe) import Data.Pid (Pid) import Foreign (ffi1, ffi2) type Prefix = String type Suffix = String type Pattern = String type Replacement = String infixl 6 concat as ++ -- | Concatenates two strings to form a new string. concat :: String -> String -> String concat = ffi2 :string :concat ----------------------------------------------------------------------------- -- | Basic functions isEmpty :: String -> Boolean isEmpty = ffi1 :string :is_empty -- | String length. length :: String -> Integer length = strlen -- | Reverse the UTF-8 String. reverse :: String -> String reverse = ffi1 :string :reverse strlen :: String -> Integer strlen = ffi1 :string :length equal :: String -> String -> Boolean equal = ffi2 :string :equal foreign import equalIgnoreCase :: String -> String -> Boolean startsWith :: String -> Prefix -> Boolean startsWith = hasPrefix foreign import hasPrefix :: String -> Prefix -> Boolean endsWith :: String -> Suffix -> Boolean endsWith = hasSuffix foreign import hasSuffix :: String -> Suffix -> Boolean ----------------------------------------------------------------------------- -- | String to Atom, Integer, Float, and Pid toAtom :: String -> Atom toAtom = ffi1 :erlang :list_to_atom toExistingAtom :: String -> Atom toExistingAtom = ffi1 :erlang :list_to_existing_atom toInteger :: String -> Integer toInteger = ffi1 :erlang :list_to_integer toFloat :: String -> Float toFloat = ffi1 :erlang :list_to_float toPid :: String -> Pid toPid = ffi1 :erlang :list_to_pid ----------------------------------------------------------------------------- -- | Index, Find and Replace foreign import indexOf :: Char -> String -> Integer foreign import lastIndexOf :: Char -> String -> Integer foreign import find :: String -> Pattern -> Maybe String foreign import findLast :: String -> Pattern -> Maybe String foreign import replace :: String -> Pattern -> Replacement -> [String] foreign import replaceFirst :: String -> Pattern -> Replacement -> [String] foreign import replaceLast :: String -> Pattern -> Replacement -> [String] ----------------------------------------------------------------------------- -- | Replicate -- | Replicate to build a new string. foreign import replicate :: Integer -> String -> String ----------------------------------------------------------------------------- -- | Join, split, take, drop and slice utf8 string join :: [String] -> String -> String join = ffi2 :string :join {- TODO: lexemes :: String -> String -> [String] lexemes = ffi2 :string "lexemes" -} foreign import split :: String -> String -> [String] -- | Breaks string up into a list strings at newline characters. foreign import lines :: String -> [String] -- | Breaks a string up into a list of words at whitespace characters. foreign import words :: String -> [String] -- | Returns the first n characters of the string. take :: Integer -> String -> String take n s = sliceTo s 0 n -- | Returns the string without the first n characters. drop :: Integer -> String -> String drop n s = slice s n slice :: String -> Integer -> String slice = ffi2 :string :slice foreign import sliceTo :: String -> Integer -> Integer -> String ----------------------------------------------------------------------------- -- | Converting utf8 string -- | Converts String to lowercase. toLower :: String -> String toLower = ffi1 :string :lowercase -- | Converts String to titlecase. toTitle :: String -> String toTitle = ffi1 :string :titlecase -- | Converts String to uppercase. toUpper :: String -> String toUpper = ffi1 :string :uppercase -- | Converts String to a case-agnostic comparable string. caseFold :: String -> String caseFold = ffi1 :string :casefold ----------------------------------------------------------------------------- -- | Padding utf8 string -- | Padding whitespace in the trailing of the string. foreign import pad :: String -> Integer -> String -- | Padding whitespace in the leading of the string. foreign import padLeft :: String -> Integer -> String -- | Padding whitespace in the both sides of the string. foreign import padBoth :: String -> Integer -> String ----------------------------------------------------------------------------- -- | Trimming utf8 string -- | Returns a string where any trailing \n or \r\n have been removed from String. chomp :: String -> String chomp = ffi1 :string :chomp -- | Trim whitespace characters and line terminators. trim :: String -> String trim = ffi1 :string :trim -- | Trim any char in the second string. foreign import trimChars:: String -> String -> String foreign import trimLeft :: String -> String foreign import trimLeftChars :: String -> String -> String foreign import trimRight :: String -> String foreign import trimRightChars :: String -> String -> String