#! /usr/bin/env python3
import ezdxf
import re
class StringBuffer:
"""
Store a string, an index and provide some operations.
"""
def __init__(self, string: str):
self._string = string
self._index = 0
self._stack = []
def __enter__(self):
self.enter()
return self
def __exit__(self, exc_type, _exc_value, _traceback):
if exc_type is None:
self.complete()
else:
self.backtrack()
def advance(self, length: int):
"""
Advance the index by length.
"""
self._index = min(len(self._string), self._index + length)
def backtrack(self):
"""
Track back from parsing something.
Pop index from stack and use it as the new current index.
"""
self._index = self._stack[-1]
del self._stack[-1]
def check(self, s: str) -> bool:
"""
Check if string s follows index.
If so, advance by length of s and return True
Otherwise, keep index and return False.
"""
if self.peek(len(s)) == s:
self.advance(len(s))
return True
return False