-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
strutils.multiReplace() crashes if search string is "" #9557
Comments
I am not sure if "" should be supported. If the search engine is non utf-8 multibyte characters aware, it can destroy characters. I would prefer if this would just raise an error for illegal argument. |
Excellent point that I never considered before. However, IMO an empty string simply doesn't "match" and so the original string is returned unmodified. |
Search string could be "" at runtime, I would prefer the proc would handle it rather than having to check myself. It's not too much work but everyone else using the proc will also benefit. That being said, even the import strutils
echo "abc".replace("", "a")
# Prints aaabaca |
import nre
doAssert replace("foo", re"", "-") == "-f-o-o-"
|
This should be consistent with |
That is what I said before knowing it breaks UTF-8 and can only be fixed by even more special logic for empty matches which is bad. |
@Araq
and could that behavior be to throw an error (either doAssert or ValueError) |
@krux02 there were a few options discussed in this issue; which option did you pick in your commit? |
@timotheecour well you can look in the commit and see. But sure. In this PR strutils won't match empty strings on anything anymore. I didn't touch |
for clarity, here's new behavior:
also, @genotrance I think your original issue was wrong, since |
Really? We've changed the way |
Was it changed in the past? The only alternative to "empty string doesn't match on anything" is to make everything |
the only alternative is throwing, which seems saner behavior to catch bugs (empty string is probably a bug in user code in that case) |
We don't throw on API usage bugs, we could have used |
/cc @Araq
huh? see #9280 (which added
As for the rest of your comment:
which is why I suggested in #9280 (comment) (as well as here) to throw (by that I mean a recoverable exception) instead of That was a main point behind the exception hierarchy rewrite in #8237 |
But programming bugs are not recoverable/catchable errors, see the RFC I wrote. |
Crash output
This works on 0.18.0 but fails on 0.19.0 and #head.
The text was updated successfully, but these errors were encountered: