/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
1
# Copyright (C) 2008 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
16
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
17
"""The python implementation of chunks_to_lines"""
18
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
19
20
def chunks_to_lines(chunks):
3890.2.10 by John Arbash Meinel
Change the python implementation to a friendlier implementation.
21
    """Re-split chunks into simple lines.
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
22
23
    Each entry in the result should contain a single newline at the end. Except
3890.2.10 by John Arbash Meinel
Change the python implementation to a friendlier implementation.
24
    for the last entry which may not have a final newline. If chunks is already
25
    a simple list of lines, we return it directly.
3890.2.8 by John Arbash Meinel
Move everything into properly parameterized tests.
26
27
    :param chunks: An list/tuple of strings. If chunks is already a list of
28
        lines, then we will return it as-is.
29
    :return: A list of strings.
30
    """
31
    # Optimize for a very common case when chunks are already lines
3890.2.10 by John Arbash Meinel
Change the python implementation to a friendlier implementation.
32
    last_no_newline = False
33
    for chunk in chunks:
34
        if last_no_newline:
35
            # Only the last chunk is allowed to not have a trailing newline
36
            # Getting here means the last chunk didn't have a newline, and we
37
            # have a chunk following it
38
            break
39
        if not chunk:
40
            # Empty strings are never valid lines
41
            break
6684.1.2 by Martin
Make _chunks_to_lines pass for Python 3
42
        elif b'\n' in chunk[:-1]:
3890.2.10 by John Arbash Meinel
Change the python implementation to a friendlier implementation.
43
            # This chunk has an extra '\n', so we will have to split it
44
            break
6684.1.2 by Martin
Make _chunks_to_lines pass for Python 3
45
        elif chunk[-1:] != b'\n':
3890.2.10 by John Arbash Meinel
Change the python implementation to a friendlier implementation.
46
            # This chunk does not have a trailing newline
47
            last_no_newline = True
48
    else:
49
        # All of the lines (but possibly the last) have a single newline at the
50
        # end of the string.
51
        # For the last one, we allow it to not have a trailing newline, but it
52
        # is not allowed to be an empty string.
53
        return chunks
3890.2.18 by John Arbash Meinel
Implement osutils.split_lines() in terms of chunks_to_lines if possible.
54
55
    # These aren't simple lines, just join and split again.
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
56
    from breezy import osutils
6684.1.2 by Martin
Make _chunks_to_lines pass for Python 3
57
    return osutils._split_lines(b''.join(chunks))