Mercurial > public > mercurial-scm > hg
comparison mercurial/pycompat.py @ 31382:c9fd842dc886
pycompat: add helper to iterate each char in bytes
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sun, 12 Mar 2017 17:04:45 -0700 |
parents | 06440ba06bc0 |
children | 1ed169c5e235 |
comparison
equal
deleted
inserted
replaced
31381:7359157b9e46 | 31382:c9fd842dc886 |
---|---|
74 sysargv = list(map(os.fsencode, sys.argv)) | 74 sysargv = list(map(os.fsencode, sys.argv)) |
75 | 75 |
76 def bytechr(i): | 76 def bytechr(i): |
77 return bytes([i]) | 77 return bytes([i]) |
78 | 78 |
79 def iterbytestr(s): | |
80 """Iterate bytes as if it were a str object of Python 2""" | |
81 return iter(s[i:i + 1] for i in range(len(s))) | |
82 | |
79 def sysstr(s): | 83 def sysstr(s): |
80 """Return a keyword str to be passed to Python functions such as | 84 """Return a keyword str to be passed to Python functions such as |
81 getattr() and str.encode() | 85 getattr() and str.encode() |
82 | 86 |
83 This never raises UnicodeDecodeError. Non-ascii characters are | 87 This never raises UnicodeDecodeError. Non-ascii characters are |
140 | 144 |
141 else: | 145 else: |
142 import cStringIO | 146 import cStringIO |
143 | 147 |
144 bytechr = chr | 148 bytechr = chr |
149 iterbytestr = iter | |
145 | 150 |
146 def sysstr(s): | 151 def sysstr(s): |
147 return s | 152 return s |
148 | 153 |
149 # Partial backport from os.py in Python 3, which only accepts bytes. | 154 # Partial backport from os.py in Python 3, which only accepts bytes. |