Url Parsing in Pascal
Here’s the translation of the Go URL parsing example to Pascal, formatted in Markdown suitable for Hugo:
program URLParsing;
uses
SysUtils, Classes, StrUtils;
type
TURL = record
Scheme: string;
User: string;
Password: string;
Host: string;
Port: string;
Path: string;
Query: string;
Fragment: string;
end;
function ParseURL(const URL: string): TURL;
var
SchemeDelimiter, UserInfoDelimiter, HostPortDelimiter, PathDelimiter,
QueryDelimiter, FragmentDelimiter: Integer;
begin
// Initialize result
Result := Default(TURL);
// Find scheme
SchemeDelimiter := Pos('://', URL);
if SchemeDelimiter > 0 then
begin
Result.Scheme := Copy(URL, 1, SchemeDelimiter - 1);
SchemeDelimiter := SchemeDelimiter + 3;
end
else
SchemeDelimiter := 1;
// Find user info
UserInfoDelimiter := PosEx('@', URL, SchemeDelimiter);
if UserInfoDelimiter > 0 then
begin
Result.User := Copy(URL, SchemeDelimiter, UserInfoDelimiter - SchemeDelimiter);
HostPortDelimiter := UserInfoDelimiter + 1;
// Split user and password
UserInfoDelimiter := Pos(':', Result.User);
if UserInfoDelimiter > 0 then
begin
Result.Password := Copy(Result.User, UserInfoDelimiter + 1, Length(Result.User));
Result.User := Copy(Result.User, 1, UserInfoDelimiter - 1);
end;
end
else
HostPortDelimiter := SchemeDelimiter;
// Find path
PathDelimiter := PosEx('/', URL, HostPortDelimiter);
if PathDelimiter = 0 then
PathDelimiter := Length(URL) + 1;
// Extract host and port
Result.Host := Copy(URL, HostPortDelimiter, PathDelimiter - HostPortDelimiter);
HostPortDelimiter := Pos(':', Result.Host);
if HostPortDelimiter > 0 then
begin
Result.Port := Copy(Result.Host, HostPortDelimiter + 1, Length(Result.Host));
Result.Host := Copy(Result.Host, 1, HostPortDelimiter - 1);
end;
// Find query
QueryDelimiter := PosEx('?', URL, PathDelimiter);
if QueryDelimiter = 0 then
QueryDelimiter := Length(URL) + 1;
// Extract path
Result.Path := Copy(URL, PathDelimiter, QueryDelimiter - PathDelimiter);
// Find fragment
FragmentDelimiter := PosEx('#', URL, QueryDelimiter);
if FragmentDelimiter = 0 then
FragmentDelimiter := Length(URL) + 1;
// Extract query
Result.Query := Copy(URL, QueryDelimiter + 1, FragmentDelimiter - QueryDelimiter - 1);
// Extract fragment
Result.Fragment := Copy(URL, FragmentDelimiter + 1, Length(URL));
end;
var
URL: string;
ParsedURL: TURL;
begin
// We'll parse this example URL, which includes a
// scheme, authentication info, host, port, path,
// query params, and query fragment.
URL := 'postgres://user:pass@host.com:5432/path?k=v#f';
// Parse the URL
ParsedURL := ParseURL(URL);
// Output the parsed components
WriteLn('Scheme: ', ParsedURL.Scheme);
WriteLn('User: ', ParsedURL.User);
WriteLn('Password: ', ParsedURL.Password);
WriteLn('Host: ', ParsedURL.Host);
WriteLn('Port: ', ParsedURL.Port);
WriteLn('Path: ', ParsedURL.Path);
WriteLn('Query: ', ParsedURL.Query);
WriteLn('Fragment: ', ParsedURL.Fragment);
end.This Pascal program demonstrates URL parsing. Here’s a breakdown of what it does:
We define a
TURLrecord to hold the different components of a URL.The
ParseURLfunction takes a URL string and breaks it down into its components, returning aTURLrecord.In the main program, we use an example URL that includes various components: scheme, authentication info, host, port, path, query params, and fragment.
We call
ParseURLto parse our example URL.Finally, we print out each component of the parsed URL.
To run this program, you would typically save it as a .pas file (e.g., urlparsing.pas) and compile it with a Pascal compiler like Free Pascal:
$ fpc urlparsing.pas
$ ./urlparsingThe output would show each component of the parsed URL:
Scheme: postgres
User: user
Password: pass
Host: host.com
Port: 5432
Path: /path
Query: k=v
Fragment: fThis example demonstrates how to handle URL parsing in Pascal. While Pascal doesn’t have built-in URL parsing libraries like some modern languages, we can implement the functionality ourselves using string manipulation techniques.