05.11., 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

Commit bcaa931b authored by eckhart's avatar eckhart

- slightly better cython optimizations

parent 5249535b
......@@ -748,7 +748,7 @@ class Grammar:
this is done lazily.
"""
if not self._reversed__:
self._reversed__ = StringView(self.document__.text[::-1])
self._reversed__ = StringView(self.document__.get_text()[::-1])
return self._reversed__
......
......@@ -9,73 +9,20 @@ import cython
# type hints for Cython python -> C compiler to speed up the most
# critical code paths of stringview.py.
cdef int first_char(text, int begin, int end)
cdef int last_char(text, int begin, int end)
cdef int first_char(str text, int begin, int end)
cdef int last_char(str text, int begin, int end)
cdef int pack_index(int index, int length)
@cython.locals(cbegin=cython.int, cend=cython.int)
cdef real_indices(begin, end, int length)
# cpdef real_indices(begin, end, int length)
cpdef real_indices(begin, end, int length)
# cdefs for class StringView: https://cython.readthedocs.io/en/latest/src/tutorial/pure.html
cdef class StringView:
cdef str text
cdef int begin, end, len
cdef str fullstring
cpdef __init__(self, text: str, begin: Optional[int] = 0, end: Optional[int] = None) -> None
cpdef __bool__(self) -> bool
cpdef __len__(self) -> int
cpdef __str__(self) -> str
cpdef __eq__(self, other) -> bool
cpdef __hash__(self) -> int
cpdef __add__(self, other) -> Union[str, 'StringView']
cpdef __radd__(self, other) -> Union[str, 'StringView']
@cython.locals(start=cython.int, stop=cython.int)
cpdef __getitem__(self, index: Optional[slice, int]) -> StringView
cpdef count(self, sub: str, start: Optional[int] = None, end: Optional[int] = None) -> int
cpdef find(self, sub: str, start: Optional[int] = None, end: Optional[int] = None) -> int
cpdef rfind(self, sub: str, start: Optional[int] = None, end: Optional[int] = None) -> int
cpdef startswith(self, prefix: str, start: int = 0, end: Optional[int] = None) -> bool
cpdef endswith(self, suffix: str, start: int = 0, end: Optional[int] = None) -> bool
cpdef match(self, regex, flags=0)
cpdef index(self, absolute_index: int) -> int
cpdef indices(self, absolute_indices: Iterable[int]) -> Tuple[int, ...]
cpdef search(self, regex)
cpdef finditer(self, regex)
@cython.locals(begin=cython.int, end=cython.int)
cpdef strip(self)
@cython.locals(begin=cython.int)
cpdef lstrip(self)
@cython.locals(end=cython.int)
cpdef rstrip(self)
@cython.locals(length=cython.int, k=cython.int, i=cython.int)
cpdef split(self, sep=None)
cdef str _text
cdef int _begin, _end, _len
cdef str _fullstring
cpdef replace(self, old, new)
This diff is collapsed.
......@@ -952,7 +952,7 @@ def parse_sxpr(sxpr: Union[str, StringView]) -> Node:
if match is None:
raise AssertionError('Malformed S-expression Node-tagname or identifier expected, '
'not "%s"' % sxpr[:40].replace('\n', ''))
end = match.end() - sxpr.begin
end = match.end() - sxpr.get_begin()
tagname = sxpr[:end]
name, class_name = (tagname.split(':') + [''])[:2]
sxpr = sxpr[end:].strip()
......@@ -986,14 +986,14 @@ def parse_sxpr(sxpr: Union[str, StringView]) -> Node:
for qtmark in ['"""', "'''", '"', "'"]:
match = sxpr.match(re.compile(qtmark + r'.*?' + qtmark, re.DOTALL))
if match:
end = match.end() - sxpr.begin
end = match.end() - sxpr.get_begin()
i = len(qtmark)
lines.append(str(sxpr[i:end - i]))
sxpr = sxpr[end:].strip()
break
else:
match = sxpr.match(re.compile(r'(?:(?!\)).)*', re.DOTALL))
end = match.end() - sxpr.begin
end = match.end() - sxpr.get_begin()
lines.append(str(sxpr[:end]))
sxpr = sxpr[end:]
result = "\n".join(lines)
......@@ -1026,7 +1026,7 @@ def parse_xml(xml: Union[str, StringView]) -> Node:
for match in s.finditer(re.compile(r'\s*(?P<attr>\w+)\s*=\s*"(?P<value>.*)"\s*')):
d = match.groupdict()
attributes[d['attr']] = d['value']
restart = match.end() - s.begin
restart = match.end() - s.get_begin()
return (s[restart:], attributes)
def parse_opening_tag(s: StringView) -> Tuple[StringView, str, OrderedDict, bool]:
......@@ -1038,7 +1038,7 @@ def parse_xml(xml: Union[str, StringView]) -> Node:
match = s.match(re.compile(r'<\s*(?P<tagname>[\w:]+)\s*'))
assert match
tagname = match.groupdict()['tagname']
section = s[match.end() - s.begin:]
section = s[match.end() - s.get_begin():]
s, attributes = parse_attributes(section)
i = s.find('>')
assert i >= 0
......@@ -1051,7 +1051,7 @@ def parse_xml(xml: Union[str, StringView]) -> Node:
match = s.match(re.compile(r'</\s*(?P<tagname>[\w:]+)>'))
assert match
tagname = match.groupdict()['tagname']
return s[match.end() - s.begin:], tagname
return s[match.end() - s.get_begin():], tagname
def parse_leaf_content(s: StringView) -> Tuple[StringView, StringView]:
"""Parses a piece of the content of a tag, just until the next opening,
......
......@@ -234,7 +234,7 @@ def create_project(path: str):
def selftest() -> bool:
"""Run a simple self-text of DHParser.
"""Run a simple self-test of DHParser.
"""
print("DHParser selftest...")
print("\nSTAGE I: Trying to compile EBNF-Grammar:\n")
......
......@@ -7,7 +7,7 @@ import multiprocessing
import os
import platform
import time
sys
import sys
def run_tests(command):
testtype = 'DOCTEST' if command.find('doctest') >= 0 else 'UNITTEST'
......
......@@ -67,7 +67,7 @@ class TestStringView:
s = " 0123456789 "
sv = StringView(s, 1, -1)
assert len(sv) == 10
assert sv.len == 10
# assert sv.len == 10
assert len(sv[5:5]) == 0
assert len(sv[7:4]) == 0
assert len(sv[-12:-2]) == 8
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment