Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use short-circuit where possible #73

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions src/gnatcoll-arg_lists.adb
Original file line number Diff line number Diff line change
Expand Up @@ -537,11 +537,7 @@ package body GNATCOLL.Arg_Lists is

if Protect_Quotes then
for S in List (L)'Range loop
if List (L)(S) = '"'
or else List (L)(S) = ' '
or else List (L) (S) = '\'
or else List (L) (S) = '''
then
if List (L) (S) in '"' | ' ' | '\' | ''' then
Length := Length + 1;
end if;
end loop;
Expand All @@ -555,13 +551,9 @@ package body GNATCOLL.Arg_Lists is
for L in List'Range loop
for J in List (L)'Range loop
if Protect_Quotes then
if List (L) (J) = '"'
or else List (L) (J) = ' '
or else List (L) (J) = '\'
or else List (L) (J) = '''
then
if List (L) (J) in '"' | ' ' | '\' | ''' then
S (Index) := '\';
Index := Index + 1;
Index := Index + 1;
end if;
end if;
S (Index) := List (L)(J);
Expand Down
2 changes: 1 addition & 1 deletion src/gnatcoll-coders-base64.adb
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ package body GNATCOLL.Coders.Base64 is

overriding function Finished (Coder : Decoder_Type) return Boolean is
begin
return Coder.Finish and not Coder.Has;
return Coder.Finish and then not Coder.Has;
end Finished;

-----------
Expand Down
2 changes: 1 addition & 1 deletion src/gnatcoll-email-mailboxes.adb
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ package body GNATCOLL.Email.Mailboxes is
is
C : Container_Access;
begin
if Parent_Cont = null or else Parent_Cont = Cont then
if Parent_Cont in null | Cont then
return;
end if;

Expand Down
6 changes: 3 additions & 3 deletions src/gnatcoll-email-parser.adb
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ package body GNATCOLL.Email.Parser is

