AutoHotkey Community

It is currently May 27th, 2012, 4:32 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 15 posts ] 
Author Message
 Post subject: InStr() enchancements
PostPosted: February 23rd, 2010, 10:31 pm 
Offline

Joined: March 27th, 2008, 2:14 pm
Posts: 700
1. Support negative StartingPos for starting a certain number of characters from the end and going right to left.
2. Add an Occurrence param to find the n occurrence of Needle in the direction specified by StartingPos.

So the docs might read something like this: (green=new, red=omitted for brevity)
InStr() wrote:
InStr(Haystack, Needle [, CaseSensitive = false, StartingPos = 1, Occurrence = 1]): Returns the position of the first occurrence of the string Needle in the string Haystack. ... If StartingPos is omitted, it defaults to 1 (the beginning of Haystack). Otherwise, specify 2 to start at Haystack's second character, 3 to start at the third, etc. If StartingPos is beyond the length of Haystack, 0 is returned. If StartingPos is less than or equal to 0, the search is conducted in reverse (right-to-left) so that the rightmost match is found starting at StartingPos characters from the right. Specify Occurrence greater than 1 to find the Nth occurrence of Needle in the direction specified by StartingPos ....


If this is added it would give a sorely needed expression replacement for StringGetPos, without breaking any current syntax.

_________________
Scripts - License


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 24th, 2010, 9:48 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
In the meantime see http://www.autohotkey.com/forum/viewtop ... 523#334523

Edit: this
Code:
MsgBox % _InStr("this is a test this is a test", "this", False, 0)
doesn't seem to produce the correct result :?: it returns 0 and if I understand it correctly it should be 16

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
PostPosted: February 24th, 2010, 1:25 pm 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
No loops or DllCall needed:
Code:
MsgBox % InStr("this is a test this is a test", "this", False, 0)

InStr(Haystack, Needle, CaseSensitive=False, StartingPos=1, Count=1) {
    scs := A_StringCaseSense
    StringCaseSense % CaseSensitive ? "On" : A_StringCaseSense="Off" ? "Off" : "Locale"
    StringGetPos, p, Haystack, % Needle, % (StartingPos>0 ? "L" : "R") . Count, % Abs(StartingPos) - (StartingPos>0)
    StringCaseSense % scs
    return ErrorLevel ? 0 : p+1
}
I agree that it should be built in.
Here, trik wrote:
I was not sure whether to return a negative or positive offset when a negative StartingPos was sent, but I went with negative.
According to the current documentation, trik went the wrong way. :)
Quote:
Regardless of the value of StartingPos, the returned position is always relative to the first character of Haystack.
Source: Functions - InStr


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 24th, 2010, 1:57 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Very useful, thanks I will use it for sure.

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 2nd, 2010, 6:00 pm 
Offline

Joined: March 27th, 2008, 2:14 pm
Posts: 700
... but this completely misses the point. We need this functionality built in to ahk directly. If I wanted a wrapper function I would have made it myself and shut up. I made this thread because we need a built-in, expression alternative to StringGetPos and imo adding this functionality directly to InStr() is the simplest, easiest, and most direct method of achieving this goal without breaking any current scripts or negatively impacting performance.

_________________
Scripts - License


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 2nd, 2010, 7:03 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
I agree with infogulch.

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Report this post
Top
 Profile  
Reply with quote  
PostPosted: March 2nd, 2010, 8:11 pm 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Lexikos wrote:
I agree that it should be built in.


+1


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 2nd, 2010, 8:57 pm 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
I agree. Also, this addition would not break any currently existing scripts.

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 2nd, 2010, 9:13 pm 
agree++

greets
derRaphael


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 2nd, 2010, 9:25 pm 
Offline
User avatar

Joined: May 29th, 2008, 4:51 am
Posts: 186
I concur as well! +1

_________________
~BigVent


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 2nd, 2010, 9:45 pm 
Offline

Joined: June 4th, 2006, 2:04 pm
Posts: 176
Location: ::1
Lexikos wrote:
I agree that it should be built in.

This.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 2nd, 2010, 11:31 pm 
Offline

Joined: March 10th, 2008, 12:55 am
Posts: 1907
Location: Minnesota, USA
i can't believe i'm wasting my 666'th post on this (actually my 667's but some mod deleted my other one...)

I was peer-pressured to agree with infogulch. sooo
+1

