Skip to content

proposal: slices: add Trim, TrimRight, and TrimLeft #66672

@dsnet

Description

@dsnet

Proposal Details

I propose the addition of:

// Trim returns a slice with all leading and trailing elements of s contained in v removed.
func Trim[S ~[]E, E comparable](s S, v ...E) S

// TrimLeft returns a slice with all leading elements of s contained in v removed.
func TrimLeft[S ~[]E, E comparable](s S, v ...E) S

// TrimRight returns a slice with all trailing elements of s contained in v removed.
func TrimRight[S ~[]E, E comparable](s S, v ...E) S

// TrimFunc returns a slice with all leading and trailing elements of s that satisfy f removed.
func TrimFunc[S ~[]E, E any](s S, f func(E) bool) bool

// TrimLeftFunc returns a slice with all leading elements of s that satisfy f removed.
func TrimLeftFunc[S ~[]E, E any](s S, f func(E) bool) bool

// TrimRightFunc returns a slice with all trailing elements of s that satisfy f removed.
func TrimRightFunc[S ~[]E, E any](s S, f func(E) bool) bool

These functions match similar functions found in the "bytes" and "strings" packages:

  • Trim
  • TrimLeft
  • TrimRight
  • TrimFunc
  • TrimLeftFunc
  • TrimRightFunc

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions