~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_rio_py.py

  • Committer: John Arbash Meinel
  • Date: 2009-06-04 17:12:29 UTC
  • mto: This revision was merged to the branch mainline in revision 4410.
  • Revision ID: john@arbash-meinel.com-20090604171229-kbgfatt63y3u3uh1
Some small tweaks to decoding strings (avoid passing over the length 2x)

Down to 1.1s (from 1.4s) for decoding all of bzr.dev.
Also, favor decoding strings and then lists in _decode_object, since that is the
frequency we have those types inside Revisions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009 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
"""Python implementation of _read_stanza_*."""
 
18
 
 
19
import re
 
20
 
 
21
from bzrlib.rio import (
 
22
    Stanza,
 
23
    )
 
24
 
 
25
_tag_re = re.compile(r'^[-a-zA-Z0-9_]+$')
 
26
def _valid_tag(tag):
 
27
    if type(tag) != str:
 
28
        raise TypeError(tag)
 
29
    return bool(_tag_re.match(tag))
 
30
 
 
31
 
 
32
def _read_stanza_utf8(line_iter):
 
33
    def iter_unicode_lines():
 
34
        for line in line_iter:
 
35
            if type(line) != str:
 
36
                raise TypeError(line)
 
37
            yield line.decode('utf-8')
 
38
    return _read_stanza_unicode(iter_unicode_lines())
 
39
 
 
40
 
 
41
def _read_stanza_unicode(unicode_iter):
 
42
    stanza = Stanza()
 
43
    tag = None
 
44
    accum_value = None
 
45
 
 
46
    # TODO: jam 20060922 This code should raise real errors rather than
 
47
    #       using 'assert' to process user input, or raising ValueError
 
48
    #       rather than a more specific error.
 
49
 
 
50
    for line in unicode_iter:
 
51
        if line is None or line == u'':
 
52
            break       # end of file
 
53
        if line == u'\n':
 
54
            break       # end of stanza
 
55
        real_l = line
 
56
        if line[0] == u'\t': # continues previous value
 
57
            if tag is None:
 
58
                raise ValueError('invalid continuation line %r' % real_l)
 
59
            accum_value.append(u'\n' + line[1:-1])
 
60
        else: # new tag:value line
 
61
            if tag is not None:
 
62
                stanza.add(tag, u''.join(accum_value))
 
63
            try:
 
64
                colon_index = line.index(u': ')
 
65
            except ValueError:
 
66
                raise ValueError('tag/value separator not found in line %r'
 
67
                                 % real_l)
 
68
            tag = str(line[:colon_index])
 
69
            if not _valid_tag(tag):
 
70
                raise ValueError("invalid rio tag %r" % (tag,))
 
71
            accum_value = [line[colon_index+2:-1]]
 
72
 
 
73
    if tag is not None: # add last tag-value
 
74
        stanza.add(tag, u''.join(accum_value))
 
75
        return stanza
 
76
    else:     # didn't see any content
 
77
        return None