Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
strutils.rfind start parameter is unecessarily unusual #11430
The current Nim stdlib
The reverse cases, well..It's unusual to say the least. C++ takes only a lower index bound/fence post. They call it
Impact. I just did a search of every single package in nimble (or at least 833 of them!). None uses that third
4 of those nimble packages even define their own proc called
In short, I feel like there are strong "programming in the large" expectations about what extra parameters to
import strutils let path = "/a/b/c/foo.tar.gz" let slash = path.rfind('/') # gives 6 echo path.rfind('.', start=slash) # gives -1 not 14 echo slash + path[slash..^1].rfind('.') # 14 as expected
Since from my search of nimble packages no one uses the third argument (well, 1 incorrect usage), you can probably call it whatever you want or even..
proc rfind*(s: string, sub: char, start=0, last=0): int = result = -1 let last = if last==0: s.high else: last for i in countdown(last, start): if sub == s[i]: return i
or if you want to change the name "just in case":
proc rfind*(s: string, sub: char, lowIndex=0, highIndex=0): int =
proc rfind*(s: string, sub: char, highIndex=-1): int =
or if you are strangely attatched to "activity speak":
proc rfind*(s: string, sub: char, loopBeginsAt=-1): int =
Personally, I lean toward 2 to minimize effort porting code from other languages. 1) is only first for textual flow/being the least work.
Ok. I'll do a PR today.
Quick question - we could actually maintain positional parameter passing backward compatibility if we make the signature the reverse of the
proc rfind*(s: string, sub: char, last = -1, start: Natural = 0): int
Then it's just renaming
proc rfind*(s: string, sub: char, start: Natural = 0, last = -1): int
It's debatable whether the reverse slice parameter order acts as a "usefully redundant visual cue" to indicate the "reverseness of it all" or if it seems more like a "booby trap" from moving back/forth between slice notation and parameter passing notation.
I'd go with (