text-replace-0.1.0.3: Simple text replacements from a list of search/replace pairs
Safe HaskellSafe-Inferred
LanguageHaskell2010

Text.Replace

Synopsis

Performing replacement

replaceWithList Source #

Arguments

:: Foldable f 
=> f Replace

List of replacement rules

-> Text

Input string

-> Text

Result after performing replacements on the input string

Apply a list of replacement rules to a string

The search for strings to replace is performed left-to-right, preferring longer matches to shorter ones.

Internally, the list will be converted to a ReplaceMap using listToMap. If the list contains more than one replacement for the same search string, the last mapping is used, and earlier mappings are ignored.

If you are going to be applying the same list of rules to multiple input strings, you should first convert the list to a Trie using listToTrie and then use replaceWithTrie instead.

replaceWithMap Source #

Arguments

:: ReplaceMap

Map of replacement rules

-> Text

Input string

-> Text

Result after performing replacements on the input string

Apply a map of replacement rules to a string

The search for strings to replace is performed left-to-right, preferring longer matches to shorter ones.

If you are going to be applying the same list of rules to multiple input strings, you should first convert the Map to a Trie using mapToTrie and then use replaceWithTrie instead.

replaceWithTrie Source #

Arguments

:: Trie

Map of replacement rules, represented as a trie

-> Text

Input string

-> Text

Result after performing replacements on the input string

Apply a trie of replacement rules to a string

The search for strings to replace is performed left-to-right, preferring longer matches to shorter ones.

To construct a Trie, you may use listToTrie or mapToTrie.

Specifying replacements

data Replace Source #

A replacement rule

Replace "abc" "xyz" means "When you encounter the string abc in the input text, replace it with xyz."

The first argument must be a non-empty string, because there is no sensible way to interpret "replace all occurrences of the empty string."

Constructors

Replace 

Fields

Instances

Instances details
Eq Replace Source # 
Instance details

Defined in Text.Replace

Methods

(==) :: Replace -> Replace -> Bool #

(/=) :: Replace -> Replace -> Bool #

Show Replace Source # 
Instance details

Defined in Text.Replace

type ReplaceMap = Map Text' Text Source #

A map where the keys are strings we're looking for and the values are strings with which we're replacing a key that we find

You may use listToMap to construct a ReplaceMap from a list of replacement rules, and you may use mapToAscList to convert back to a list.

listToMap :: Foldable f => f Replace -> ReplaceMap Source #

Construct a replacement map from a list of replacement rules

If the list contains more than one replacement for the same search string, the last mapping is used, and earlier mappings are ignored.

mapToAscList :: ReplaceMap -> [Replace] Source #

Convert a replacement map to a list of replacement rules

The rules in the list will be sorted according to their replaceFrom field in ascending order.

Replacements in trie structure

type Trie = Map Char Trie' Source #

A representation of a ReplaceMap designed for efficient lookups when we perform the replacements in replaceWithTrie

You may construct a Trie using listToTrie or mapToTrie.

data Trie' Source #

A variant of Trie which may contain a value at the root of the tree

Constructors

Trie' 

Instances

Instances details
Eq Trie' Source # 
Instance details

Defined in Text.Replace

Methods

(==) :: Trie' -> Trie' -> Bool #

(/=) :: Trie' -> Trie' -> Bool #

Show Trie' Source # 
Instance details

Defined in Text.Replace

Methods

showsPrec :: Int -> Trie' -> ShowS #

show :: Trie' -> String #

showList :: [Trie'] -> ShowS #

listToTrie :: Foldable f => f Replace -> Trie Source #

Convert a list of replacement rules to a trie, which is used to efficiently implement replaceWithTrie

If the list contains more than one replacement for the same search string, the last mapping is used, and earlier mappings are ignored.

ascListToTrie Source #

Arguments

:: Foldable f 
=> f Replace

This list must be sorted according to the replaceFrom field in ascending order

🌶️ Warning: this precondition is not checked

-> Trie 

Convert a list of replacement rules to a Trie, where the rules must be sorted in ascending order by the replaceFrom field

🌶️ Warning: this precondition is not checked. If you are not sure, it is safer to use listToTrie instead.

mapToTrie :: ReplaceMap -> Trie Source #

Convert a replacement map to a trie, which is used to efficiently implement replaceWithTrie

drawTrie :: Trie -> Text Source #

Draws a text diagram of a trie; useful for debugging

Non-empty text

data Text' Source #

Non-empty text

Constructors

Text' 

Fields

Instances

Instances details
Eq Text' Source # 
Instance details

Defined in Text.Replace

Methods

(==) :: Text' -> Text' -> Bool #

(/=) :: Text' -> Text' -> Bool #

Ord Text' Source # 
Instance details

Defined in Text.Replace

Methods

compare :: Text' -> Text' -> Ordering #

(<) :: Text' -> Text' -> Bool #

(<=) :: Text' -> Text' -> Bool #

(>) :: Text' -> Text' -> Bool #

(>=) :: Text' -> Text' -> Bool #

max :: Text' -> Text' -> Text' #

min :: Text' -> Text' -> Text' #

Show Text' Source # 
Instance details

Defined in Text.Replace

Methods

showsPrec :: Int -> Text' -> ShowS #

show :: Text' -> String #

showList :: [Text'] -> ShowS #

IsString Text' Source #
fromString = text'fromString

🌶️ Warning: (fromString "" :: Text') = ⊥

Instance details

Defined in Text.Replace

Methods

fromString :: String -> Text' #

text'fromString :: String -> Text' Source #

Convert an ordinary String to a non-empty Text'

🌶️ Warning: text'fromString "" = ⊥

text'fromText :: Text -> Text' Source #

Convert an ordinary Text to a non-empty Text'

🌶️ Warning: text'fromText "" = ⊥