Skip to content

Commit

Permalink
👌 Improve performance of skipSpaces/skipChars (#271)
Browse files Browse the repository at this point in the history
Don't compute src length on every iteration
  • Loading branch information
chrisjsewell authored Jun 1, 2023
1 parent f52249e commit 36a428b
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions markdown_it/rules_block/state_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,12 @@ def skipEmptyLines(self, from_pos: int) -> int:

def skipSpaces(self, pos: int) -> int:
"""Skip spaces from given position."""
while pos < len(self.src):
if not isStrSpace(self.src[pos]):
while True:
try:
current = self.src[pos]
except IndexError:
break
if not isStrSpace(current):
break
pos += 1
return pos
Expand All @@ -165,16 +169,24 @@ def skipSpacesBack(self, pos: int, minimum: int) -> int:

def skipChars(self, pos: int, code: int) -> int:
"""Skip character code from given position."""
while pos < len(self.src):
if self.srcCharCode[pos] != code:
while True:
try:
current = self.srcCharCode[pos]
except IndexError:
break
if current != code:
break
pos += 1
return pos

def skipCharsStr(self, pos: int, ch: str) -> int:
"""Skip character string from given position."""
while pos < len(self.src):
if self.src[pos] != ch:
while True:
try:
current = self.src[pos]
except IndexError:
break
if current != ch:
break
pos += 1
return pos
Expand Down

0 comments on commit 36a428b

Please sign in to comment.