This is an archive of the discontinued Mercurial Phabricator instance.

templates: extract function to `stringutil` for getting first line of text
ClosedPublic

Authored by martinvonz on Mar 25 2022, 1:38 AM.

Details

Summary

It's surprisingly hard to get the first line from a string, so let's
have our own function in stringutil for it.

Diff Detail

Repository
rHG Mercurial
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

martinvonz created this revision.Mar 25 2022, 1:38 AM

I wonder if we shouldn't optimize for the common case of multiple lines by using find() instead. Searching for \n first and \r second (in the substring if the first got a hit) and only then slicing the result.

I wonder if we shouldn't optimize for the common case of multiple lines by using find() instead. Searching for \n first and \r second (in the substring if the first got a hit) and only then slicing the result.

I was wondering the same, but it's easier said than done because Python doesn't seem to give access to the bytes used for splitting in splitlines(). I was thinking of using split() as you said, but I didn't think of using splitlines()` on the first part of the result, so thanks for the tip! I'll add a commit to the end of this stack.

Alphare accepted this revision.Apr 6 2022, 5:51 AM
This revision is now accepted and ready to land.Apr 6 2022, 5:51 AM