diff mercurial/wireprototypes.py @ 37612:5e71dea79aae

wireproto: move value encoding functions to wireprototypes (API) These functions should live in the same place. I plan to separate client from server code in upcoming commits. wireprototypes is where we are putting shared code like this. Differential Revision: https://phab.mercurial-scm.org/D3257
author Gregory Szorc <gregory.szorc@gmail.com>
date Wed, 11 Apr 2018 10:50:58 -0700
parents 0b7475ea38cf
children 96d735601ca1
line wrap: on
line diff
--- a/mercurial/wireprototypes.py	Tue Apr 10 19:09:35 2018 -0700
+++ b/mercurial/wireprototypes.py	Wed Apr 11 10:50:58 2018 -0700
@@ -5,6 +5,10 @@
 
 from __future__ import absolute_import
 
+from .node import (
+    bin,
+    hex,
+)
 from .thirdparty.zope import (
     interface as zi,
 )
@@ -102,6 +106,34 @@
     def __init__(self, v):
         self.value = v
 
+# list of nodes encoding / decoding
+def decodelist(l, sep=' '):
+    if l:
+        return [bin(v) for v in  l.split(sep)]
+    return []
+
+def encodelist(l, sep=' '):
+    try:
+        return sep.join(map(hex, l))
+    except TypeError:
+        raise
+
+# batched call argument encoding
+
+def escapebatcharg(plain):
+    return (plain
+            .replace(':', ':c')
+            .replace(',', ':o')
+            .replace(';', ':s')
+            .replace('=', ':e'))
+
+def unescapebatcharg(escaped):
+    return (escaped
+            .replace(':e', '=')
+            .replace(':s', ';')
+            .replace(':o', ',')
+            .replace(':c', ':'))
+
 class baseprotocolhandler(zi.Interface):
     """Abstract base class for wire protocol handlers.