You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
package main
import(
"bufio""fmt""strings""unicode"
)
funcmain() {
config:="key\tthis is the key value\n"+"otherkey another key value\n"+"keywithnovalue"scanner:=bufio.NewScanner(strings.NewReader(config))
forscanner.Scan() {
key, value, found:=strings.CutSpace(scanner.Text())
fmt.Println("key:", key)
iffound {
fmt.Println("value:", value)
}
}
}
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:
// CutSpace slices s 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. The found result reports white space characters appears in s.// If no whitespace characters appear in s, CutSpace returns s, "", false.funcCutSpace(sstring) (before, afterstring, foundbool) {
i:=indexFunc(s, unicode.IsSpace, true)
ifi==-1 {
returns, "", false
}
returns[:i], TrimLeftFunc(s[i:], unicode.IsSpace), true
}
bytes.CutSpace
// CutSpace slices s 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. The found result reports white space characters appears in s.// If no whitespace characters appear in s, CutSpace returns s, nil, false.//// CutSpace returns slices of the original slice s, not copies.funcCutSpace(s []byte) (before, after []byte, foundbool) {
i:=indexFunc(s, unicode.IsSpace, true)
ifi==-1 {
returns, nil, false
}
returns[:i], TrimLeftFunc(s[i:], unicode.IsSpace), true
}
The text was updated successfully, but these errors were encountered:
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
The text was updated successfully, but these errors were encountered: