changeset 54:79b3b77fd4bf draft

Make string parsing ~2x faster by not going character by character
author Lewin Bormann <lbo@spheniscida.de>
date Fri, 24 May 2019 00:07:58 +0200
parents 2202ec2712c8
children 20b76a3c3211
files pcombinators/primitives.py
diffstat 1 files changed, 4 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/pcombinators/primitives.py	Fri May 24 00:07:39 2019 +0200
+++ b/pcombinators/primitives.py	Fri May 24 00:07:58 2019 +0200
@@ -27,16 +27,10 @@
         self._s = s
 
     def parse(self, st):
-        hold = st.hold()
-        s = self._s
-        i = 0
-        while i < len(s) and s[i] == st.peek():
-            st.next()
-            i += 1
-        if i == len(s):
-            st.release(hold)
-            return (self._s, st)
-        st.reset(hold)
+        potential = st.remaining(len(self._s))
+        if potential.startswith(self._s):
+            st.advance(len(self._s))
+            return self._s, st
         return (None, st)
 
 class OneOf(Parser):