From 11df702fb45c6f3176942a10ebe6d723b3f5575a Mon Sep 17 00:00:00 2001 From: Chris Jefferson Date: Fri, 30 Apr 2021 15:03:48 +0100 Subject: [PATCH] Fix printing of strings inside functions Previously we normalised all whitespace in printed out functions, which would effect whitespace inside strings. --- lib/function.gi | 14 +++++++++++--- tst/testinstall/function.tst | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lib/function.gi b/lib/function.gi index 60fe73a3e63..695c072a278 100644 --- a/lib/function.gi +++ b/lib/function.gi @@ -78,13 +78,21 @@ end); InstallMethod(String, "for a function, with whitespace reduced", [IsFunction and IsInternalRep], function(fun) - local s, stream; + local s, stream,lines, pos, i; s := ""; stream := OutputTextString(s, true); PrintTo(stream, fun); CloseStream(stream); - NormalizeWhitespace(s); - return MakeImmutable(s); + lines := SplitString(s, "\n"); + for i in [1..Length(lines)] do + Chomp(lines[i]); + pos := 1; + while pos <= Length(lines[i]) and lines[i][pos] = ' ' do + pos := pos + 1; + od; + lines[i] := lines[i]{[pos..Length(lines[i])]}; + od; + return MakeImmutable(JoinStringsWithSeparator(lines, " ")); end); BIND_GLOBAL( "VIEW_STRING_OPERATION", diff --git a/tst/testinstall/function.tst b/tst/testinstall/function.tst index 92e683630b9..84c26cea4c5 100644 --- a/tst/testinstall/function.tst +++ b/tst/testinstall/function.tst @@ -137,6 +137,31 @@ gap> Print({x,y} -> x + y, "\n"); function ( x, y ) return x + y; end +gap> String({x,y} -> x + y); +"function ( x, y ) return x + y; end" + +# Test nesting +gap> Print(function(x) if x then if x then while x do od; fi; fi; end, "\n"); +function ( x ) + if x then + if x then + while x do + ; + od; + fi; + fi; + return; +end +gap> String(function(x) if x then if x then while x do od; fi; fi; end); +"function ( x ) if x then if x then while x do ; od; fi; fi; return; end" + +# Check strings in functions +gap> Print({x} -> "a b","\n"); +function ( x ) + return "a b"; +end +gap> String({x} -> "a b"); +"function ( x ) return \"a b\"; end" gap> f := ({x,y} -> x + y); function( x, y ) ... end gap> f(2,3);