unexpected return values using StrReplace as a BoundFunc object

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

unexpected return values using StrReplace as a BoundFunc object

06 Feb 2018, 20:52

I fail to understand why all these MsgBoxes don't show the same value respectively. Can someone shed a light on this? It seems to occur only if Needle does not appear in Haystack.

Code: Select all

#NoEnv
#Warn
#SingleInstance force
; Windows 8.1 64 bit
; Autohotkey v1.1.27.04/v1.1.27.07 32-bit Unicode
; encodage: utf-8 (with BOM)

MsgBox % RegExReplace("Haystack", "z", "ReplaceText") ; Haystack
MsgBox % Func("RegExReplace").bind("Haystack", "z", "ReplaceText").call() ; sometimes 斘/᷸/tack, sometimes 斘5᷸5tack etc.
MsgBox % (Func("RegExReplace").bind("Haystack", "z", "ReplaceText")).call() ; sometimes 斘/᷸/tack, sometimes 斘5᷸5tack etc.
MsgBox % (f:=Func("RegExReplace").bind("Haystack", "z", "ReplaceText")).call() ; Haystack
MsgBox % StrReplace("Haystack", "z", "ReplaceText") ; Haystack
MsgBox % Func("StrReplace").bind("Haystack", "z", "ReplaceText").call() ; sometimes 醘ȨÀȦtack, sometimes 醘ȣÀštack etc.
MsgBox % (Func("StrReplace").bind("Haystack", "z", "ReplaceText")).call() ; sometimes 醘ȨÀȦtack, sometimes 醘ȣÀštack etc.
MsgBox % (f:=Func("StrReplace").bind("Haystack", "z", "ReplaceText")).call() ; Haystack
Thank you.
my scripts
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: unexpected return values using StrReplace as a BoundFunc object

06 Feb 2018, 21:27

- It looks bug-like. I got the same results, although different unexpected characters. By putting in a go-between function, I didn't get any errors. (The correct result is 'Haystack', the string should remain unchanged as it does not contain 'z'.)

Code: Select all

q:: ;test bound funcs with built-in functions
MsgBox %     Func("RegExReplace").bind("Haystack", "z", "ReplaceText").call() ; sometimes 斘/᷸/tack, sometimes 斘5᷸5tack etc.
MsgBox %    (Func("RegExReplace").bind("Haystack", "z", "ReplaceText")).call() ; sometimes 斘/᷸/tack, sometimes 斘5᷸5tack etc.
MsgBox % (f:=Func("RegExReplace").bind("Haystack", "z", "ReplaceText")).call() ; Haystack

MsgBox %     Func("RegExReplace2").bind("Haystack", "z", "ReplaceText").call() ; Haystack
MsgBox %    (Func("RegExReplace2").bind("Haystack", "z", "ReplaceText")).call() ; Haystack
MsgBox % (f:=Func("RegExReplace2").bind("Haystack", "z", "ReplaceText")).call() ; Haystack
return

RegExReplace2(Haystack, NeedleRegEx, Replacement:="", ByRef OutputVarCount:="", Limit:=-1, StartingPosition:=1)
{
	return RegExReplace(Haystack, NeedleRegEx, Replacement, OutputVarCount, Limit, StartingPosition)
}
- I searched my txt copy of the documentation, and there weren't any examples of binding the func and calling it, in a one-liner. All of the examples did it in two stages, across two lines. However, users might expect that this would be doable.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: unexpected return values using StrReplace as a BoundFunc object

06 Feb 2018, 22:30

Hi jeeswg,

I haven't thought about wrapping the command into a wrapper to see if it concerns only built-in functions, thanks for tightening the problem;)
I searched my txt copy of the documentation, and there weren't any examples of binding the func and calling it, in a one-liner. All of the examples did it in two stages, across two lines.
Cheers, good to know. But I agree with you: users might expect that this would be doable.Btw, I use a lot this kind of one-line syntax and never had such unexpected results.

Since it is question of object syntax and since the use of parentheses and the order of all respective evaluations seems to modulate the behaviour, I first took a look @ objects (general information) in the documentation. However, the only remark I found in this respect concerns the hybrid usage of the dot and the brackets (Objects known limitation).
my scripts
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: unexpected return values using StrReplace as a BoundFunc object

07 Feb 2018, 07:39

I will guess that it is related to the boundfunc being released after the expression is evaluated, since no replacement occurs, the function (regexreplace) probably returns the (address of the) input string, which is also released when the boundfunc is. So the command (msgbox) is passed an invalid pointer. To illustrate,

Code: Select all

MsgBox % Func("RegExReplace").bind("Haystack", "z", "ReplaceText").call() == "Haystack"	; The left hand side of == isn't released before the comparison has been evaluated.
MsgBox % Func("RegExReplace").bind("Haystack", "z", "ReplaceText").call()
If a replacement occurs, or if you assign the result to a variable (within the expression), or if you return the result from a user defined function, the result is copied to a new buffer, which has nothing to do with the boundfunc, hence there is no problem in those cases. /end guessing

Although my imagination cannot come up with any (sensible) real use case when this would be a problem, this topic should probably be moved to bug reports for professional assessment.

Cheers.
v2
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: unexpected return values using StrReplace as a BoundFunc object

08 Mar 2018, 22:08

Bump.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: unexpected return values using StrReplace as a BoundFunc object

22 Apr 2018, 14:34

Hi everyone,
jeeswg wrote:Bump.
Yeah, I'm sorry for the delayed response...

@Helgef your explanation is convincing.
Helgef wrote:Although my imagination cannot come up with any (sensible) real use case when this would be a problem, this topic should probably be moved to bug reports for professional assessment.
Done. I agree with you. In particular, without claiming that everything shoud be retroactive from v2 to v1, it works on v2.

best regards,
my scripts

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Google [Bot], Marium0505, Nerafius and 195 guests