/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: John Arbash Meinel
  • Date: 2008-12-11 03:08:03 UTC
  • mto: This revision was merged to the branch mainline in revision 3895.
  • Revision ID: john@arbash-meinel.com-20081211030803-gctunob7zsten3qg
Move everything into properly parameterized tests.

Also add tests that we preserve the object when it is already lines.

The compiled form takes 450us on a 7.6k line file (NEWS).
So for common cases, we should have virtually no overhead.

Show diffs side-by-side

added added

removed removed

Lines of Context:
820
820
    return pathjoin(*p)
821
821
 
822
822
 
823
 
def chunks_to_lines(chunks):
824
 
    """Ensure that chunks is split cleanly into lines.
825
 
 
826
 
    Each entry in the result should contain a single newline at the end. Except
827
 
    for the last entry which may not have a final newline.
828
 
 
829
 
    :param chunks: An iterable of strings
830
 
    :return: A list of strings.
831
 
    """
832
 
    # Optimize for a very common case when chunks are already lines
833
 
    def fail():
834
 
        raise IndexError
835
 
    try:
836
 
        # This is a bit ugly, but is the fastest way to check if all of the
837
 
        # chunks are individual lines.
838
 
        # You can't use function calls like .count(), .index(), or endswith()
839
 
        # because they incur too much python overhead.
840
 
        # It works because
841
 
        #   if chunk is an empty string, it will raise IndexError, which will
842
 
        #       be caught.
843
 
        #   if chunk doesn't end with '\n' then we hit fail()
844
 
        #   if there is more than one '\n' then we hit fail()
845
 
        # timing shows this loop to take 2.58ms rather than 3.18ms for
846
 
        # split_lines(''.join(chunks))
847
 
        # Further, it means we get to preserve the original lines, rather than
848
 
        # expanding memory
849
 
        [(chunk[-1] == '\n' and '\n' not in chunk[:-1]) or fail()
850
 
         for chunk in chunks]
851
 
        return chunks
852
 
    except IndexError:
853
 
        pass
854
 
    return split_lines(''.join(chunks))
855
 
 
856
 
 
857
823
try:
858
824
    from bzrlib._chunks_to_lines_pyx import chunks_to_lines
859
825
except ImportError:
860
 
    pass
 
826
    from bzrlib._chunks_to_lines_py import chunks_to_lines
861
827
 
862
828
 
863
829
def split_lines(s):