Forum > FPC development

PosSetEx bug

<< < (2/3) > >>

Bart:
If you consider it a documentation issue, then it should be stated that the behavoiur is undefined with values for count <= 0.
To me it would make more sense to have it behave lik PosEx with invalid count parameters.

Bart

dje:
RPosEx is interesting. It handles negitive offsets, but in a strange way.

Offsets are cardinal, so, neg values are treated as positive, and discarded with the max bound check. But, zero offset are converted to pointers:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---     p:=@s[offs];     p2:=@s[1]; And then discarded via this code since @s[0] < @s[1]

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---while (p2<=p)
So, that means PosEx and RPosEx handle zero and neg numbers by returning zero.

Although, RPosEx looks more like an accidental side effect.

marcov:
It is. RPos and the set variant are based on older Modula2 routines, where the main type (Cardinal) is unsigned.

Maybe posex got fixed for some Delphi compatibility measure, but for non delphi compat routines, I think it is wiser to simply adapt the documentation.

KodeZwerg:

--- Quote from: marcov on December 09, 2022, 04:25:00 pm ---I definitely didn't consider negative input logical
--- End quote ---
Always await the unexpected :-* else we wouldn't have this thread  O:-)

--- Quote from: marcov on December 09, 2022, 04:25:00 pm ---as it serves no purpose whatsoever.
--- End quote ---
Just because I say my opinion, it is a bug, does not mean that I am not on your side, I fully agree to you that it is ill to call it that way.

About where that bug might be, I am clueless since I am not the inventor of that routine.
Whatever he/she might have thought it should do, it should be in balance with it's documentation.
So one of them is wrong, my personal opinion, it is the method since negative index for a string can never be good.

dje:
The Delphi doc's for PosEx state:

--- Quote ---If SubStr is not found or Offset is invalid (greater than the length of S or less than 1), then the result is 0.
--- End quote ---
https://docwiki.embarcadero.com/Libraries/Sydney/en/System.StrUtils.PosEx

I understand that PosSetEx is not in Delphi, but shouldn't it follow the same logic as PosEx?.

On, a side note, I was hoping one day the function RPosSetEx would be added to match the RPosEx  :)

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version