AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

InStr() enchancements

 
Reply to topic    AutoHotkey Community Forum Index -> Wish List
View previous topic :: View next topic  
Author Message
infogulch



Joined: 27 Mar 2008
Posts: 649

PostPosted: Tue Feb 23, 2010 9:31 pm    Post subject: InStr() enchancements Reply with quote

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
Back to top
View user's profile Send private message
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Wed Feb 24, 2010 8:48 am    Post subject: Reply with quote

In the meantime see http://www.autohotkey.com/forum/viewtopic.php?p=334523#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 Question it returns 0 and if I understand it correctly it should be 16
_________________
AHK Wiki FAQ
TF : Text files & strings lib, TF Forum
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 7295
Location: Australia

PostPosted: Wed Feb 24, 2010 12:25 pm    Post subject: Re: InStr() enchancements Reply with quote

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. Smile
Quote:
Regardless of the value of StartingPos, the returned position is always relative to the first character of Haystack.
Source: Functions - InStr
Back to top
View user's profile Send private message Visit poster's website
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Wed Feb 24, 2010 12:57 pm    Post subject: Reply with quote

Very useful, thanks I will use it for sure.
_________________
AHK Wiki FAQ
TF : Text files & strings lib, TF Forum
Back to top
View user's profile Send private message
infogulch



Joined: 27 Mar 2008
Posts: 649

PostPosted: Tue Mar 02, 2010 5:00 pm    Post subject: Reply with quote

... 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
Back to top
View user's profile Send private message
Tuncay



Joined: 07 Nov 2006
Posts: 1886
Location: Germany

PostPosted: Tue Mar 02, 2010 6:03 pm    Post subject: Reply with quote

I agree with infogulch.
_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--
Back to top
View user's profile Send private message Send e-mail Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Tue Mar 02, 2010 7:11 pm    Post subject: Re: InStr() enchancements Reply with quote

Lexikos wrote:
I agree that it should be built in.


+1
Back to top
View user's profile Send private message Send e-mail
MasterFocus



Joined: 08 Apr 2009
Posts: 3035
Location: Rio de Janeiro - RJ - Brasil

PostPosted: Tue Mar 02, 2010 7:57 pm    Post subject: Reply with quote

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.
"

Antonio França
My stuff: Google Profile
Back to top
View user's profile Send private message Visit poster's website
dR*
Guest





PostPosted: Tue Mar 02, 2010 8:13 pm    Post subject: Reply with quote

agree++

greets
derRaphael
Back to top
BigVent



Joined: 29 May 2008
Posts: 180

PostPosted: Tue Mar 02, 2010 8:25 pm    Post subject: Reply with quote

I concur as well! +1
_________________
~BigVent
Back to top
View user's profile Send private message
ChalamiuS



Joined: 04 Jun 2006
Posts: 176
Location: ::1

PostPosted: Tue Mar 02, 2010 8:45 pm    Post subject: Reply with quote

Lexikos wrote:
I agree that it should be built in.

This.
Back to top
View user's profile Send private message
tidbit



Joined: 09 Mar 2008
Posts: 1807
Location: Minnesota, USA

PostPosted: Tue Mar 02, 2010 10:31 pm    Post subject: Reply with quote

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.
Even monkeys fall from trees. - Japanese proverb
Back to top
View user's profile Send private message
ruespe



Joined: 17 Jun 2008
Posts: 243

PostPosted: Wed Mar 03, 2010 9:24 am    Post subject: Re: InStr() enchancements Reply with quote

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
Back to top
View user's profile Send private message
Zaelia



Joined: 31 Oct 2008
Posts: 604
Location: France

PostPosted: Sun May 30, 2010 7:37 pm    Post subject: Reply with quote

+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
}
Back to top
View user's profile Send private message Send e-mail
Zaelia



Joined: 31 Oct 2008
Posts: 604
Location: France

PostPosted: Mon May 31, 2010 5:20 am    Post subject: Reply with quote

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
}
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Wish List All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group