diff hglib/util.py @ 19:19d2c55c3928

util: introduce skiplines
author Idan Kamara <idankk86@gmail.com>
date Thu, 11 Aug 2011 15:42:58 +0300
parents 5882a698ad5c
children 3d7e0325ba1c
line wrap: on
line diff
--- a/hglib/util.py	Thu Aug 11 15:20:49 2011 +0300
+++ b/hglib/util.py	Thu Aug 11 15:42:58 2011 +0300
@@ -24,6 +24,27 @@
             return cs.read()
     return ''
 
+def skiplines(s, prefix):
+    """
+    Skip lines starting with prefix in s
+
+    >>> skiplines('a\\nb\\na\\n', 'a')
+    'b\\na\\n'
+    >>> skiplines('a\\na\\n', 'a')
+    ''
+    >>> skiplines('', 'a')
+    ''
+    >>> skiplines('a\\nb', 'b')
+    'a\\nb'
+    """
+    cs = cStringIO.StringIO(s)
+
+    for line in cs:
+        if not line.startswith(prefix):
+            return line + cs.read()
+
+    return ''
+
 def cmdbuilder(name, *args, **kwargs):
     """
     A helper for building the command arguments