I asked Google's Gemini for parsing code.
2025.04.17 05:03
My Request :
I need parsing code in Object Pascal if message starts with '|' character followed by 2 characters commands, then ':' to seperate command from following message which ends with '>' character
Gemini's answer :
function ParseMessage(const Message: string; out Command: string; out Data: string): Boolean;
var
PipePos: Integer;
ColonPos: Integer;
GreaterThanPos: Integer;
begin
Result := False; // Initialize result to failure
Command := '';
Data := '';
// Find the position of the '|' character
PipePos := Pos('|', Message);
// Check if '|' exists and is at the beginning of the message
if PipePos = 1 then
begin
// Check if the message has at least 5 characters (|CC:D>)
if Length(Message) >= 5 then
begin
// Extract the 2-character command
Command := Copy(Message, 2, 2);
// Find the position of the ':' character after the command
ColonPos := Pos(':', Message);
// Check if ':' exists and is after the command
if ColonPos = 4 then
begin
// Find the position of the '>' character after the ':'
GreaterThanPos := Pos('>', Message);
// Check if '>' exists and is after the ':'
if GreaterThanPos > ColonPos then
begin
// Extract the data between ':' and '>'
Data := Copy(Message, ColonPos + 1, GreaterThanPos - ColonPos - 1);
Result := True; // Parsing successful
end;
end;
end;
end;
end;
// Example Usage:
var
Message1: string;
Command1: string;
Data1: string;
Success1: Boolean;
Message2: string;
Command2: string;
Data2: string;
Success2: Boolean;
begin
Message1 := '|AB:This is some data>';
Success1 := ParseMessage(Message1, Command1, Data1);
if Success1 then
begin
Writeln('Message: ', Message1);
Writeln('Command: ', Command1);
Writeln('Data: ', Data1);
end else
begin
Writeln('Failed to parse message: ', Message1);
end;
Writeln;
Message2 := 'Invalid Message';
Success2 := ParseMessage(Message2, Command2, Data2);
if Success2 then
begin
Writeln('Message: ', Message2);
Writeln('Command: ', Command2);
Writeln('Data: ', Data2);
end else
begin
Writeln('Failed to parse message: ', Message2);
end;
Writeln;
Message2 := '|CD:Another>Example'; // Extra characters after '>'
Success2 := ParseMessage(Message2, Command2, Data2);
if Success2 then
begin
Writeln('Message: ', Message2);
Writeln('Command: ', Command2);
Writeln('Data: ', Data2);
end else
begin
Writeln('Failed to parse message: ', Message2);
end;
Writeln;
Message2 := '|E:Short>'; // Command too short
Success2 := ParseMessage(Message2, Command2, Data2);
if Success2 then
begin
Writeln('Message: ', Message2);
Writeln('Command: ', Command2);
Writeln('Data: ', Data2);
end else
begin
Writeln('Failed to parse message: ', Message2);
end;
Writeln;
Message2 := '|FGData>'; // Missing ':'
Success2 := ParseMessage(Message2, Command2, Data2);
if Success2 then
begin
Writeln('Message: ', Message2);
Writeln('Command: ', Command2);
Writeln('Data: ', Data2);
end else
begin
Writeln('Failed to parse message: ', Message2);
end;
Readln;
end.
=======================================================
Explanation:
-
function ParseMessage(const Message: string; out Command: string; out Data: string): Boolean;
:- This defines a function named
ParseMessage
that takes a constant stringMessage
as input. - It has two
out
parameters:Command
(to store the 2-character command) andData
(to store the message data). - It returns a
Boolean
value indicating whether the parsing was successful (True
) or not (False
).
- This defines a function named
-
var PipePos: Integer; ColonPos: Integer; GreaterThanPos: Integer;
:- Declares integer variables to store the positions of the '|', ':', and '>' characters within the message.
-
Result := False; Command := ''; Data := '';
:- Initializes the
Result
toFalse
(assuming failure initially) and clears theCommand
andData
strings.
- Initializes the
-
PipePos := Pos('|', Message);
:- Uses the
Pos
function to find the first occurrence of the '|' character in theMessage
.
- Uses the
-
if PipePos = 1 then
:- Checks if the '|' character was found and if it's at the beginning of the message (position 1).
-
if Length(Message) >= 5 then
:- Ensures the message has at least 5 characters to potentially contain the required structure (
|CC:D>
).
- Ensures the message has at least 5 characters to potentially contain the required structure (
-
Command := Copy(Message, 2, 2);
:- If the '|' is at the beginning, this line extracts the 2 characters immediately following it (from position 2 with a length of 2) and stores them in the
Command
variable.
- If the '|' is at the beginning, this line extracts the 2 characters immediately following it (from position 2 with a length of 2) and stores them in the
-
ColonPos := Pos(':', Message);
:- Finds the first occurrence of the ':' character in the
Message
.
- Finds the first occurrence of the ':' character in the
-
if ColonPos = 4 then
:- Checks if the ':' character was found and if it's at the expected position (after the '|' and the 2-character command).
-
GreaterThanPos := Pos('>', Message);
:- Finds the first occurrence of the '>' character in the
Message
.
- Finds the first occurrence of the '>' character in the
-
if GreaterThanPos > ColonPos then
:- Checks if the '>' character was found and if it appears after the ':' character.
-
Data := Copy(Message, ColonPos + 1, GreaterThanPos - ColonPos - 1);
:- If all the conditions are met, this line extracts the substring between the ':' (at
ColonPos + 1
) and the '>' (atGreaterThanPos - 1
) and stores it in theData
variable.
- If all the conditions are met, this line extracts the substring between the ':' (at
-
Result := True;
:- Sets the
Result
toTrue
to indicate successful parsing.
- Sets the
-
end;
:- Closes the conditional blocks.
How to Use:
- Copy and paste the
ParseMessage
function into your Object Pascal code (e.g., in theimplementation
section of a unit). - Call the
ParseMessage
function with your message string and two variables to receive the parsedCommand
andData
. - Check the boolean return value of the function to see if the parsing was successful.
The example usage in the begin...end.
block demonstrates how to call the function and interpret its results.
Comment 0
No. | Subject | Author | Date | Views |
---|---|---|---|---|
» | I asked Google's Gemini for parsing code. | me | 2025.04.17 | 82 |
9 |
ESP-NOW without Long Range & External Antenna is terrible!
![]() | me | 2025.04.16 | 81 |
8 |
ESP-NOW with ESP8266 - Two way communication test done
![]() | me | 2025.04.11 | 86 |
7 |
Retry on PWM from Radxa ZERO 3W for servo control
![]() | me | 2025.03.25 | 124 |
6 | Problems found on I2C communication | me | 2025.03.22 | 132 |
5 | radxa zero 3w running an app on booting | me | 2025.03.13 | 142 |
4 |
Basic Communication between ZERO 3W <==> Pro Mini via I2C
![]() | me | 2025.03.13 | 144 |
3 | Important Links | me | 2025.03.12 | 164 |
2 | To-Do List as of Mar. 12, 2025 | me | 2025.03.12 | 159 |
1 |
Mar. 12, 2025 Status
![]() | me | 2025.03.12 | 159 |