From de03049c5abd1222c80a019c72960d833e75dbb8 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Tue, 3 Aug 2021 00:34:34 +0200 Subject: [PATCH] Fix ViewString and DisplayString for ranges ... to match ViewObj and Display. As a result, Display, PrintObj, DisplayString, PrintString and String now all behave the same for ranges, while ViewObj and ViewString differ. This is probably quite surprising to people, but changing it one way or the other will affect packages, so for now let's play it safe and only address the "obvious" bugs. --- lib/list.gi | 32 ++++++++++++++++++++++++++++++-- tst/testinstall/range.tst | 16 ++++++++-------- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/lib/list.gi b/lib/list.gi index 50f3cbc212..e2cce07929 100644 --- a/lib/list.gi +++ b/lib/list.gi @@ -382,6 +382,34 @@ local str,ls, i; return str; end ); +InstallMethod( DisplayString, + "for a range", + [ IsRange ], + range -> Concatenation( String( range ), "\n" ) ); + +InstallMethod( ViewString, + "for a range", + [ IsRange ], + function( list ) + local str; + str := "[ "; + Append( str, String( list[1] ) ); + if Length( list ) > 1 then + if Length(list) = 2 or list[2] - list[1] <> 1 then + Append( str, ", " ); + Append( str, String( list[2] ) ); + fi; + if Length(list) > 2 then + Append( str, " .. " ); + Append( str, String( list[ Length( list ) ] ) ); + fi; + fi; + Append( str, " ]" ); + Assert(0, IsStringRep(str)); + ConvertToStringRep( str ); + return str; + end ); + InstallMethod( String, "for a range", [ IsRange ], @@ -3829,7 +3857,7 @@ LIST_WITH_IDENTICAL_ENTRIES ); ## and in the 'ViewString' method for finite lists. ## InstallMethod( ViewObj, - "for finite lists", + "for a finite list", [ IsList and IsFinite ], {} -> RankFilter(IsList) + 1 - RankFilter(IsList and IsFinite), function( list ) @@ -3857,7 +3885,7 @@ function( list ) end ); InstallMethod( ViewObj, - "for ranges", + "for a range", [ IsList and IsFinite and IsRange ], function( list ) Print( "[ " ); diff --git a/tst/testinstall/range.tst b/tst/testinstall/range.tst index f72777957a..a28abb3854 100644 --- a/tst/testinstall/range.tst +++ b/tst/testinstall/range.tst @@ -40,7 +40,7 @@ gap> TestPrintRangeRep:=function(r) # gap> TestPrintRangeRep([0..1]); Display: [ 0 .. 1 ] -DisplayString: +DisplayString: [ 0 .. 1 ] ViewObj: [ 0, 1 ] ViewString: [ 0, 1 ] PrintObj: [ 0 .. 1 ] @@ -48,31 +48,31 @@ PrintString: [ 0 .. 1 ] String: [ 0 .. 1 ] gap> TestPrintRangeRep([0..2]); Display: [ 0 .. 2 ] -DisplayString: +DisplayString: [ 0 .. 2 ] ViewObj: [ 0 .. 2 ] -ViewString: [ 0, 1, 2 ] +ViewString: [ 0 .. 2 ] PrintObj: [ 0 .. 2 ] PrintString: [ 0 .. 2 ] String: [ 0 .. 2 ] gap> TestPrintRangeRep([0,2..4]); Display: [ 0, 2 .. 4 ] -DisplayString: +DisplayString: [ 0, 2 .. 4 ] ViewObj: [ 0, 2 .. 4 ] -ViewString: [ 0, 2, 4 ] +ViewString: [ 0, 2 .. 4 ] PrintObj: [ 0, 2 .. 4 ] PrintString: [ 0, 2 .. 4 ] String: [ 0, 2 .. 4 ] gap> TestPrintRangeRep([0,-1..-2]); Display: [ 0, -1 .. -2 ] -DisplayString: +DisplayString: [ 0, -1 .. -2 ] ViewObj: [ 0, -1 .. -2 ] -ViewString: [ 0, -1, -2 ] +ViewString: [ 0, -1 .. -2 ] PrintObj: [ 0, -1 .. -2 ] PrintString: [ 0, -1 .. -2 ] String: [ 0, -1 .. -2 ] gap> TestPrintRangeRep([0,-1..-1]); Display: [ 0, -1 .. -1 ] -DisplayString: +DisplayString: [ 0, -1 .. -1 ] ViewObj: [ 0, -1 ] ViewString: [ 0, -1 ] PrintObj: [ 0, -1 .. -1 ]