[PATCH 1 of 2] [extendedchangelog] encode/decode function

Benoit Boissinot benoit.boissinot at ens-lyon.org
Thu Sep 28 18:04:50 UTC 2006


# HG changeset patch
# User Benoit Boissinot <benoit.boissinot at ens-lyon.org>
# Date 1159462041 -7200
# Node ID a82e07c4a9fdd0992098aea3c73c4812245d7a53
# Parent  eb0906ebba81c7c9e1f50b9c316e7cc475fc968b
[extendedchangelog] encode/decode function

encode '\n', '\r' and '\0'

diff -r eb0906ebba81 -r a82e07c4a9fd mercurial/changelog.py
--- a/mercurial/changelog.py	Wed Sep 27 08:27:16 2006 -0500
+++ b/mercurial/changelog.py	Thu Sep 28 18:47:21 2006 +0200
@@ -9,6 +9,26 @@ from i18n import gettext as _
 from i18n import gettext as _
 from demandload import demandload
 demandload(globals(), "os time util")
+
+def string_escape(text):
+    """
+    >>> d = {'nl': chr(10), 'bs': chr(92), 'cr': chr(13), 'nul': chr(0)}
+    >>> s = "ab%(nl)scd%(bs)s%(bs)sn%(nul)sab%(cr)scd%(bs)s%(nl)s" % d
+    >>> s
+    'ab\\ncd\\\\\\\\n\\x00ab\\rcd\\\\\\n'
+    >>> res = string_escape(s)
+    >>> s == string_unescape(res)
+    True
+    """
+    # subset of the string_escape codec
+    text = text.replace('\\', '\\\\').replace('\n', '\\n').replace('\r', '\\r')
+    return text.replace('\0', '\\0')
+
+def string_unescape(text):
+    # equivalent of text.decode('string_escape')
+    s = [frag.replace('\\n', '\n').replace('\\r', '\r').replace('\\0', '\0')
+            for frag in text.split('\\\\')]
+    return '\\'.join(s)
 
 class changelog(revlog):
     def __init__(self, opener, defversion=REVLOGV0):
diff -r eb0906ebba81 -r a82e07c4a9fd tests/test-doctest.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-doctest.py	Thu Sep 28 18:47:21 2006 +0200
@@ -0,0 +1,10 @@
+#!/usr/bin/env python
+#
+
+import doctest
+
+import mercurial.changelog
+# test doctest from changelog
+
+doctest.testmod(mercurial.changelog)
+



More information about the Mercurial mailing list