[PATCH 3 of 5] create the encode and decode functions for the store

Benoit Boissinot benoit.boissinot at ens-lyon.org
Fri Dec 1 11:35:32 UTC 2006


# HG changeset patch
# User Benoit Boissinot <benoit.boissinot at ens-lyon.org>
# Date 1164968967 -3600
# Node ID 92e3dc18710cbb78c71763215692ac0f68345a54
# Parent  e6ff1d2c0b87ced2253581c0cd156a74f84fc3a3
create the encode and decode functions for the store

diff -r e6ff1d2c0b87 -r 92e3dc18710c mercurial/util.py
--- a/mercurial/util.py	Fri Dec 01 10:21:46 2006 +0100
+++ b/mercurial/util.py	Fri Dec 01 11:29:27 2006 +0100
@@ -793,6 +793,32 @@ else:
             st = fstat(f)
         return st.st_uid == os.getuid()
 
+def _buildencodefun():
+    e = '_'
+    cmap = dict([ (chr(x), chr(x)) for x in xrange(127) ])
+    for x in (range(32) + range(126, 256)):
+        cmap[chr(x)] = "~%02x" % x
+    for x in range(ord("A"), ord("Z")+1) + [ord(e)]:
+        cmap[chr(x)] = e + chr(x).lower()
+    dmap = {}
+    for k, v in cmap.iteritems():
+        dmap[v] = k
+    def decode(s):
+        i = 0
+        while i < len(s):
+            for l in xrange(1, 4):
+                try:
+                    yield dmap[s[i:i+l]]
+                    i += l
+                    break
+                except KeyError:
+                    pass
+            else:
+                raise KeyError
+    return (lambda s: "".join([cmap[c] for c in s]),
+            lambda s: "".join(list(decode(s))))
+
+encodefilename, decodefilename = _buildencodefun()
 
 def opener(base, audit=True):
     """



More information about the Mercurial-devel mailing list