26 ms·
In Python, `"substring" in "string"` computes a boolean result suitable for use in an if statement. find() is for the second case. Python strings are immutable,
by wolf550e 8y ago
In Python, `"substring" in "string"` computes a boolean result suitable for use in an if statement. find() is for the second case. Python strings are immutable, so substring and split can be very cheap (pointing to same buffer without copying), but explicitly copying a substring to enable the GC to collect the large string is not very nice.
- haimez 8y agoYou might think that, but Java had the same problem with substring and decided that it WAS actually better than the alternative of sharing the original buffer. I might be wrong, but I think Java 6 or 7 changed the behavior to always copy after significant analysis of the trade off space. Pathological cases do occur and often enough to render a process entirely dead.
- Animats 8y agoGo has functions like Index(s) and LastIndex(s), which return a byte index into a UTF-8 string. These just replicate similar C library functions. The C library is perhaps not the place to look for good string semantics. Go could have returned a slice covering the rest of the string instead. Or maybe slices of "everything up to the match", "the matching part", and "everything after the match". That would avoid dealing with direct indices into a UTF-8 byte string. If the inputs are valid UTF-8, the outputs will be valid UTF-8. With indices, it's so easy to mess it up. Or worse, create a string parsing vunerability.