Patchwork [1,of,4] pycompat: add helper to iterate each char in bytes

login
register
mail settings
Submitter Yuya Nishihara
Date March 13, 2017, 12:41 a.m.
Message ID <75c128696f8dbc4aaa56.1489365713@mimosa>
Download mbox | patch
Permalink /patch/19263/
State Accepted
Headers show

Comments

Yuya Nishihara - March 13, 2017, 12:41 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1489363485 25200
#      Sun Mar 12 17:04:45 2017 -0700
# Node ID 75c128696f8dbc4aaa56f0e2200ebd7f560a36d5
# Parent  7359157b9e46908645cc9cef62a5cf46224dba71
pycompat: add helper to iterate each char in bytes

Patch

diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
--- a/mercurial/pycompat.py
+++ b/mercurial/pycompat.py
@@ -76,6 +76,10 @@  if ispy3:
     def bytechr(i):
         return bytes([i])
 
+    def iterbytestr(s):
+        """Iterate bytes as if it were a str object of Python 2"""
+        return iter(s[i:i + 1] for i in range(len(s)))
+
     def sysstr(s):
         """Return a keyword str to be passed to Python functions such as
         getattr() and str.encode()
@@ -142,6 +146,7 @@  else:
     import cStringIO
 
     bytechr = chr
+    iterbytestr = iter
 
     def sysstr(s):
         return s