From 4f4938e066cc995f70bec801a3073e3d0277abe5 Mon Sep 17 00:00:00 2001 From: Mike Nolta Date: Fri, 26 Jun 2015 16:00:27 -0400 Subject: [PATCH] triple quoted strings: don't dedent interpolates Before: julia> s = """ $("\n ") """ "\n\n" After: julia> s = """ $("\n ") """ "\n \n" Plus some minor cleanup of 333bb876d. --- src/julia-parser.scm | 44 ++++++++++++++++++++++++++++---------------- test/strings.jl | 3 +++ 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/julia-parser.scm b/src/julia-parser.scm index 7809c04439c68..29a0ea3d9d23f 100644 --- a/src/julia-parser.scm +++ b/src/julia-parser.scm @@ -1632,33 +1632,45 @@ (define (take-char p) (begin (read-char p) p)) +; map the first element of lst +(define (map-first f lst) + (if (null? lst) () + (cons (f (car lst)) (cdr lst)))) + +; map the elements of lst where (pred index) is true +; e.g., (map-at odd? (lambda (x) 0) '(a b c d)) -> '(a 0 c 0) +(define (map-at pred f lst) + (define (map-at- pred f lst i r) + (if (null? lst) (reverse r) + (let* ((x (car lst)) + (y (if (pred i) (f x) x))) + (map-at- pred f (cdr lst) (+ i 1) (cons y r))))) + (map-at- pred f lst 0 ())) + (define (parse-string-literal s custom) (let ((p (ts:port s))) (if (eqv? (peek-char p) #\") (if (eqv? (peek-char (take-char p)) #\") - (strip-first-newline + (map-first strip-leading-newline (dedent-triplequoted-string (parse-string-literal- 2 (take-char p) s custom))) (list "")) (parse-string-literal- 0 p s custom)))) -(define (strip-first-newline lst) - (let* ((f (car lst)) - (n (sizeof f))) - (if (and (> n 0) (eqv? (string.char f 0) #\newline)) - (cons (string.sub f 1 n) (cdr lst)) - lst))) +(define (strip-leading-newline s) + (if (and (> (sizeof s) 0) (eqv? (string.char s 0) #\newline)) + (string.tail s 1) + s)) (define (dedent-triplequoted-string lst) (let ((prefix (triplequoted-string-indentation lst))) (if (length> prefix 0) - (map (lambda (s) - (if (string? s) - (string-swap s - (list->string (cons #\newline prefix)) - #\newline) - s)) - lst) + (map-at even? + (lambda (s) + (string-replace s + (list->string (cons #\newline prefix)) + #\newline)) + lst) lst))) (define (triplequoted-string-indentation lst) @@ -1716,8 +1728,8 @@ (string-split- s sep (+ i (sizeof sep)) (cons (string.sub s start i) splits)) (reverse (cons (string.sub s start (sizeof s)) splits))))) -; swap all occurrences of a in s with b -(define (string-swap s a b) +; replace all occurrences of a in s with b +(define (string-replace s a b) (string.join (string-split s a) b)) (define (parse-interpolate s) diff --git a/test/strings.jl b/test/strings.jl index c3fde28cbbc27..cde9f51cabfa2 100644 --- a/test/strings.jl +++ b/test/strings.jl @@ -937,6 +937,9 @@ s = " p" @test """ foo bar\t""" == "foo$(nl)bar\t" +@test """ + $("\n ") + """ == "\n \n" # bytes2hex and hex2bytes hex_str = "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"