~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_chunks_to_lines_py.py

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
"""The python implementation of chunks_to_lines"""
18
 
 
19
 
from __future__ import absolute_import
20
 
 
21
 
 
22
 
def chunks_to_lines(chunks):
23
 
    """Re-split chunks into simple lines.
24
 
 
25
 
    Each entry in the result should contain a single newline at the end. Except
26
 
    for the last entry which may not have a final newline. If chunks is already
27
 
    a simple list of lines, we return it directly.
28
 
 
29
 
    :param chunks: An list/tuple of strings. If chunks is already a list of
30
 
        lines, then we will return it as-is.
31
 
    :return: A list of strings.
32
 
    """
33
 
    # Optimize for a very common case when chunks are already lines
34
 
    last_no_newline = False
35
 
    for chunk in chunks:
36
 
        if last_no_newline:
37
 
            # Only the last chunk is allowed to not have a trailing newline
38
 
            # Getting here means the last chunk didn't have a newline, and we
39
 
            # have a chunk following it
40
 
            break
41
 
        if not chunk:
42
 
            # Empty strings are never valid lines
43
 
            break
44
 
        elif '\n' in chunk[:-1]:
45
 
            # This chunk has an extra '\n', so we will have to split it
46
 
            break
47
 
        elif chunk[-1] != '\n':
48
 
            # This chunk does not have a trailing newline
49
 
            last_no_newline = True
50
 
    else:
51
 
        # All of the lines (but possibly the last) have a single newline at the
52
 
        # end of the string.
53
 
        # For the last one, we allow it to not have a trailing newline, but it
54
 
        # is not allowed to be an empty string.
55
 
        return chunks
56
 
 
57
 
    # These aren't simple lines, just join and split again.
58
 
    from bzrlib import osutils
59
 
    return osutils._split_lines(''.join(chunks))