case N (N'First) is
when 'c' =>
return N = "cc" or else N = "content-type";
return N in "cc" | "content-type";
when 'd' =>
return N = "date";
when 'f' =>
return N = "from";
when 'i' =>
return N = "in-reply-to";
when 'm' =>
return N = "message-id" or else N = "mime-version";
return N in "message-id" | "mime-version";
when 'r' =>
return N = "references" or else N = "reply-to";
return N in "references" | "reply-to";
when 's' =>
return N = "subject";
when 't' =>
Expand Down
65 changes: 21 additions & 44 deletions src/gnatcoll-email-utils.adb
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ package body GNATCOLL.Email.Utils is
is
Start : constant Integer := Index;
begin
if S (Index) = '-' or else S (Index) = '+' then
if S (Index) in '-' | '+' then
Index := Index + 1;
end if;

Expand Down Expand Up @@ -360,11 +360,9 @@ package body GNATCOLL.Email.Utils is
begin
-- Timezone (we might have none in badly formed dates)
if Index < Date'Last then
if Date (Index) = '-' or else Date (Index) = '+' or else
Date (Index) in '0' .. '9'
then
Read_Integer (Date (Index .. Date'Last), Index,
Value => TZ_Local);
if Date (Index) in '-' | '+' | '0' .. '9' then
Read_Integer
(Date (Index .. Date'Last), Index, Value => TZ_Local);
TZ := Time_Offset ((TZ_Local / 100) * 60 + TZ_Local mod 100);
else

Expand All @@ -380,8 +378,9 @@ package body GNATCOLL.Email.Utils is
then
TZ := 0;
else
TZ := Named_TZ_Offset
(Named_TZ'Value (Date (Index .. Index + 2)));
TZ :=
Named_TZ_Offset
(Named_TZ'Value (Date (Index .. Index + 2)));
end if;
end if;
end if;
Expand Down Expand Up @@ -642,10 +641,7 @@ package body GNATCOLL.Email.Utils is
-- Skip spaces

while From <= Str'Last
and then (Str (From) = ASCII.LF
or else Str (From) = ASCII.CR
or else Str (From) = ASCII.HT
or else Str (From) = ' ')
and then (Str (From) in ASCII.LF | ASCII.CR | ASCII.HT | ' ')
loop
From := From + 1;
end loop;
Expand Down Expand Up @@ -682,15 +678,11 @@ package body GNATCOLL.Email.Utils is

-- ',' is the standard separator in mail messages, but ';' is
-- often used by users when manually typing a list of addresses
elsif Str (From) = ','
or else Str (From) = ';'
or else Str (From) = ASCII.LF
or else Str (From) = ASCII.CR
or else Str (From) = ASCII.HT
elsif Str (From) in ',' | ';' | ASCII.LF | ASCII.CR | ASCII.HT
or else (Buffer_Has_At and then Str (From) = ' ')
then
-- End of current address
From := From + 1;
From := From + 1;
Found := True;
return;

Expand Down Expand Up @@ -1180,12 +1172,10 @@ package body GNATCOLL.Email.Utils is
Is_EOL : Boolean) return Boolean
is
begin
if Char = ' ' or else Char = ASCII.HT then
if Char in ' ' | ASCII.HT then
return Is_EOL or else Where in Any_Header;

elsif Char = '='
or else Char = '?'
or else Character'Pos (Char) not in 32 .. 126
elsif Char in '=' | '?' or else Character'Pos (Char) not in 32 .. 126
then
return True;

Expand All @@ -1211,7 +1201,7 @@ package body GNATCOLL.Email.Utils is

-- No need to quote whitespace unless at EOL

if (Str (J) = ' ' or else Str (J) = ASCII.HT) and then not EOL then
if (Str (J) in ' ' | ASCII.HT) and then not EOL then
null;

elsif Needs_Quoting (Str (J), Where, EOL) then
Expand Down Expand Up @@ -1695,27 +1685,14 @@ package body GNATCOLL.Email.Utils is

if Set = Charset_US_ASCII then
Encoding := Encoding_7bit;
elsif Set = Charset_ISO_8859_1
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you want this code to be rewritten?
given that you want remove code reformatting, and preserve formatting logic in sections that are being refactored.

Note that the newly rewritten code must be pretty printed,
since otherwise the rewritten code might violate style rules (as specified in the project file).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case a too long line is certainly possible ;-)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am referring to places where reformatting is not motivated by anything, like gnatcoll-memory.adb:291

Copy link
Collaborator

@t-14 t-14 Dec 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you want this code to be rewritten?

In this specific case, try to keep the same terms on each line as it was previously. We used to have one line for latin-3 and another for latin-4, your patch reformats it so references to latin-4 are on two different lines, this harms readability.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You did however not turn off pretty printing,
so running the pretty printer might have changed your constraints.

It is an automated change, but it is a nice case of requirements for rewriting existing code.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You did however not turn off pretty printing,

I don't understand. "turn off pretty printing" where? gnatcoll is obviously hand-formatted.

or else Set = "latin_1" or else Set = "latin-1"
or else Set = Charset_ISO_8859_2
or else Set = "latin_2" or else Set = "latin-2"
or else Set = Charset_ISO_8859_3
or else Set = "latin_3" or else Set = "latin-3"
or else Set = Charset_ISO_8859_4
or else Set = "latin_4" or else Set = "latin-4"
or else Set = Charset_ISO_8859_9
or else Set = "latin_5" or else Set = "latin-5"
or else Set = Charset_ISO_8859_10
or else Set = "latin_6" or else Set = "latin-6"
or else Set = Charset_ISO_8859_13
or else Set = "latin_7" or else Set = "latin-7"
or else Set = Charset_ISO_8859_14
or else Set = "latin_8" or else Set = "latin-8"
or else Set = Charset_ISO_8859_15
or else Set = "latin_9" or else Set = "latin-9"
or else Set = Charset_Windows_1252
or else Set = "viscii"
or else Set = Charset_UTF_8 or else Set = "utf8"
elsif Set in Charset_ISO_8859_1 | "latin_1" | "latin-1" |
Charset_ISO_8859_2 | "latin_2" | "latin-2" | Charset_ISO_8859_3 |
"latin_3" | "latin-3" | Charset_ISO_8859_4 | "latin_4" |
"latin-4" | Charset_ISO_8859_9 | "latin_5" | "latin-5" |
Charset_ISO_8859_10 | "latin_6" | "latin-6" | Charset_ISO_8859_13 |
"latin_7" | "latin-7" | Charset_ISO_8859_14 | "latin_8" |
"latin-8" | Charset_ISO_8859_15 | "latin_9" | "latin-9" |
Charset_Windows_1252 | "viscii" | Charset_UTF_8 | "utf8"
then
Encoding := Encoding_QP;
else
Expand Down
11 changes: 3 additions & 8 deletions src/gnatcoll-email.adb
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,7 @@ package body GNATCOLL.Email is
L_Name : constant String := To_Lower (Name);
begin

if L_Name = "from"
or else L_Name = "sender"
or else L_Name = "to"
or else L_Name = "cc"
or else L_Name = "bcc"
then
if L_Name in "from" | "sender" | "to" | "cc" | "bcc" then
return Addr_Header;
else
return Other_Header;
Expand All @@ -154,7 +149,7 @@ package body GNATCOLL.Email is

function Is_Whitespace (Char : Character) return Boolean is
begin
return Char = ' ' or Char = ASCII.HT;
return Char in ' ' | ASCII.HT;
end Is_Whitespace;

----------------------
Expand Down Expand Up @@ -1297,7 +1292,7 @@ package body GNATCOLL.Email is
end if;

else
if MIME_Type /= "" and not Prepend then
if MIME_Type /= "" and then not Prepend then
Replace_Header (Msg, H_CT);
if H_CTE = Null_Header then
Delete_Headers (Msg, Content_Transfer_Encoding);
Expand Down
5 changes: 2 additions & 3 deletions src/gnatcoll-geometry.adb
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,10 @@ package body GNATCOLL.Geometry is
Bis2 : constant Line := Bisector (Segment'(1 => P2, 2 => P3));
Center : constant Point := Intersection (Bis1, Bis2);
begin
if Center = No_Point or else Center = Infinity_Points then
if Center in No_Point | Infinity_Points then
return No_Circle;
else
return (Center => Center,
Radius => Distance (Center, P1));
return (Center => Center, Radius => Distance (Center, P1));
end if;
end To_Circle;

Expand Down
15 changes: 6 additions & 9 deletions src/gnatcoll-io-native.adb
Original file line number Diff line number Diff line change
Expand Up @@ -664,16 +664,13 @@ package body GNATCOLL.IO.Native is
GNAT.Directory_Operations.Read (D, Item, Last);
exit when Last = 0;

if Item (1 .. Last) /= "."
and then Item (1 .. Last) /= ".."
if Item (1 .. Last) not in "." | ".."
and then
(not Dirs_Only
or else
GNAT.OS_Lib.Is_Directory (Name & Item (1 .. Last)))
(not Dirs_Only
or else GNAT.OS_Lib.Is_Directory (Name & Item (1 .. Last)))
and then
(not Files_Only
or else
GNAT.OS_Lib.Is_Regular_File (Name & Item (1 .. Last)))
(not Files_Only
or else GNAT.OS_Lib.Is_Regular_File (Name & Item (1 .. Last)))
then
if Ret = null then
Ret := new GNAT.Strings.String_List (1 .. 10);
Expand All @@ -685,7 +682,7 @@ package body GNATCOLL.IO.Native is
Ret := Tmp;
end if;

N := N + 1;
N := N + 1;
Ret (N) := new String'(Item (1 .. Last));
end if;
end loop;
Expand Down
13 changes: 6 additions & 7 deletions src/gnatcoll-io-remote-unix.adb
Original file line number Diff line number Diff line change
Expand Up @@ -677,8 +677,7 @@ package body GNATCOLL.IO.Remote.Unix is
exit when Matched (0) = No_Match;
Index := Matched (1).Last + 1;

if Output (Matched (1).First .. Matched (1).Last) /= "."
and then Output (Matched (1).First .. Matched (1).Last) /= ".."
if Output (Matched (1).First .. Matched (1).Last) not in "." | ".."
then
Nb_Files := Nb_Files + 1;
end if;
Expand All @@ -696,12 +695,12 @@ package body GNATCOLL.IO.Remote.Unix is
exit when Matched (0) = No_Match;
Index := Matched (1).Last + 1;

if Output (Matched (1).First .. Matched (1).Last) /= "."
and then Output (Matched (1).First .. Matched (1).Last)
/= ".."
if Output (Matched (1).First .. Matched (1).Last) not in "." |
".."
then
List (File_Idx) := new String'
(Output (Matched (1).First .. Matched (1).Last));
List (File_Idx) :=
new String'
(Output (Matched (1).First .. Matched (1).Last));
File_Idx := File_Idx + 1;
end if;
end loop;
Expand Down
15 changes: 7 additions & 8 deletions src/gnatcoll-io-remote-windows.adb
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ package body GNATCOLL.IO.Remote.Windows is
Exec.Execute_Remotely (Args, Output, Status);
Free (Args);

if Status and Output /= null then
if Status and then Output /= null then
S := GNATCOLL.Utils.Split (Output.all, ' ');

begin
Expand Down Expand Up @@ -760,8 +760,7 @@ package body GNATCOLL.IO.Remote.Windows is
exit when Matched (0) = No_Match;
Index := Matched (1).Last + 1;

if Output (Matched (1).First .. Matched (1).Last) /= "."
and then Output (Matched (1).First .. Matched (1).Last) /= ".."
if Output (Matched (1).First .. Matched (1).Last) not in "." | ".."
then
Nb_Files := Nb_Files + 1;
end if;
Expand All @@ -779,12 +778,12 @@ package body GNATCOLL.IO.Remote.Windows is
exit when Matched (0) = No_Match;
Index := Matched (1).Last + 1;

if Output (Matched (1).First .. Matched (1).Last) /= "."
and then Output (Matched (1).First .. Matched (1).Last)
/= ".."
if Output (Matched (1).First .. Matched (1).Last) not in "." |
".."
then
List (File_Idx) := new String'
(Output (Matched (1).First .. Matched (1).Last));
List (File_Idx) :=
new String'
(Output (Matched (1).First .. Matched (1).Last));
File_Idx := File_Idx + 1;
end if;
end loop;
Expand Down
2 changes: 1 addition & 1 deletion src/gnatcoll-io-remote.adb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ package body GNATCOLL.IO.Remote is
begin
-- Regexps might return file strings with a trailing CR or LF. Let's
-- remove those before creating the File record.
while Path (Last) = ASCII.CR or Path (Last) = ASCII.LF loop
while Path (Last) in ASCII.CR | ASCII.LF loop
Last := Last - 1;
end loop;

Expand Down
7 changes: 3 additions & 4 deletions src/gnatcoll-memory.adb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ package body GNATCOLL.Memory is
procedure Free (Ptr : System.Address) is
begin

if Ptr /= System.Null_Address and not Memory_Check then
if Ptr /= System.Null_Address and then not Memory_Check then
if Memory_Monitor then

Initialize_System_Memory_Debug_Pool;
Expand Down Expand Up @@ -287,9 +287,8 @@ package body GNATCOLL.Memory is

Memory_Check := Disable_Free;

if Activate_Monitor and not Memory_Monitor then
Initialize_System_Memory_Debug_Pool
(Has_Unhandled_Memory => True);
if Activate_Monitor and then not Memory_Monitor then
Initialize_System_Memory_Debug_Pool (Has_Unhandled_Memory => True);
Memory_Monitor := True;
end if;

Expand Down
2 changes: 1 addition & 1 deletion src/gnatcoll-mmap.adb
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ package body GNATCOLL.Mmap is

function Is_Mutable (Region : Mapped_Region) return Boolean is
begin
return Region.Mutable or Region.Write;
return Region.Mutable or else Region.Write;
end Is_Mutable;

----------------
Expand Down
Loading