Implement faster key/value parser.
This commit is contained in:
parent
debeb2fd59
commit
2514e93ce3
1 changed files with 23 additions and 12 deletions
|
@ -35,7 +35,6 @@
|
|||
|
||||
(import scheme
|
||||
(chicken base)
|
||||
(chicken irregex)
|
||||
testing)
|
||||
|
||||
;; 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
|
||||
;; the value were present.
|
||||
(define (parser-parse-line line)
|
||||
(if (= (string-length line) 0)
|
||||
#f
|
||||
(let ((dm (irregex-search (irregex "[ \\t]" 'u) line)))
|
||||
(if dm
|
||||
(let* ((sep-idx (irregex-match-start-index dm))
|
||||
(key-str (substring line 0 sep-idx))
|
||||
(key (string->symbol key-str))
|
||||
(sep+val (substring line sep-idx))
|
||||
(val (irregex-replace (irregex "^[ \\t]*" 'u) sep+val "")))
|
||||
(cons key val))
|
||||
(string->symbol line)))))
|
||||
(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
|
||||
(if (< vpos llen)
|
||||
(cons (string->symbol
|
||||
(substring line 0 spos))
|
||||
(substring line vpos))
|
||||
(string->symbol
|
||||
(substring line 0 spos))))))
|
||||
|
||||
;; Self-tests
|
||||
(define (parser-tests!)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue