I am trying to write two escape(text, delimiter) functions escape(text, delimiter) and unescape(text, delimiter) with the following properties:
The escape result does not contain delimiter .
unescape is the flip side of escape , i.e.
unescape(escape(text, delimiter), delimiter) == text
for all text and delimiter values
OK to limit valid delimiter values.
Background . I want to create a string of values โโseparated by a separator. To retrieve the same list from a string again, I have to make sure that the individual split strings do not contain a separator.
What I tried : I came up with a simple solution (pseudocode):
escape(text, delimiter): return text.Replace("\", "\\").Replace(delimiter, "\d") unescape(text, delimiter): return text.Replace("\d", delimiter).Replace("\\", "\")
but found that property 2 could not be executed in the test line "\d<delimiter>" . I currently have the following working solution
escape(text, delimiter): return text.Replace("\", "\b").Replace(delimiter, "\d") unescape(text, delimiter): return text.Replace("\d", delimiter).Replace("\b", "\")
which seems to work until delimiter not \ , b or d (this is normal, I don't want to use them as delimiters). However, since I have not officially proven my case, I am afraid that I have missed some case where one of the properties is violated. Since this is such a common problem, I assume that there is already a โwell-known provenโ algorithm for this, so my question is (see name).
Heinzi
source share