[PATCH 2 of 2 hglib] util: rewrite eatlines (faster and simpler version)
Idan Kamara
idankk86 at gmail.com
Tue Aug 9 17:48:56 CDT 2011
# HG changeset patch
# User Idan Kamara <idankk86 at gmail.com>
# Date 1312930118 -10800
# Node ID 31a5e7bcd4027af5a1bdf927b27b5b764a5c60e7
# Parent 6dd7ca323148bb807fd18be3cc4673ac37216922
util: rewrite eatlines (faster and simpler version)
diff -r 6dd7ca323148 -r 31a5e7bcd402 hglib/util.py
--- a/hglib/util.py Wed Aug 10 01:38:18 2011 +0300
+++ b/hglib/util.py Wed Aug 10 01:48:38 2011 +0300
@@ -1,4 +1,4 @@
-import itertools
+import itertools, cStringIO
def grouper(n, iterable):
''' list(grouper(2, range(4))) -> [(0, 1), (2, 3)] '''
@@ -6,11 +6,23 @@
return itertools.izip(*args)
def eatlines(s, n):
- idx = 0
- for i in xrange(n):
- idx = s.find('\n', idx) + 1
+ """
+ >>> eatlines("1\\n2", 1)
+ '2'
+ >>> eatlines("1\\n2", 2)
+ ''
+ >>> eatlines("1\\n2", 3)
+ ''
+ >>> eatlines("1\\n2\\n3", 1)
+ '2\\n3'
+ """
+ cs = cStringIO.StringIO(s)
- return s[idx:]
+ for line in cs:
+ n -= 1
+ if n == 0:
+ return cs.read()
+ return ''
def cmdbuilder(name, *args, **kwargs):
"""
More information about the Mercurial-devel
mailing list