-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: byteseq: add a generic byte string manipulation package #48643
Comments
Reducing code duplication is useful, but that could be done by adding an internal package. This proposal by itself would be useful if we didn't already have bytes and strings packages. But we do. What do we gain by adding a third variant? |
Proposed API allows us to use parameters of different types, you can instanitate That's quite similiar to what packages like |
I have a silly question: is it possible for the compiler to realize that there’s an unnecessary byte-slice conversion and optimize it away? |
The compiler already does this in some specific cases. I don't know if there is a general optimization for it. See https://golang.org/wiki/CompilerOptimizations. |
The strings and bytes packages have subtly different semantics around copying that I don't see how to capture in this new package. Also, the strings and bytes packages already exist and can't be deleted for compatibility reasons. It doesn't seem like a win to make a third way to do things. |
This proposal has been added to the active column of the proposals project |
It's seems reasonable for me. package strings
// Index returns the index of the first instance of substr in s, or -1 if substr is not present in s.
func Index[Substr constraints.Byteseq](s string, substr Substr) int { ... }
func Index(s, substr string) int { return Index[string](s, substr) }
... |
Permitting both |
Also #5376 |
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
This proposal is for use with #43651. I propose to define a new package,
byteseq
, that will provide simple genericfunctions to manipulate UTF-8 encoded strings and byte slices.
Goals of this proposal:
string
<->[]byte
conversion overhead.strings
andbytes
packages.~string | ~[]byte
constraint denotes that function shouldnot mutate their arguments.
API description:
Notice that API proposal below does not include functions like
strings.Map
orstrings.Join
that build a new string.The reason is avoiding dependency on
strings.Builder
.The text was updated successfully, but these errors were encountered: