-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Description
The current documentation doesn't specify the result of the following code (could be either "declare" or "decodel") because they both match at the same starting position:
strings.NewReplacer("decl", "declare", "dec", "decode").Replace("decl")The actual result is "declare", i.e. the first pair takes precedence (not the longest old string).
I just run into a use case that would require this precedence to be guaranteed, but I can't rely on it because it's not documented.
Looking at the code I presume the precedence is intentional: the generic algorithm uses a lookup trie for prioritized key/value pairs, and keys are added with decreasing priority, see here (len(oldnew)-i is the priority).
Unless there is actually a reason for it to be left undefined, I propose something like:
Leftmost matches take precedence, and among those, the leftmost pair in
NewReplacer's arguments takes precedence.
Ok, hopefully someone will come up with better words...