Recent

Author Topic: Length of String is not fixed inside For?  (Read 3079 times)

Zvoni

  • Hero Member
  • *****
  • Posts: 2914
Re: Length of String is not fixed inside For?
« Reply #15 on: February 10, 2025, 10:05:00 am »
Hi
Hmmm, if not stringreplace, then maybe like this:
*snip*
I dunno, but like this it makes some sense to me ...at least.
Regards Benny
Absolutly no reason for something convoluted like this

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. Var s,r:String;
  4.  
  5. function PhpAddSlashes(const aStr: string): string;
  6. Var i:Integer;
  7. Begin
  8.   Result:=aStr;
  9.   For i:=Length(aStr) DownTo 1 Do
  10.     If aStr[i]='\' Then System.Insert('\',Result,i);
  11. End;
  12.  
  13. begin
  14.   s:='\I \always\ arrive late at the office, but I make up for it by leaving \early\';
  15.   r:=PhpAddSlashes(s);
  16.   Writeln(r);
  17.   Readln;
  18. end.
Returns
Quote
\\I \\always\\ arrive late at the office, but I make up for it by leaving \\early\\

EDIT:
From the Wikipedia-Link
Quote
Although it had a wide user base in the late 1990s, VP has not evolved significantly since 2001, and after a few maintenance-only releases, the owner declared that development had ceased in 2005.[1]

On 4 Apr 2005, Virtual Pascal was announced 'dead' on the official site. The last released version (2.1 Build 279) was announced on 13 May 2004.[1]

beating a dead horse?
« Last Edit: February 10, 2025, 10:08:44 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

TRon

  • Hero Member
  • *****
  • Posts: 4157
Re: Length of String is not fixed inside For?
« Reply #16 on: February 10, 2025, 10:12:15 am »
beating a dead horse?
imho, yes. But at the same time some people do seem to have a hard time parting from it. And, mind I am only guessing that OP is using VP based on the comments in the code. Who had the tag-line again that reads: "assumption is the mother of all f-ups" ?  :)
Today is tomorrow's yesterday.

cdbc

  • Hero Member
  • *****
  • Posts: 1966
    • http://www.cdbc.dk
Re: Length of String is not fixed inside For?
« Reply #17 on: February 10, 2025, 10:18:40 am »
Hi
@Zvoni: Sorry mate, but I don't like these mem-reallocations:
Code: Pascal  [Select][+][-]
  1. If aStr[i]='\' Then System.Insert('\',Result,i);
I find it speedier with 1 allocation, 1 loop & 1 adjustment, than 10 realloc's (per your example)
Regards Benny

edit: Oh ...and you can make it speedier, by using pchars inside the function.
« Last Edit: February 10, 2025, 10:26:40 am by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

MarkMLl

  • Hero Member
  • *****
  • Posts: 8334
Re: Length of String is not fixed inside For?
« Reply #18 on: February 10, 2025, 10:20:09 am »
imho, yes. But at the same time some people do seem to have a hard time parting from it.

Regretably, the same could be said for every member of the FPC community.

However, the bottom line is that OP said what he was using and asked a question based on that, and the appropriate course of action is to try to answer the question rather than to criticise his judgement.

Who knows, he might have been considering moving over to FPC.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Zvoni

  • Hero Member
  • *****
  • Posts: 2914
Re: Length of String is not fixed inside For?
« Reply #19 on: February 10, 2025, 10:23:03 am »
Hi
@Zvoni: Sorry mate, but I don't like these mem-reallocations:
Code: Pascal  [Select][+][-]
  1. If aStr[i]='\' Then System.Insert('\',Result,i);
I find it speedier with 1 allocation, 1 loop & 1 adjustment, than 10 realloc's (per your example)
Regards Benny

Accepted.
Though look at your code, and look at mine......
Nevermind: Do you really think you're going to notice a difference in performance on todays hardware?
Well, except if you send a gargantuan string to the function.....

imho, yes. But at the same time some people do seem to have a hard time parting from it.

Regretably, the same could be said for every member of the FPC community.

However, the bottom line is that OP said what he was using and asked a question based on that, and the appropriate course of action is to try to answer the question rather than to criticise his judgement.

Who knows, he might have been considering moving over to FPC.

MarkMLl


