~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/xml5.py

  • Committer: John Arbash Meinel
  • Date: 2006-08-16 22:00:19 UTC
  • mto: This revision was merged to the branch mainline in revision 1942.
  • Revision ID: john@arbash-meinel.com-20060816220019-541cb90093258ac3
Using real utf8 and cache_utf8 has similar performance, 272ms, and 363ms

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
    global _utf8_re
43
43
    if _utf8_re is not None:
44
44
        return
45
 
    _utf8_re = re.compile(u'[&<>\'\"\u0080-\uffff]')
 
45
    _utf8_re = re.compile('[&<>\'\"]')
46
46
 
47
47
 
48
48
def _utf8_escape_replace(match, _map=_utf8_escape_map):
58
58
    # in overall time. But if you miss frequently, then if None is much
59
59
    # faster. For our use case, we *rarely* have a revision id, file id
60
60
    # or path name that is unicode. So use try/KeyError.
61
 
    try:
62
 
        return _map[match.group()]
63
 
    except KeyError:
64
 
        return "&#%d;" % ord(match.group())
 
61
    return _map[match.group()]
65
62
 
66
63
 
67
64
_unicode_to_escaped_map = {}
68
65
 
69
 
def _encode_and_escape(unicode_str, _map=_unicode_to_escaped_map):
 
66
def _encode_and_escape(unicode_str, _map=_unicode_to_escaped_map,
 
67
                       _encode=cache_utf8.encode):
70
68
    """Encode the string into utf8, and escape invalid XML characters"""
71
69
    # We frequently get entities we have not seen before, so it is better
72
70
    # to check if None, rather than try/KeyError
76
74
        # and then escape only XML meta characters. This could take
77
75
        # advantage of cache_utf8 since a lot of the revision ids
78
76
        # and file ids would already be cached.
79
 
        text = str(_utf8_re.sub(_utf8_escape_replace, unicode_str)) + '"'
 
77
        text = _utf8_re.sub(_utf8_escape_replace, _encode(unicode_str)) + '"'
80
78
        _map[unicode_str] = text
81
79
    return text
82
80