changeset 3852:8a9a1a7e1698

create the encode and decode functions for the store
author Benoit Boissinot <benoit.boissinot@ens-lyon.org>
date Sun, 10 Dec 2006 00:07:02 +0100
parents 8f18e31c4441
children c0b449154a90
files mercurial/util.py
diffstat 1 files changed, 28 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/util.py	Sun Dec 10 00:06:59 2006 +0100
+++ b/mercurial/util.py	Sun Dec 10 00:07:02 2006 +0100
@@ -869,6 +869,34 @@
             st = fstat(f)
         return st.st_uid == os.getuid()
 
+def _buildencodefun():
+    e = '_'
+    win_reserved = [ord(x) for x in '|\?*<":>+[]']
+    cmap = dict([ (chr(x), chr(x)) for x in xrange(127) ])
+    for x in (range(32) + range(126, 256) + win_reserved):
+        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):
     """