Mark, accepted.
« Last Edit: February 10, 2025, 10:25:01 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

cdbc

  • Hero Member
  • *****
  • Posts: 1966
    • http://www.cdbc.dk
Re: Length of String is not fixed inside For?
« Reply #20 on: February 10, 2025, 10:32:04 am »
Hi
@Zvoni:
Quote
Do you really think you're going to notice a difference in performance on todays hardware?
Well, I have, on today's hardware, met algorithms so slow, that I had to look twice, only to find "Idiotic very ineffective string-handling".
It can really surprise you...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

TRon

  • Hero Member
  • *****
  • Posts: 4157
Re: Length of String is not fixed inside For?
« Reply #21 on: February 10, 2025, 10:39:52 am »
Regretably, the same could be said for every member of the FPC community.

However, the bottom line is that OP said what he was using and asked a question based on that, and the appropriate course of action is to try to answer the question rather than to criticise his judgement.

Who knows, he might have been considering moving over to FPC.
Fair enough.

But, for the record, you believe I was judgemental ? With regards to what exactly ? I mean, it is probably me but I do not see how one could pinpoint an issue without doing critical analysis.
Today is tomorrow's yesterday.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10920
  • Debugger - SynEdit - and more
    • wiki
Re: Length of String is not fixed inside For?
« Reply #22 on: February 10, 2025, 10:43:13 am »
Hi
@Zvoni:
Quote
Do you really think you're going to notice a difference in performance on todays hardware?
Well, I have, on today's hardware, met algorithms so slow, that I had to look twice, only to find "Idiotic very ineffective string-handling".
It can really surprise you...
Regards Benny

Off-Topic.

True indeed. With FPC it usually takes longer strings before it gets noticeable => though the "with fpc" should actually read "with fpc and its default mem manager" => because it usually allocates at least 16 bytes (and afaik bigger blocks to a 16 byte bound). So resizing a string within those bounds for as many times as you like, makes a less noticeable impact. But growing over such a bound, may (as in "has the potential to") give a real slowdown.

Add to that:
- test runs with heaptrc => very noticable
- comparisons on OS that require conversion to widestring (for case insensitive)

But nothing to do with the loop issue.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8334
Re: Length of String is not fixed inside For?
« Reply #23 on: February 10, 2025, 11:00:10 am »
But, for the record, you believe I was judgemental ? With regards to what exactly ? I mean, it is probably me but I do not see how one could pinpoint an issue without doing critical analysis.

No, definitely not and in particular it wasn't you who came up with "WTF's Virtual Pascal?" or something similar.

Quote
There’s an old joke about a city boy, who comes out to the country for his cousin’s wedding.

He can’t remember the way, so he stops to ask a farmer for directions. The farmer looks at him, scratches his head, thinks for a moment, frowns and says:

‘Well, if I were you, I wouldn’t start from here.’

However I think I have to make the point that just about /everybody/ here has dived into the /content/ of his for loop, rather than focusing on that specific statement, and ripped it to shreds in a way that OP might find offputting.

And I'm afraid that I'm particularly critical of responses which start talking about something which is at worst a misunderstanding of how Pascal flow control works in terms of memory reallocation. Now that's definitely something which is worth knowing, but only a very long way down the line.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Zvoni

  • Hero Member
  • *****
  • Posts: 2914
Re: Length of String is not fixed inside For?
« Reply #24 on: February 10, 2025, 11:11:04 am »
Hi
@Zvoni: Sorry mate, but I don't like these mem-reallocations:
Code: Pascal  [Select][+][-]
  1. If aStr[i]='\' Then System.Insert('\',Result,i);
I find it speedier with 1 allocation, 1 loop & 1 adjustment, than 10 realloc's (per your example)
Regards Benny

