more naming

spir denis.spir at gmail.com
Sat Jan 22 12:11:38 PST 2011


On 01/22/2011 05:27 PM, Andrei Alexandrescu wrote:
> OK, so we have replace(haystack, needle, nail) which replaces _all_
> occurrences of needle in haystack with nail. How would you call a
> function that replaces only the _first_ occurrence of needle with nail?
>
> Must be a distinct function, not a runtime parameter to the existing
> function. This is because the function that replaces only one occurrence
> only requires nail to be an input range.

Apart voting for replaceFirst:

Python doc:
string.replace(str, old, new[, maxreplace])¶
     Return a copy of string str with all occurrences of substring old 
replaced by new. If the optional argument maxreplace is given, the first 
maxreplace occurrences are replaced.

In years and years of string processing, I have used maxreplace once, I 
guess, and the value was not 1. Does something like replace first belong 
to stdlib? Even more if it needs be so special. When needed, isnt'it 
easy to write in two lines using find's result? Also, what is the 
correct behaviour when not found (what is the one true way)?


Denis
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d mailing list