Recent

Author Topic: [SOLVED] Improvement of function SetCompileModeSwitch  (Read 856 times)

lagprogramming

  • Sr. Member
  • ****
  • Posts: 407
[SOLVED] Improvement of function SetCompileModeSwitch
« on: November 19, 2023, 08:33:46 am »
compiler/scanner.pas has function SetCompileModeSwitch(s:string; changeInit: boolean):boolean;
Within the code of the function "s:=copy(s,1,length(s)-1);" can be encountered twice. The attached patch replaces it with "setlength(s,length(s)-1);".
« Last Edit: November 20, 2023, 10:13:02 am by lagprogramming »

AlexTP

  • Hero Member
  • *****
  • Posts: 2519
    • UVviewsoft

vangli

  • New Member
  • *
  • Posts: 47
Re: Improvement of function SetCompileModeSwitch
« Reply #2 on: November 19, 2023, 09:12:35 am »
Hi. Just of curriosity. Isn't it?

Code: Pascal  [Select][+][-]
  1. s:=copy(s,1,length(s)-1);

will guaranty that "s" is minimum an empty string if it happens that "s" is single byte or already an empty string.

Code: Pascal  [Select][+][-]
  1. setlength(s,length(s)-1);

will release memory if "s" is one byte long or already an empty string. Then the rest of the function SetCompileModeSwitch will crash?
Regards Bent

TRon

  • Hero Member
  • *****
  • Posts: 3776
Re: Improvement of function SetCompileModeSwitch
« Reply #3 on: November 19, 2023, 09:34:57 am »
will release memory if "s" is one byte long or already an empty string. Then the rest of the function SetCompileModeSwitch will crash?
Nah, not really  :)

Code: Pascal  [Select][+][-]
  1. case s[length(s)] of
  2.  
I do not have to remember anything anymore thanks to total-recall.

vangli

  • New Member
  • *
  • Posts: 47
Re: Improvement of function SetCompileModeSwitch
« Reply #4 on: November 19, 2023, 09:53:58 am »
Thks TRon. I may have been (mis)leaded by https://www.freepascal.org/docs-html/rtl/system/setlength.html where it states the following:

"Note that SetLength is governed by the Copy-On-Write principle for strings and dynamic arrays: the reference count after a call to SetLength will be 1 (except when the length is zero, then the array is removed from memory). "

I read the parenthesis to include both strings and arrays, but it may only be true for array length.
Regards Bent

 

TinyPortal © 2005-2018