comparison mercurial/templatefilters.py @ 9387:20ed9909dbd9

templatefilters: indent: do not compute text.endswith('\n') in each iteration
author Nicolas Dumazet <nicdumz.commits@gmail.com>
date Sat, 22 Aug 2009 19:40:15 +0200
parents 31177742f54a
children 1d75c683ada1
comparison
equal deleted inserted replaced
9386:eae98607b349 9387:20ed9909dbd9
103 103
104 def indent(text, prefix): 104 def indent(text, prefix):
105 '''indent each non-empty line of text after first with prefix.''' 105 '''indent each non-empty line of text after first with prefix.'''
106 lines = text.splitlines() 106 lines = text.splitlines()
107 num_lines = len(lines) 107 num_lines = len(lines)
108 endswithnewline = text[-1:] == '\n'
108 def indenter(): 109 def indenter():
109 for i in xrange(num_lines): 110 for i in xrange(num_lines):
110 l = lines[i] 111 l = lines[i]
111 if i and l.strip(): 112 if i and l.strip():
112 yield prefix 113 yield prefix
113 yield l 114 yield l
114 if i < num_lines - 1 or text.endswith('\n'): 115 if i < num_lines - 1 or endswithnewline:
115 yield '\n' 116 yield '\n'
116 return "".join(indenter()) 117 return "".join(indenter())
117 118
118 def permissions(flags): 119 def permissions(flags):
119 if "l" in flags: 120 if "l" in flags: