Open cblach opened 1 year ago
how common is this?
Change https://go.dev/cl/530835 mentions this issue: strings: added CutSpace, bytes: added CutSpace
Do you know whether there is code in the Go standard library that would benefit from this? One of the strongest arguments for adding strings.Cut
was the number of places that it could be used in the standard library (see #46336).
Golang's cut function is very useful for reducing boilerplate, when parsing text/byte segment. Likewise the bytes.Fields and strings.Fields is useful for parsing spacing separated data fields.
However, sometimes when parsing text it's useful to Cut using white spaces as seperator and considering the the whole whitespace segment as a single seperator (like Fields does). For instance when scanning and parsing a config file that should allow any whitespace as key-value separator, this allows separation of the key and value with less boilerplate:
I propose adding strings.CutSpace and bytes.CutSpace which slices around the each instance of one or more consecutive white space characters, as defined by unicode.IsSpace, returning the text before and after the white space characters.
strings.CutSpace:
bytes.CutSpace