Skip to content

Improvement of function EncodeURI

by 'lagprogramming'.

packages/fcl-base/src/uriparser.pp has

    function EncodeURI(const URI: TURI): String;
    // ! if no scheme then first colon in path should be escaped
    begin
      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 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".

Patch: uri.diff

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information