Forum > FPC development
[SOLVED] Improvement of function EncodeURI
lagprogramming:
packages/fcl-base/src/uriparser.pp has
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function EncodeURI(const URI: TURI): String;// ! if no scheme then first colon in path should be escapedbegin SetLength(Result, 0); if Length(URI.Protocol) > 0 then Result := LowerCase(URI.Protocol) + ':'; if URI.HasAuthority then begin Result := Result + '//'; if Length(URI.Username) > 0 then begin Result := Result + URI.Username; if Length(URI.Password) > 0 then Result := Result + ':' + URI.Password; Result := Result + '@'; end; Result := Result + URI.Host; end; if URI.Port <> 0 then Result := Result + ':' + IntToStr(URI.Port); Result := Result + Escape(URI.Path, ValidPathChars); if Length(URI.Document) > 0 then begin if (Length(URI.Path) > 0) and ((Length(Result) = 0) or (Result[Length(Result)] <> '/')) then Result := Result + '/'; Result := Result + Escape(URI.Document, ValidPathChars); end; if Length(URI.Params) > 0 then Result := Result + '?' + Escape(URI.Params, ValidPathChars); if Length(URI.Bookmark) > 0 then Result := Result + '#' + Escape(URI.Bookmark, ValidPathChars);end;The initial "SetLength(Result, 0);" line has been replaced with "Result := '';".
All comparisons of Length(string) with zero have been replaced by comparisons of string with ''.
The function becomes:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function EncodeURI(const URI: TURI): String;// ! if no scheme then first colon in path should be escapedbegin Result := ''; if URI.Protocol <> '' then Result := LowerCase(URI.Protocol) + ':'; if URI.HasAuthority then begin Result := Result + '//'; if URI.Username <> '' then begin Result := Result + URI.Username; if URI.Password <> '' then Result := Result + ':' + URI.Password; Result := Result + '@'; end; Result := Result + URI.Host; end; if URI.Port <> 0 then Result := Result + ':' + IntToStr(URI.Port); Result := Result + Escape(URI.Path, ValidPathChars); if URI.Document <> '' then begin if (URI.Path <> '') and ((Result = '') or (Result[Length(Result)] <> '/')) then Result := Result + '/'; Result := Result + Escape(URI.Document, ValidPathChars); end; if URI.Params <> '' then Result := Result + '?' + Escape(URI.Params, ValidPathChars); if URI.Bookmark <> '' then Result := Result + '#' + Escape(URI.Bookmark, ValidPathChars);end;The only comparison of a length of a string with zero in the entire file remained inside function ParseURI, so that line has been modified from "if Length(Authority)>0 then" to "if Authority <> '' then".
A patch has been attached.
Fibonacci:
Out of curiosity I checked who and when would initialize the result to an empty string using SetLength(), it turns out that this file was added 20 years ago on 17.05.2003, until now this line was unchanged. The author is still an active maintainer with last commit just 5 days ago.
Hello Michaƫl!
runewalsh:
I tried to make a cool MR that should supposedly do the Length > 0 thing automatically, so maybe wait for one to ten years before rushing into ''s.
AlexTP:
Posted to https://gitlab.com/freepascal.org/fpc/source/-/issues/40455
AlexTP:
It was applied.
New idea for this func optimization.
Don't concat small strings. But collect parts in the TStringArray with predefined Length (about 10). When all done, concat all StringArray parts via one mem realloc (like TStringList.Text does).
Navigation
[0] Message Index
[#] Next page