Skip to content

Commit b4cc3f7

Browse files
committed
Use phrase/2 to improve readability of code #1
1 parent 0afdfeb commit b4cc3f7

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

messages.pl

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
:- use_module(library(lists)).
1717
:- use_module(library(charsio)).
18+
:- use_module(library(dcgs)).
1819

1920
:- use_module('types').
2021

@@ -23,31 +24,27 @@
2324

2425
% StartupMessage
2526
startup_message(User, Database, Bytes) :-
26-
int32(196608, B1),
27+
int32(196608, B1), % Version
2728
pstring("user", B2),
2829
pstring(User, B3),
2930
pstring("database", B4),
3031
pstring(Database, B5),
31-
append(B1, B2, B12),
32-
append(B3, B4, B34),
33-
append(B12, B34, B1234),
34-
append(B1234, B5, B12345),
35-
append(B12345, [0], B),
36-
length(B, L),
32+
phrase((B1, B2, B3, B4, B5, [0]), Bs),
33+
length(Bs, L),
3734
BytesLength is L + 4,
3835
int32(BytesLength, B0),
39-
append(B0, B, Bytes).
36+
append(B0, Bs, Bytes).
4037

4138
% AuthenticationMD5Password
4239
auth_message(md5, Salt, Bytes) :-
43-
Bytes = [82,_,_,_,_|Bytes0],
40+
Bytes = [82,_,_,_,_|Bytes0], % Byte R
4441
Bytes0 = [B7, B6, B5, B4, B3, B2, B1, B0],
4542
int32(5, [B7, B6, B5, B4]),
4643
Salt = [B3, B2, B1, B0].
4744

4845
% AuthenticationCleartextPassword
4946
auth_message(password, Bytes) :-
50-
Bytes = [82,_,_,_,_|Bytes0],
47+
Bytes = [82,_,_,_,_|Bytes0], % Byte R
5148
Bytes0 = [B3, B2, B1, B0],
5249
int32(3, [B3, B2, B1, B0]).
5350

@@ -61,7 +58,7 @@
6158

6259
% AuthenticationOk
6360
auth_ok_message(Bytes) :-
64-
Bytes = [82,0,0,0,8,0,0,0,0].
61+
Bytes = [82,0,0,0,8,0,0,0,0]. % Byte R
6562

6663
% Query
6764
query_message(Query, Bytes) :-
@@ -73,7 +70,7 @@
7370

7471
% ErrorResponse
7572
error_message(Error, Bytes) :-
76-
Bytes = [69,_,_,_,_,B0|Bytes0],
73+
Bytes = [69,_,_,_,_,B0|Bytes0], % Byte E
7774
(B0 = 0 ->
7875
Error = "No error message"
7976
; pstring(Error, Bytes0)
@@ -89,7 +86,7 @@
8986

9087
% CommandComplete
9188
command_complete_message(Bytes) :-
92-
Bytes = [67|_].
89+
Bytes = [67|_]. % Byte C
9390

9491
% RowDescription
9592
row_description_message(Columns, Bytes) :-
@@ -119,15 +116,15 @@
119116
get_fields([Column|Columns], Fields, Bytes) :-
120117
Bytes = [B3, B2, B1, B0|Bytes0],
121118
int32(Length, [B3, B2, B1, B0]),
122-
Length = 4294967295,
119+
Length = 4294967295, % -1
123120
Column = null,
124121
Fields0 is Fields - 1,
125122
get_fields(Columns, Fields0, Bytes0).
126123

127124
get_fields([Column|Columns], Fields, Bytes) :-
128125
Bytes = [B3, B2, B1, B0|Bytes0],
129126
int32(Length, [B3, B2, B1, B0]),
130-
Length \= 4294967295,
127+
Length \= 4294967295, % -1
131128
take(Length, Bytes0, ColumnBytes, NewBytes),
132129
chars_utf8bytes(Column, ColumnBytes),
133130
Fields0 is Fields - 1,
@@ -139,4 +136,4 @@
139136

140137
% ReadyForQuery
141138
ready_for_query_message(Bytes) :-
142-
Bytes = [90|_].
139+
Bytes = [90|_]. % Byte Z

0 commit comments

Comments
 (0)