Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Synopsis
- replaceWithList :: Foldable f => f Replace -> String -> String
- replaceWithMap :: ReplaceMap -> String -> String
- replaceWithTrie :: Trie -> String -> String
- data Replace = Replace {}
- type ReplaceMap = Map String' String
- listToMap :: Foldable f => f Replace -> ReplaceMap
- mapToAscList :: ReplaceMap -> [Replace]
- type Trie = Map Char Trie'
- data Trie' = Trie' {
- trieRoot :: Maybe String
- trieBranches :: Trie
- listToTrie :: Foldable f => f Replace -> Trie
- ascListToTrie :: Foldable f => f Replace -> Trie
- mapToTrie :: ReplaceMap -> Trie
- drawTrie :: Trie -> String
- newtype String' = String' (NonEmpty Char)
- string'fromString :: String -> String'
- string'head :: String' -> Char
- string'tail :: String' -> String
Performing replacement
:: 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.
:: 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.
:: 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
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."
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
.
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.
:: 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 string
Non-empty string.
Instances
Eq String' Source # | |
Ord String' Source # | |
Show String' Source # | |
IsString String' Source # |
🌶️ Warning: |
Defined in Text.Replace fromString :: String -> String' # |
string'fromString :: String -> String' Source #
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.