Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ scriptLib.pas - macrosLog, | Suggestions for Improvements and Optimization #39

Open
filhotecmail opened this issue Mar 11, 2023 · 1 comment

Comments

@filhotecmail
Copy link

The condition "if sizeOfFile(MACROS_LOG_FILE) = 0 then" is checking if the log file is empty before appending the new record. This can lead to an additional read of the file to check the size, which can be inefficient for large files. An alternative would be to keep a separate counter or use a global variable to check if this is the first time a record is being added and then add the log header if so.

const
  HtmlEncodeChars: array[0..4] of record
    Char: Char;
    Code: string;
  end = (
    (Char: '<'; Code: '&lt;'),
    (Char: '>'; Code: '&gt;'),
    (Char: '&'; Code: '&amp;'),
    (Char: '"'; Code: '&quot;'),
    (Char: ''''; Code: '&apos;')
  );

function htmlEncode(const s: string): string;
var
  i, j, len: integer;
begin
  len := Length(s);
  SetLength(Result, len * 6); // máximo tamanho possível após a conversão
  j := 1;

  for i := 1 to len do
  begin
    if s[i] < ' ' then // caracteres de controle
    begin
      Result[j] := '?';
      Inc(j);
    end
    else
    begin
      case s[i] of
        '<', '>', '&', '"', '''':
          begin
            Move(PChar(HtmlEncodeChars[s[i] = '<']).^, PChar(@Result[j])^, Length(HtmlEncodeChars[s[i] = '<']) * SizeOf(HtmlEncodeChars[0]));
            Inc(j, Length(HtmlEncodeChars[s[i] = '<']));
          end;
      else
        Result[j] := s[i];
        Inc(j);
      end;
    end;
  end;

  SetLength(Result, j - 1);
end;

Create a constant called HtmlEncodeChars that contains a list of HTML characters and their HTML encoded equivalents.

I've defined an htmlEncode function that iterates through the characters in the input string, checks whether each character needs to be HTML-encoded, and then adds the encoded character to the result. This is done using the Move function to copy the corresponding HTML code into the result.

If the macrosLog method is called frequently over a short period of time, it can be useful to group multiple log entries into a single write to disk.

This can be done by adding a global variable that stores the last write time to disk and then checking that a sufficient amount of time has passed since the last disk write before writing the new log entry.

@rejetto
Copy link
Owner

rejetto commented Mar 12, 2023

hey Carlos,
I forgot to write on the first page that this project is not actively developed anymore.
I started a completely new project an another repository.
https://github.com/rejetto/hfs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants