| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
Text.Replace
Synopsis
- replaceWithList :: Foldable f => f Replace -> Text -> Text
- replaceWithMap :: ReplaceMap -> Text -> Text
- replaceWithTrie :: Trie -> Text -> Text
- data Replace = Replace {
- replaceFrom :: Text'
- replaceTo :: Text
- type ReplaceMap = Map Text' Text
- listToMap :: Foldable f => f Replace -> ReplaceMap
- mapToAscList :: ReplaceMap -> [Replace]
- type Trie = Map Char Trie'
- data Trie' = Trie' {
- trieRoot :: Maybe Text
- trieBranches :: Trie
- listToTrie :: Foldable f => f Replace -> Trie
- ascListToTrie :: Foldable f => f Replace -> Trie
- mapToTrie :: ReplaceMap -> Trie
- drawTrie :: Trie -> Text
- data Text' = Text' {}
- text'fromString :: String -> Text'
- text'fromText :: Text -> Text'
Performing replacement
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.
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.
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
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
| |
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.
A variant of Trie which may contain a value at the root of the tree
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.
Arguments
| :: Foldable f | |
| => f Replace | This list must be sorted according to the 🌶️ 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
Non-empty text
Non-empty text
Constructors
| Text' | |
Instances
| Eq Text' Source # | |
| Ord Text' Source # | |
| Show Text' Source # | |
| IsString Text' Source # |
🌶️ Warning: |
Defined in Text.Replace Methods fromString :: String -> Text' # | |
text'fromString :: String -> Text' Source #