 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
infogulch
Joined: 27 Mar 2008 Posts: 649
|
Posted: Tue Feb 23, 2010 9:31 pm Post subject: InStr() enchancements |
|
|
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 |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 7295 Location: Australia
|
Posted: Wed Feb 24, 2010 12:25 pm Post subject: Re: InStr() enchancements |
|
|
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 |
|
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
|
| Back to top |
|
 |
infogulch
Joined: 27 Mar 2008 Posts: 649
|
Posted: Tue Mar 02, 2010 5:00 pm Post subject: |
|
|
... 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 |
|
 |
Tuncay
Joined: 07 Nov 2006 Posts: 1886 Location: Germany
|
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Tue Mar 02, 2010 7:11 pm Post subject: Re: InStr() enchancements |
|
|
| Lexikos wrote: | | I agree that it should be built in. |
+1 |
|
| Back to top |
|
 |
MasterFocus
Joined: 08 Apr 2009 Posts: 3035 Location: Rio de Janeiro - RJ - Brasil
|
Posted: Tue Mar 02, 2010 7:57 pm Post subject: |
|
|
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 |
|
 |
dR* Guest
|
Posted: Tue Mar 02, 2010 8:13 pm Post subject: |
|
|
agree++
greets
derRaphael |
|
| Back to top |
|
 |
BigVent
Joined: 29 May 2008 Posts: 180
|
Posted: Tue Mar 02, 2010 8:25 pm Post subject: |
|
|
I concur as well! +1 _________________ ~BigVent |
|
| Back to top |
|
 |
ChalamiuS
Joined: 04 Jun 2006 Posts: 176 Location: ::1
|
Posted: Tue Mar 02, 2010 8:45 pm Post subject: |
|
|
| Lexikos wrote: | | I agree that it should be built in. |
This. |
|
| Back to top |
|
 |
tidbit
Joined: 09 Mar 2008 Posts: 1807 Location: Minnesota, USA
|
Posted: Tue Mar 02, 2010 10:31 pm Post subject: |
|
|
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 |
|
 |
ruespe
Joined: 17 Jun 2008 Posts: 243
|
Posted: Wed Mar 03, 2010 9:24 am Post subject: Re: InStr() enchancements |
|
|
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 |
|
 |
Zaelia
Joined: 31 Oct 2008 Posts: 604 Location: France
|
Posted: Sun May 30, 2010 7:37 pm Post subject: |
|
|
+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 |
|
 |
Zaelia
Joined: 31 Oct 2008 Posts: 604 Location: France
|
Posted: Mon May 31, 2010 5:20 am Post subject: |
|
|
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 |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|