Module Escape
A module for building predicates that escape and unescape strings, based on a map of characters
to escape and their escaped equivalents in the predicate escapeMap.
The escapeMap predicate is a binary predicate that takes two strings, the first being the
character to escape and the second being the escaped equivalent. For example, if a newline should
be escaped to \n, and \n should be unescaped to a newline, then escapeMap("\n", "n") should
hold.
The escape map is designed to work regardless of the escape character used. For example, if the
escape character is $ instead of a backslash, then the previously mentioned escape map would
automatically escape \n into $n and unescape $n into \n.
The predicate escapeMap does not need to declare that the escape character should itself be
escaped. This is handled by the escape and unescape predicates.
predicate myEscapeMap(Char real, Char escaped) {
real.isStr("\n") and escaped.isStr("n")
}
// Selects "foo\\nbar\\\\baz", "foo\nbar\\baz".
select Escape<myEscapeMap>::escape("foo\nbar\\baz", "\\"),
Escape<myEscapeMap>::unescape("foo\\nbar\\\\baz", "\\")
// Selects "foo$nbar$$baz", "foo\nbar$baz".
select Escape<myEscapeMap>::escape("foo\nbar$baz", "$"),
Escape<myEscapeMap>::unescape("foo$nbar$$baz", "$")
The following predefined escape maps are available:
defaultEscapeMap: The default escape map, which escapes newlines, carriage returns, and tabs.regexEscapeMap: The regex escape map, which escapes all regex special characters.emptyEscapeMap: An empty escape map, which does not escape any characters.
Import path
import qtil.strings.EscapePredicates
| escape | Perform the escape operation on a string, using the escape map, using backslash as the escape character. |
| escape | Perform the escape operation on a string, using the escape map, and a custom escape character. |
| unescape | Reverse the escape operation on a string, using the escape map, using backslash as the escape character. |
| unescape | Reverse the escape operation on a string, using the escape map, and a custom escape character. |
Parameters
| escapeMap | pred |