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
proposal: strings: add Lines #51039
Comments
Here are some other approaches I've seen while searching: strings.Split(strings.ReplaceAll(s, "\r\n", "\n"), "\n") regexp.MustCompile("\r?\n").Split(s, -1) |
Assuming a "line" is anything delimited by one of |
@ALTree thanks, I'll be using that. However, it doesn't give the correct output for strings with blank lines like |
Oh, right. It's actually just Fields. You want something that returns 4 strings for "text\n\n\ntext`, two of which are empty, which is different from what Fields does. |
What about adding |
What would the signature of the callback look like? Something like |
EDIT: right, that's probably why it doesn't exist. The API is less obvious. You'd need to check substrings instead of the single runes. |
If something like func SplitMulti(s, seps []string) []string
func SplitMultiAfter(s, seps []string) []string
func SplitMultiAfterN(s, seps []string, n int) []string
func SplitMultiN(s, seps []string, n int) []string |
Cut works great.
|
For that matter, strings.SplitAfter(text, "\n") also works. |
This proposal has been added to the active column of the proposals project |
One potential problem with data, _ := os.ReadFile("lines.txt)
for _, line := range strings.Lines(string(data)) {
// use line
} |
Having a reader and writers that transform line endings would be great. (Or, if https://pkg.go.dev/golang.org/x/text/transform were in std, they could just be transformers.) |
@jimmyfrasche can be done with https://pkg.go.dev/github.com/icholy/replace
|
Since you have that, how are you ending up with a string in memory that may have windows line endings? |
@jimmyfrasche Say you're working with an AST which exposes comments as multiline strings. |
It sounds like (1) the API is somewhat inefficient since it has to allocate the slice, (2) there are other ways to write the code that are more efficient and just as clean using existing standard library APIs, and (3) this could be in an external library easily instead if the exact operation of 'return a slice of all the lines' is required. In that case, it seems like this is heading toward likely decline. Do I have that right? |
Given the target use-cases, this would be negligible.
I'm not sure about "just as clean". The proposed
ofc
Ya, I think so. The potential for abuse makes it not worth it IMO. |
The problem with putting things in the standard library is that it will get used for many uses cases beyond the target. Things in the standard library have to be particularly robust against unexpected uses. |
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
If I already have a string in memory, there's no simple and efficient way of splitting it into lines.
The naïve approach won't handle
\r\n
.Using a
bufio.Scanner
works but it creates copies instead of slicing the existing string.I'd like a new
strings.Lines
function similar tostrings.Fields
for this use-case.The text was updated successfully, but these errors were encountered: