Skip to content

Commit 5579e2d

Browse files
committed
avoid being O(n^2) in worst case, improve style
1 parent 8600586 commit 5579e2d

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

src/julia-parser.scm

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,35 +2114,35 @@
21142114
(define (unescape-parsed-string-literal strs)
21152115
(map-at even? unescape-string strs))
21162116

2117+
;; remove `\` followed by a newline
2118+
(define (strip-escaped-newline s)
2119+
(let ((in (open-input-string s))
2120+
(out (open-output-string)))
2121+
(define (loop preceding-backslash?)
2122+
(let ((c (read-char in)))
2123+
(cond ((eof-object? c))
2124+
(preceding-backslash?
2125+
(if (not (eqv? c #\newline))
2126+
(begin (write-char #\\ out) (write-char c out)))
2127+
(loop #f))
2128+
((eqv? c #\\) (loop #t))
2129+
(else (write-char c out) (loop #f)))))
2130+
(loop #f)
2131+
(io.tostring! out)))
2132+
21172133
(define (parse-string-literal s delim raw)
2118-
(let ((p (ts:port s)))
2119-
((if raw identity unescape-parsed-string-literal)
2120-
(map (lambda (s)
2121-
(if (and (not raw) (string? s))
2122-
;; remove `\` followed by a newline
2123-
(let ((spl (string-split s "\\\n")))
2124-
(foldl (lambda (line s)
2125-
;; if there is an odd number of backslashes before the backslash
2126-
;; preceding the newline, keep the backslash and newline since
2127-
;; the backslash is actually escaped
2128-
(define (odd-backslashes? (i (length s)))
2129-
(and (> i 0)
2130-
(let ((i (string.dec s i)))
2131-
(and (eqv? (string.char s i) #\\)
2132-
(not (odd-backslashes? i))))))
2133-
(if (odd-backslashes?)
2134-
(string s "\\\n" line)
2135-
(string s line)))
2136-
""
2137-
spl))
2138-
s))
2139-
(if (eqv? (peek-char p) delim)
2140-
(if (eqv? (peek-char (take-char p)) delim)
2141-
(map-first strip-leading-newline
2142-
(dedent-triplequoted-string
2143-
(parse-string-literal- 2 (take-char p) s delim raw)))
2144-
(list ""))
2145-
(parse-string-literal- 0 p s delim raw))))))
2134+
(let* ((p (ts:port s))
2135+
(str (if (eqv? (peek-char p) delim)
2136+
(if (eqv? (peek-char (take-char p)) delim)
2137+
(map-first strip-leading-newline
2138+
(dedent-triplequoted-string
2139+
(parse-string-literal- 2 (take-char p) s delim raw)))
2140+
(list ""))
2141+
(parse-string-literal- 0 p s delim raw))))
2142+
(if raw str (unescape-parsed-string-literal
2143+
(map (lambda (s)
2144+
(if (string? s) (strip-escaped-newline s) s))
2145+
str)))))
21462146

21472147
(define (strip-leading-newline s)
21482148
(let ((n (sizeof s)))

0 commit comments

Comments
 (0)