# HG changeset patch # User Ludovic Chabant # Date 1460873526 25200 # Node ID dab26ab3d533fae7676050a6ccffc7962b6d6aea # Parent c62d83e17abff0ea3b075308c1abecb21191c7c0 internal: Don't run regexes for the 99% case of pages with no segments. diff -r c62d83e17abf -r dab26ab3d533 piecrust/page.py --- a/piecrust/page.py Sat Apr 16 22:50:07 2016 -0700 +++ b/piecrust/page.py Sat Apr 16 23:12:06 2016 -0700 @@ -303,12 +303,30 @@ return len(s.split('\n')) +def _string_needs_parsing(txt, offset): + txtlen = len(txt) + index = txt.find('-', offset) + while index >= 0 and index < txtlen - 8: + if txt[index + 1] == '-' and txt[index + 2] == '-': + return True + index = txt.find('-', index + 1) + return False + + def parse_segments(raw, offset=0): # Get the number of lines in the header. header_lines = _count_lines(raw[:offset].rstrip()) current_line = header_lines - # Start parsing. + # Figure out if we need any parsing. + do_parse = _string_needs_parsing(raw, offset) + if not do_parse: + seg = ContentSegment() + seg.parts = [ + ContentSegmentPart(raw[offset:], None, offset, current_line)] + return {'content': seg} + + # Start parsing segments and parts. matches = list(segment_pattern.finditer(raw, offset)) num_matches = len(matches) if num_matches > 0: