From d5231cf7ad1ff8324d52204e875078834e3cafdb Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Wed, 1 May 2019 20:32:47 -0500 Subject: [PATCH] Improve the multiline algorithm Require that the whitespace being stripped is a common prefix. --- lib/dhall/parser.rb | 11 ++++++----- lib/dhall/util.rb | 4 ++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/dhall/parser.rb b/lib/dhall/parser.rb index 1ab8e71..31a532d 100644 --- a/lib/dhall/parser.rb +++ b/lib/dhall/parser.rb @@ -276,14 +276,15 @@ module Dhall module SingleQuoteLiteral def value chunks = capture(:single_quote_continue).value - raw = chunks.join + "\n" - indent = raw.scan(/^[ \t]*(?=[^ \t\r\n])/).map(&:length).min - indent = 0 if raw.end_with?("\n\n") + raw = chunks.join + indent = raw.scan(/^[ \t]*(?=[^ \t\r\n])/).map(&:chars) + .reduce(&Util.method(:longest_common_prefix)).length + indent = 0 if raw.end_with?("\n") TextLiteral.for( *chunks - .chunk { |c| c != "\n" } - .flat_map { |(line, chunk)| line ? chunk[indent..-1] : chunk } + .chunk { |c| c != "\n" } + .flat_map { |(line, chunk)| line ? chunk[indent..-1] : chunk } ) end end diff --git a/lib/dhall/util.rb b/lib/dhall/util.rb index 0d4fa50..eea4f9e 100644 --- a/lib/dhall/util.rb +++ b/lib/dhall/util.rb @@ -177,5 +177,9 @@ module Dhall str.encode(Encoding::UTF_8) end end + + def self.longest_common_prefix(a, b) + a.zip(b).take_while { |(x, y)| x == y }.map(&:first) + end end end -- 2.34.7