Recent

Author Topic: What is Missing in FreePascal?  (Read 3291 times)

derek.john.evans

  • Guest
What is Missing in FreePascal?
« on: October 04, 2015, 09:56:20 am »
First up, I love Pascal. But, after coding each project, I end up with these little functions, which are slight variations on what is currently available.

Here are 4 examples:

Code: Pascal  [Select][+][-]
  1. function PosExIC(const ASubStr, AString: String; const AOffset: Integer): Integer;
  2. var
  3.   LPos: PChar;
  4. begin
  5.   if (AOffset < 1) or (AOffset > Length(AString)) then begin
  6.     Result := 0;
  7.   end else begin
  8.     LPos := stripos(pchar(AString) + AOffset - 1, pchar(ASubStr));
  9.     if LPos = nil then begin
  10.       Result := 0;
  11.     end else begin
  12.       Result := LPos - pchar(AString) + 1;
  13.     end;
  14.   end;
  15. end;
  16.  

Code: Pascal  [Select][+][-]
  1. function PosIC(const ASubStr, AString: String): Integer;
  2. begin
  3.   Result := PosExIC(ASubStr, AString, 1);
  4. end;
  5.  

Code: Pascal  [Select][+][-]
  1. function RPosSet(const ACharSet: TSysCharSet; const AString: String): Integer;
  2. begin
  3.   for Result := Length(AString) downto 1 do begin
  4.     if AString[Result] in ACharSet then begin
  5.       Exit;
  6.     end;
  7.   end;
  8.   Result := 0;
  9. end;
  10.  

Code: Pascal  [Select][+][-]
  1. function RPosSetEx(const ACharSet: TSysCharSet; const AString: String; const AOffset: Integer): Integer;
  2. begin
  3.   for Result := Min(AOffset, Length(AString)) downto 1 do begin
  4.     if AString[Result] in ACharSet then begin
  5.       Exit;
  6.     end;
  7.   end;
  8.   Result := 0;
  9. end;
  10.  

PosExIC and PosIC are two functions which I don't understand are not in FreePascal. Or, where is the floating point modulus? It would be nice if some basic functions were added without waiting for a Embarcadero initiative.

Maybe, I'm missing some understanding of FreePascal development.

I guess the issue I see is, someone asked for the best way to change the innerhtml of a tag. The answers used regular expressions, which I thought was somewhat of a overkill, considering the simple requirement (and how it would be coded in other languages).

But, if there was a PosIC and PosExIC, then this could have been the solution:
Code: Pascal  [Select][+][-]
  1. function ChangeInnerHtml(const AHtml, ATag, AInnerHtml: String): String;
  2. var
  3.   LStart, LEnd: Integer;
  4. begin
  5.   Result := AHtml;
  6.   LStart := PosIC('<' + ATag + '>', Result);
  7.   if LStart > 0 then begin
  8.     LStart += Length(ATag) + 2;
  9.     LEnd := PosExIC('</' + ATag + '>', Result, LStart);
  10.     if LEnd > 0 then begin
  11.       Result := StuffString(Result, LStart, LEnd - LStart, AInnerHtml);
  12.     end;
  13.   end;
  14. end;  
  15.  

Anyway, so the question is, what do you think is an important "missing function" (based on your own developments), and what would be your implementation?

Cheers.

« Last Edit: October 04, 2015, 10:11:00 am by Geepster »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: What is Missing in FreePascal?
« Reply #1 on: October 04, 2015, 10:26:04 am »
How does your PosExIC() differ from the existing PosEx() in StrUtils (except that there the offset is a cardinal, and not a const parameter)?
« Last Edit: October 04, 2015, 10:30:21 am by howardpc »

derek.john.evans

  • Guest
Re: What is Missing in FreePascal?
« Reply #2 on: October 04, 2015, 10:36:42 am »
How does your PosExIC() differ from the existing PosEx() in StrUtils (except that there the offset is a cardinal, and not a const parameter)?

IC = Ignore Case. Pos and PosEx are case sensitive.

EDIT: The often recommended answer is to use the LowerCase() function to do a case insensitivity search. It works, but to me, that just seems a ridiculous situation. Its 2015!
« Last Edit: October 04, 2015, 10:45:54 am by Geepster »

Edson

  • Hero Member
  • *****
  • Posts: 1302
Re: What is Missing in FreePascal?
« Reply #3 on: October 04, 2015, 09:48:27 pm »
Maybe it's only needed a new versión of PosEx(), with the parameter: "IgnoreCase".

I would like to have a versión of Copy(), without the parameter "Count", and that accept negative values in "Index".
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

 

TinyPortal © 2005-2018