### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: [SOLVED] Optimization of function ParseURI  (Read 388 times)

#### lagprogramming

• Sr. Member
• Posts: 390
##### [SOLVED] Optimization of function ParseURI
« on: August 06, 2023, 09:55:48 am »
packages/fcl-base/src/uriparser.pp has function ParseURI(const URI, DefaultProtocol: String; DefaultPort: Word;Decode : Boolean = True):  TURI;
The following patch replaces:
s := Copy(s, 1, i - 1); with SetLength(s, i - 1);
s := Copy(s, 1, i) with SetLength(s, i)
Authority := Copy(Authority, 1, i - 1); with SetLength(Authority, i - 1);
Delete(Authority, i, MaxInt); with SetLength(Authority, i - 1);
if Length(Authority) > 0 then with if Authority <> '' then
In addition, some spaces have been added to Result.Host := Copy(Authority, i+1, MaxInt);
Here is the patch:
Code: Pascal  [Select][+][-]
1. diff --git a/packages/fcl-base/src/uriparser.pp b/packages/fcl-base/src/uriparser.pp
3. --- a/packages/fcl-base/src/uriparser.pp
4. +++ b/packages/fcl-base/src/uriparser.pp
5. @@ -207,7 +207,7 @@ function ParseURI(const URI, DefaultProtocol: String; DefaultPort: Word;Decode :
6.      Result.Bookmark := Copy(s, i + 1, MaxInt);
7.      if Decode then
8.        Result.Bookmark:=Unescape(Result.Bookmark);
9. -    s := Copy(s, 1, i - 1);
10. +    SetLength(s, i - 1);
11.    end;
12.
13.    // Extract the params
14. @@ -218,7 +218,7 @@ function ParseURI(const URI, DefaultProtocol: String; DefaultPort: Word;Decode :
15.      Result.Params := Copy(s, i + 1, MaxInt);
16.      if Decode then
17.        Result.Params:=Unescape(Result.Params);
18. -    s := Copy(s, 1, i - 1);
19. +    SetLength(s, i - 1);
20.    end;
21.
22.    // extract authority
23. @@ -248,7 +248,7 @@ function ParseURI(const URI, DefaultProtocol: String; DefaultPort: Word;Decode :
24.        if Decode then
25.          Result.Document:=Unescape(Result.Document);
26.        if (Result.Document <> '.') and (Result.Document <> '..') then
27. -        s := Copy(s, 1, i)
28. +        SetLength(s, i)
29.        else
30.          Result.Document := '';
31.        break;
32. @@ -287,7 +287,7 @@ function ParseURI(const URI, DefaultProtocol: String; DefaultPort: Word;Decode :
33.      if PortValid then
34.      begin
35.        Result.Port := StrToInt(Copy(Authority, i + 1, MaxInt));
36. -      Authority := Copy(Authority, 1, i - 1);
37. +      SetLength(Authority, i - 1);
38.      end;
39.    end;
40.
41. @@ -296,11 +296,11 @@ function ParseURI(const URI, DefaultProtocol: String; DefaultPort: Word;Decode :
42.    i := Pos('@', Authority);
43.    if i > 0 then
44.    begin
45. -    Result.Host := Copy(Authority, i+1, MaxInt);
46. -    Delete(Authority, i, MaxInt);
47. +    Result.Host := Copy(Authority, i + 1, MaxInt);
48. +    SetLength(Authority, i - 1);
49.
51. -    if Length(Authority) > 0 then
52. +    if Authority <> '' then
53.      begin
54.        i := Pos(':', Authority);
55.        if i = 0 then
« Last Edit: August 06, 2023, 12:42:03 pm by lagprogramming »

#### AlexTP

• Hero Member
• Posts: 2292
##### Re: Optimization of function ParseURI
« Reply #1 on: August 06, 2023, 10:35:39 am »