On Sat, 18 Jun 2022 at 21:57, Jonathan Slenders <jonat...@slenders.be> wrote: > > Good catch! One correction here, I somewhat mixed up the benchmarks. I forgot > both projects of mine required support for universal line endings exactly > like splitlines() does this out of the box. That requires a more complex > regex pattern. I was actually using: > re.compile(r"\n|\r(?!\n)") > And then the regex becomes significantly slower than the splitlines() > solution, which is still much slower than it has to be. > > This makes me realize that `str.indexes(char)` is actually not what I need, > but really a `str.line_offsets()` which returns exactly the positions that > `str.splitlines()` would use. Does that make sense? > > If this is reasonable, I wouldn't mind working on the implementation.
At this point, I'm inclined to say that this is too specialised to be a string method, and it might be better as a 3rd party utility. I imagine there are problems with that, not least the issue of having to depend on a compiled extension that may not be available on all platforms. But as an *optional* dependency (with a fallback to the current splitlines approach) is it really such a problem? Sorry to be the one who's always suggesting this, but I'm genuinely interested in why a 3rd party solution isn't sufficient. After all, it has the advantage of working on older versions of Python (and given that one of your use cases is Textual, I can't imagine anyone would be happy if that required Python 2.12+...) If the proposal had still been for a general str.indexes(char), I might have thought differently (although given that re is fast when searching for a single character, that may not be worthwhile either). Paul _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/N2WF3WCZKIMP5XVKTSISW2ZEG5I2KLMS/ Code of Conduct: http://python.org/psf/codeofconduct/