Skip to content

Commit

Permalink
Optimize wmliterator.isDirective()
Browse files Browse the repository at this point in the history
Skips an unecessary for loop and if conditional.
  • Loading branch information
legoktm committed Jul 27, 2015
1 parent de864c0 commit 95a84bb
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions data/tools/wesnoth/wmliterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,13 @@ def wmlfindin(element, scopeElement, wmlItor):
return itor
return None


def isDirective(elem):
"Identify things that shouldn't be indented."
if isinstance(elem, WmlIterator):
elem = elem.element
for prefix in ("#ifdef", "#ifndef", "#ifhave", "#ifnhave", "#ifver", "#ifnver", "#else", "#endif", "#define", "#enddef", "#undef"):
if elem.startswith(prefix):
return True
return False
return elem.startswith(("#ifdef", "#ifndef", "#ifhave", "#ifnhave", "#ifver", "#ifnver", "#else", "#endif", "#define", "#enddef", "#undef"))


def isCloser(elem):
"Are we looking at a closing tag?"
Expand Down Expand Up @@ -186,7 +185,7 @@ def closeScope(self, scopes, closerElement):
if isDirective(closerElement):
while not isDirective(scopes.pop()):
pass
elif (closerElement==closeMacroType):
elif closerElement == closeMacroType:
elem = ''
while not elem.startswith('{'):
closed = scopes.pop()
Expand All @@ -202,12 +201,12 @@ def closeScope(self, scopes, closerElement):
elem = closed
if isinstance(closed, WmlIterator):
elem = closed.element
if (elem.startswith('{') and closerElement != closeMacroType):
if elem.startswith('{') and closerElement != closeMacroType:
scopes.append(closed)
elif (isOpener(elem) and closerElement != '[/'+elem[1:]
and '+'+closerElement != elem[1]+'[/'+elem[2:]):
elif isOpener(elem) and closerElement != '[/' + elem[1:] \
and '+' + closerElement != elem[1] + '[/' + elem[2:]:
self.printError('reached', closerElement, 'at line', self.lineno+1, 'before closing scope', elem)
scopes.append(closed) # to reduce additional errors (hopefully)
scopes.append(closed) # to reduce additional errors (hopefully)
return True
except IndexError:
return False
Expand Down Expand Up @@ -239,6 +238,8 @@ def parseElements(self, text):
"""
elements = [] #(elementType, sortPos, scopeDelta)
# first remove any lua strings
#text = re.sub('<<.*?>>', '', text)
#"""
beginquote = text.find('<<')
while beginquote >= 0:
endquote = text.find('>>', beginquote+2)
Expand All @@ -248,6 +249,7 @@ def parseElements(self, text):
else:
text = text[:beginquote] + text[endquote+2:]
beginquote = text.find('<<')
#"""
# remove any quoted strings
beginquote = text.find('"')
while beginquote >= 0:
Expand Down Expand Up @@ -409,12 +411,12 @@ def next(self):
self.element, nextScopes = self.parseElements(self.text)
self.nextScopes = []
for elem in nextScopes:
# remember scopes by storing a copy of the iterator
# remember scopes by storing a copy of the iterator
copyItor = self.copy()
copyItor.element = elem
self.nextScopes.append(copyItor)
copyItor.nextScopes.append(copyItor)
if(len(self.element) == 1):
if len(self.element) == 1:
# currently we only wish to handle simple single assignment syntax
self.element = self.element[0]
if self.endScope is not None and not self.scopes.count(self.endScope):
Expand Down

0 comments on commit 95a84bb

Please sign in to comment.