edit: Oh ...and you can make it speedier, by using pchars inside the function.
Fair enough
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. Var s,r:String;
  4.  
  5. Function PhpAddSlashes(Const aStr:String):String;
  6. Var
  7.   i:Integer;
  8.   x:Integer;
  9.   c:Integer;
  10. Begin
  11.   c:=0;
  12.   x:=Length(aStr);
  13.   //Count slahses
  14.   For i:=1 To x Do If aStr[i]='\' Then Inc(c);
  15.   SetLength(Result,x+c);  //Resize Result
  16.   i:=1;
  17.   x:=1;
  18.   Repeat
  19.     Result[x]:=aStr[i];
  20.     If Result[x]='\' Then
  21.       Begin
  22.         Result[x+1]:='\';
  23.         Inc(x);
  24.       end;
  25.     Inc(i);
  26.     Inc(x);
  27.   until x>Length(Result);
  28. End;
  29.  
  30. begin
  31.   s:='\I \always\ arrive late at the office, but I make up for it by leaving \early\';
  32.   r:=PhpAddSlashes(s);
  33.   Writeln(r);
  34.   Readln;
  35. end.

Note: I'm aware that i'm not defending against an out of bound access in the source-string
« Last Edit: February 10, 2025, 11:31:31 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

TRon

  • Hero Member
  • *****
  • Posts: 4157
Re: Length of String is not fixed inside For?
« Reply #25 on: February 10, 2025, 11:22:14 am »
No, definitely not and in particular it wasn't you ...
Ok, a misunderstanding from my part. Apologies.

Quote
However I think I have to make the point that just about /everybody/ here has dived into the /content/ of his for loop, rather than focusing on that specific statement, and ripped it to shreds in a way that OP might find offputting.

And I'm afraid that I'm particularly critical of responses which start talking about something which is at worst a misunderstanding of how Pascal flow control works in terms of memory reallocation. Now that's definitely something which is worth knowing, but only a very long way down the line.
That is more than fair because I did comment on that in particular in my communication with Zvoni.

In case d2010 is offended by any of my remarks then my apologies.


Do note that (especially in case of beginners corner) I usually try to stay as close to original code as possible and use (or at least try to use) the same thinking strategy in order to solve/approach the issue, unless the original code really begs for a different approach. In this particular case there was and is not a compelling reason to divert (much) from the original code. Again, minimal changes that would depend on what OP actually meant with his remarks.
Today is tomorrow's yesterday.

Warfley

  • Hero Member
  • *****
  • Posts: 1870
Re: Length of String is not fixed inside For?
« Reply #26 on: February 10, 2025, 11:23:55 am »
First, from the documentation: https://www.freepascal.org/docs-html/current/ref/refsu58.html#x168-19200013.2.4
Quote
Free Pascal always calculates the upper bound exactly once before initializing the counter variable with the initial value.
So you cannot have a changing upper bound.

Second, the for loop is quite a complex construct in Pascal, unlike for example in C where it is just an extension of the while loop, in Pascal the for loop does much more:
Code: Pascal  [Select][+][-]
  1.  for i:=min to max do
  2.     statement;
  3.  
  4. // Is equivalent to:
  5.  
  6.   i:=min;
  7.   tmpmax:=max;
  8.   if i<=tmpmax then
  9.     repeat
  10.       statement;
  11.       if i>=tmpmax then
  12.         break
  13.       else
  14.         inc(i);
  15.     until false;
  16.  

cdbc

  • Hero Member
  • *****
  • Posts: 1966
    • http://www.cdbc.dk
Re: Length of String is not fixed inside For?
« Reply #27 on: February 10, 2025, 11:42:49 am »
Hi
@Zvoni: Nicely done mate =^
Regards Benny

edit: I use =^ as a 'thumb's up' instead of a smiley thumb's up
« Last Edit: February 10, 2025, 11:46:08 am by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

dbannon

  • Hero Member
  • *****
  • Posts: 3298
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Length of String is not fixed inside For?
« Reply #28 on: February 10, 2025, 12:30:53 pm »
Yes, I agree, that's a nice piece of work Zvoni, I am already thinking where I can use that particular trick.

But what about the reverse ? Imagine a text 'document' that contains lots of LF,CR characters in Windows style, and converting that to Unix single char model. Does FPC adjust the memory allocation for every delete() ?   I do something like that and am not aware that its particularly slow.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

cdbc

  • Hero Member
  • *****
  • Posts: 1966
    • http://www.cdbc.dk
Re: Length of String is not fixed inside For?
« Reply #29 on: February 10, 2025, 12:38:33 pm »
Hi
@dbannon: Davo, take a look at how TStrings / TStringList does it...
IIRC it reads it agnostically and depending on the platform, writes the necessary lineendings internally...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

 

TinyPortal © 2005-2018