Skip to content

Commit

Permalink
avoid being O(n^2) in worst case, improve style
Browse files Browse the repository at this point in the history
  • Loading branch information
simeonschaub committed May 12, 2021
1 parent 8600586 commit 5579e2d
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2114,35 +2114,35 @@
(define (unescape-parsed-string-literal strs)
(map-at even? unescape-string strs))

;; remove `\` followed by a newline
(define (strip-escaped-newline s)
(let ((in (open-input-string s))
(out (open-output-string)))
(define (loop preceding-backslash?)
(let ((c (read-char in)))
(cond ((eof-object? c))
(preceding-backslash?
(if (not (eqv? c #\newline))
(begin (write-char #\\ out) (write-char c out)))
(loop #f))
((eqv? c #\\) (loop #t))
(else (write-char c out) (loop #f)))))
(loop #f)
(io.tostring! out)))

(define (parse-string-literal s delim raw)
(let ((p (ts:port s)))
((if raw identity unescape-parsed-string-literal)
(map (lambda (s)
(if (and (not raw) (string? s))
;; remove `\` followed by a newline
(let ((spl (string-split s "\\\n")))
(foldl (lambda (line s)
;; if there is an odd number of backslashes before the backslash
;; preceding the newline, keep the backslash and newline since
;; the backslash is actually escaped
(define (odd-backslashes? (i (length s)))
(and (> i 0)
(let ((i (string.dec s i)))
(and (eqv? (string.char s i) #\\)
(not (odd-backslashes? i))))))
(if (odd-backslashes?)
(string s "\\\n" line)
(string s line)))
""
spl))
s))
(if (eqv? (peek-char p) delim)
(if (eqv? (peek-char (take-char p)) delim)
(map-first strip-leading-newline
(dedent-triplequoted-string
(parse-string-literal- 2 (take-char p) s delim raw)))
(list ""))
(parse-string-literal- 0 p s delim raw))))))
(let* ((p (ts:port s))
(str (if (eqv? (peek-char p) delim)
(if (eqv? (peek-char (take-char p)) delim)
(map-first strip-leading-newline
(dedent-triplequoted-string
(parse-string-literal- 2 (take-char p) s delim raw)))
(list ""))
(parse-string-literal- 0 p s delim raw))))
(if raw str (unescape-parsed-string-literal
(map (lambda (s)
(if (string? s) (strip-escaped-newline s) s))
str)))))

(define (strip-leading-newline s)
(let ((n (sizeof s)))
Expand Down

0 comments on commit 5579e2d

Please sign in to comment.