_________________
rawr. be very afraid
*poke*
Note: My name is all lowercase for a reason.
"I think Bigfoot is blurry, that's the problem. It's not the photographer's fault, Bigfoot is blurry. So there's a large, out-of-focus monster roaming the countryside."


Report this post
Top
 Profile  
Reply with quote  
PostPosted: March 3rd, 2010, 10:24 am 
Offline

Joined: June 17th, 2008, 7:51 am
Posts: 243
Totaly agree. Just one addition:
InStr() wrote:
InStr(Haystack, Needle [, CaseSensitive = false, StartingPos = 1, Occurrence = 1,ExactOccurrence=false]): Returns the position of the first occurrence of the string Needle in the string Haystack. ... If StartingPos is omitted, it defaults to 1 (the beginning of Haystack). Otherwise, specify 2 to start at Haystack's second character, 3 to start at the third, etc. If StartingPos is beyond the length of Haystack, 0 is returned. If StartingPos is less than or equal to 0, the search is conducted in reverse (right-to-left) so that the rightmost match is found starting at StartingPos characters from the right. Specify Occurrence greater than 1 to find the Nth occurrence of Needle in the direction specified by StartingPos. If Occurrence is greater then the last found occurrence, the position of the last found occurrence will be returned (if there is one), unless ExactOccurence is set to True ....

_________________
Greetings
Rog


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 30th, 2010, 8:37 pm 
Offline
User avatar

Joined: October 31st, 2008, 9:39 am
Posts: 641
Location: France
+1

But with "R" option it's the mess... I think we need something like this % StrLen(%Haystack%)-Abs(StartingPos)
In other word, StartingPos is the offset position always in left to right read

If we change InStr() in AHK I really hope that Starting search position will be always in left to right, do you know example when right to left position is need ?

Howerver is not a problem, something like this:
Code:
StrPos(Haystack, Needle, PosFlag="1", Count="1") {
  StringGetPos, pos, Haystack, % Needle, % (PosFlag>0 ? "L": "R") . Count, % PosFlag>0 ? PosFlag : StrLen(%Haystack%)-PosFlag
  return pos
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 31st, 2010, 6:20 am 
Offline
User avatar

Joined: October 31st, 2008, 9:39 am
Posts: 641
Location: France
Function is very slow compared to ahk command, so I write this script for full support right to left order, I hope you like it, need other test for fix bug and optimize it...

note: It's crazy with StringGetPos with "right to left" to have returned position in "left to right" and StartPosition in "right to left"... problem appear in string like this "0000000needle00000000000needle000needle" for search the needle at middle...

About compatibility, InStr is crap for right to left so maybe we need a command like StrPos(), but it's my point of view...


Code:
;   myPos := StrPos(Haystack, Needle, p=1, w="L", m=1, s=0, r=0)
; Haystack
;   Whose contents will be searched
; Needle
;   The string to search for
; Position or Pointer (p and return of this function):
;   If positive, it is a position in left to right
;   If negative, it is a position in right to left
;   If 0, it is an error
;   If ommited, the default is 1
; Direction or Way (w) :
;   If not specified, the search direction is in normal way (left to right)
;   Else use "R" for reverse it (right to left)
; Occurence or Match (m) :
;   If ommited, the default is 1
;   Else the function find the umpteenth occurence
; String Case Sensitivity (s) :
;   If not specified, the function use the current case sensitivity
;   Else use "On", "Off" or "Locale"
; Relative or Returned Result (r) :
;   If not specified, the return have the same reading order and sign than pos
;   Else use "R" for force the return position in right to left
;   And "L" for force the return position in left to right
; note: R = right to left, reverse, relative,...


StrPos(Haystack, Needle, p=1, w="L", m=1, s=0, r=0) {
    s0 := A_StringCaseSense
StringCaseSense % s="On" ? "On" : s="Off" ? "Off" : s="Locale" ? "Locale" : s0
StringGetPos, result, Haystack, % Needle, % w . m, % ((w="L" && p>0) || (w="R" && p<0)) ? abs(p)-1 : StrLen(Haystack)-abs(p)
StringCaseSense % s0
    return  ErrorLevel ? 0 : ((p<0 && r=0) || (r="R")) ? -(StrLen(Haystack)-result+1-StrLen(Needle)) : result+1
}


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 4 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group