Implement faster key/value parser.

This commit is contained in:
Dominik Pantůček 2023-05-16 22:00:54 +02:00
parent debeb2fd59
commit 2514e93ce3

View file

@ -35,7 +35,6 @@
(import scheme (import scheme
(chicken base) (chicken base)
(chicken irregex)
testing) testing)
;; Pass 0: Removes any comments and removes any leading and trailing ;; Pass 0: Removes any comments and removes any leading and trailing
@ -69,17 +68,29 @@
;; one token was there and pair of symbol and string if both key and ;; one token was there and pair of symbol and string if both key and
;; the value were present. ;; the value were present.
(define (parser-parse-line line) (define (parser-parse-line line)
(if (= (string-length line) 0) (let* ((llen (string-length line))
(spos (let sloop ((sidx 0))
(if (or (= sidx llen)
(let ((ch (string-ref line sidx)))
(or (eq? ch #\space)
(eq? ch #\tab))))
sidx
(sloop (add1 sidx)))))
(vpos (let vloop ((vidx spos))
(if (or (= vidx llen)
(not (let ((ch (string-ref line vidx)))
(or (eq? ch #\space)
(eq? ch #\tab)))))
vidx
(vloop (add1 vidx))))))
(if (= llen 0)
#f #f
(let ((dm (irregex-search (irregex "[ \\t]" 'u) line))) (if (< vpos llen)
(if dm (cons (string->symbol
(let* ((sep-idx (irregex-match-start-index dm)) (substring line 0 spos))
(key-str (substring line 0 sep-idx)) (substring line vpos))
(key (string->symbol key-str)) (string->symbol
(sep+val (substring line sep-idx)) (substring line 0 spos))))))
(val (irregex-replace (irregex "^[ \\t]*" 'u) sep+val "")))
(cons key val))
(string->symbol line)))))
;; Self-tests ;; Self-tests
(define (parser-tests!) (define (parser-tests!)