Skip to content

proposal: slices: add IndexAll to find a subslice #76787

@fumin

Description

@fumin

Proposal Details

I propose adding the following function:

package slices

// IndexAll returns the index of the first instance of sep in s, or -1 if sep is not present in s. 
func IndexAll[S ~[]E, E comparable](s, sep S) int  

Currently, slices.Index is too primitive and only does the trivial thing of computing whether a single element 2 is inside say {1, 1, 2, 3}.
On the other hand, bytes.Index is more powerful and allows us to search for {1, 2, 3} within {1, 1, 2, 3}.

The implementation of slices.IndexAll could simply be a version of bytes.Index converted to generics.
This would enable users to index []int, []image.Point the same way as []byte.

Metadata

Metadata

Assignees

No one assigned

    Labels

    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolProposal

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions