Recent

Author Topic: RPosEx param Offs has type Cardinal, why not SizeInt  (Read 288 times)

AlexTP

  • Hero Member
  • *****
  • Posts: 2713
    • UVviewsoft
RPosEx param Offs has type Cardinal, why not SizeInt
« on: January 29, 2026, 07:38:13 am »
Code: Pascal  [Select][+][-]
  1. function RPosEx(C: char; const S: AnsiString; offs: cardinal): SizeInt;
  2.  
  3. var I   : SizeUInt;
  4.     p,p2: pChar;
  5.  
  6. Begin
  7.  I:=Length(S);
  8.  If (I<>0) and (offs<=i) Then
  9.    begin
  10.      p:=@s[offs];
  11.      p2:=@s[1];
  12.      while (p2<=p) and (p^<>c) do dec(p);
  13.      RPosEx:=(p-p2)+1;
  14.    end
  15.   else
  16.     RPosEX:=0;
  17. End;

if we change type of Offs to SizeInt, it will be more logical.
« Last Edit: January 29, 2026, 08:54:15 am by AlexTP »

Zvoni

  • Hero Member
  • *****
  • Posts: 3396
Re: RPosEx param Offs has type Cardinal, why not SizeInt
« Reply #1 on: January 29, 2026, 11:32:00 am »
The only fishy thing i see with your proposal, is that "offs" is used as an Index in Line 10.
SizeInt allows negative Values, which doesn't make sense in the context of accessing a string's characters by Index
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

 

TinyPortal © 2005-2018