Skip to content

Commit 3ecfd43

Browse files
committed
Optimize parser
Clean up parser code and optimize a bit more
1 parent 150f123 commit 3ecfd43

File tree

1 file changed

+14
-32
lines changed

1 file changed

+14
-32
lines changed

entries/rlawson/src/parser.pas

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,20 @@ implementation
3636
procedure ProcessMeasurements(var buffer: array of char; bufferLength: integer;
3737
var results: TFPHashList);
3838
var
39-
currentChar: char;
4039
idx: integer = 0;
4140
currentTempSign: integer = 1;
4241
temp, cityStart: integer;
4342
city: shortstring;
4443
reading: PTCityTemp;
4544
begin
46-
//Writeln('bufferLength: ', bufferLength);
4745
while idx < (bufferLength - 1) do
4846
begin
49-
{slurp up the city}
50-
city := '';
47+
// read the city until separator
48+
//city := '';
5149
cityStart := idx;
52-
currentChar := buffer[idx];
53-
while currentChar <> REC_SEP do
54-
begin
55-
Inc(idx);
56-
currentChar := buffer[idx];
57-
end;
50+
while buffer[idx] <> REC_SEP do Inc(idx);
5851
SetString(city, @buffer[cityStart], (idx - cityStart));
59-
{slurp up the temp reading}
52+
// increment through temp reading and calculate integer temp (*100)
6053
Inc(idx); // move pointer past the ;
6154
currentTempSign := 1;
6255
// check for negative sign, if so flag the multiplier and then move past neg sign
@@ -66,30 +59,20 @@ procedure ProcessMeasurements(var buffer: array of char; bufferLength: integer;
6659
Inc(idx);
6760
end;
6861
// look ahead - is decimal point 2 spaces away then we have two digits
69-
temp := 0;
7062
if buffer[idx + 2] = DECIMAL_POINT then
7163
begin
72-
temp := 100 * (byte(buffer[idx]) - ANSI_ZERO);
73-
Inc(idx);
74-
end;
75-
temp := temp + 10 * (byte(buffer[idx]) - ANSI_ZERO);
76-
idx := idx + 2;
77-
temp := currentTempSign * (temp + (byte(buffer[idx]) - ANSI_ZERO));
78-
if temp > 999 then
79-
begin
80-
WriteLn('Somethign wrong!', city, ' | ', temp, ' | ', buffer[idx - 3],
81-
' | ', buffer[idx - 2],
82-
' | ', buffer[idx - 1], ' | ', buffer[idx]);
83-
break;
84-
end;
85-
86-
currentChar := buffer[idx];
87-
while currentChar <> LF do
64+
temp := currentTempSign * (100 * byte(buffer[idx]) + 10 *
65+
byte(buffer[idx + 1]) + byte(buffer[idx + 2]) - 5328);
66+
// move past digits and CRLF and position pointer to first character of next record
67+
idx := idx + 6;
68+
end
69+
else
8870
begin
89-
Inc(idx);
90-
currentChar := buffer[idx];
71+
temp := currentTempSign * (10 * byte(buffer[idx + 1]) +
72+
byte(buffer[idx + 2]) - 528);
73+
// move past digits and CRLF and position pointer to first character of next record
74+
idx := idx + 5;
9175
end;
92-
Inc(idx);
9376
reading := results.Find(city);
9477
if reading = nil then
9578
begin
@@ -109,7 +92,6 @@ procedure ProcessMeasurements(var buffer: array of char; bufferLength: integer;
10992
reading^.numReadings := reading^.numReadings + 1;
11093
end;
11194
end;
112-
//WriteLn('results: ', results.Count);
11395
end;
11496

11597
function PascalRound(x: double): double;

0 commit comments

Comments
 (0)