text-replace-0.0.0.6: Simple text replacements from a list of search/replace pairs

Safe HaskellSafe
LanguageHaskell2010

Text.Replace

Contents

Synopsis

Performing replacement

replaceWithList Source #

Arguments

:: Foldable f 
=> f Replace

List of replacement rules

-> String

Input string

-> String

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

-> String

Input string

-> String

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

-> String

Input string

-> String

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
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 String' String 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 ReplaceMap 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
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 -> String Source #

Draws a text diagram of a trie; useful for debugging.

Non-empty string

newtype String' Source #

Non-empty string.

Constructors

String' (NonEmpty Char) 
Instances
Eq String' Source # 
Instance details

Defined in Text.Replace

Methods

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

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

Ord String' Source # 
Instance details

Defined in Text.Replace

Show String' Source # 
Instance details

Defined in Text.Replace

IsString String' Source #
fromString = string'fromString

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

Instance details

Defined in Text.Replace

Methods

fromString :: String -> String' #

string'fromString :: String -> String' Source #

Convert an ordinary String to a non-empty String'.

🌶️ Warning: string'fromString "" = ⊥

string'head :: String' -> Char Source #

The first character of a non-empty string.

string'tail :: String' -> String Source #

All characters of a non-empty string except the first.