Skip to content

Commit

Permalink
delete all the 1.23 polyfills
Browse files Browse the repository at this point in the history
  • Loading branch information
mcy committed Feb 28, 2025
1 parent f3565ea commit 4c3f337
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 99 deletions.
5 changes: 2 additions & 3 deletions experimental/internal/taxa/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ import (
"fmt"
"iter"
"math/bits"
"slices"
"strings"

"github.com/bufbuild/protocompile/internal/ext/slicesx"
)

// Set is a set of [Noun] values, implicitly ordered by the [Noun] values'
Expand Down Expand Up @@ -97,7 +96,7 @@ func (s Set) All() iter.Seq[Noun] {
//
// If the set is empty, returns the empty string.
func (s Set) Join(conj string) string {
elems := slicesx.Collect(s.All())
elems := slices.Collect(s.All())

var out strings.Builder
switch len(elems) {
Expand Down
4 changes: 2 additions & 2 deletions experimental/internal/taxa/taxa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
package taxa_test

import (
"slices"
"testing"

"github.com/stretchr/testify/assert"

"github.com/bufbuild/protocompile/experimental/internal/taxa"
"github.com/bufbuild/protocompile/internal/ext/slicesx"
)

func TestSet(t *testing.T) {
Expand All @@ -38,7 +38,7 @@ func TestSet(t *testing.T) {

assert.Equal(t,
[]taxa.Noun{taxa.EOF, taxa.Decl, taxa.Message, taxa.Array, taxa.Comment},
slicesx.Collect(set.All()),
slices.Collect(set.All()),
)
}

Expand Down
3 changes: 2 additions & 1 deletion experimental/parser/diagnostics_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package parser

import (
"fmt"
"slices"
"unicode"
"unicode/utf8"

Expand Down Expand Up @@ -164,7 +165,7 @@ func justify(stream *token.Stream, span report.Span, message string, edits ...ju
}

return report.SuggestEditsWithWidening(span, message,
slicesx.Collect(slicesx.Map(edits, func(j justified) report.Edit { return j.Edit }))...)
slices.Collect(slicesx.Map(edits, func(j justified) report.Edit { return j.Edit }))...)
}

// doJustifyBetween performs "between" justification.
Expand Down
4 changes: 3 additions & 1 deletion experimental/parser/parse_decl.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package parser

import (
"slices"

"github.com/bufbuild/protocompile/experimental/ast"
"github.com/bufbuild/protocompile/experimental/internal/taxa"
"github.com/bufbuild/protocompile/experimental/report"
Expand Down Expand Up @@ -57,7 +59,7 @@ func parseDecl(p *parser, c *token.Cursor, in taxa.Noun) ast.DeclAny {
})
case 2:
p.Error(errUnexpected{
what: report.JoinSeq(slicesx.Values(unexpected)),
what: report.JoinSeq(slices.Values(unexpected)),
where: in.In(),
want: startsDecl,
got: "tokens",
Expand Down
4 changes: 1 addition & 3 deletions experimental/report/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (
"sync"
"unicode"
"unicode/utf8"

"github.com/bufbuild/protocompile/internal/ext/slicesx"
)

// Spanner is any type with a [Span].
Expand Down Expand Up @@ -151,7 +149,7 @@ func (s Span) String() string {
// If there are at least two distinct files among the non-zero spans,
// this function panics.
func Join(spans ...Spanner) Span {
return JoinSeq[Spanner](slicesx.Values(spans))
return JoinSeq[Spanner](slices.Values(spans))
}

// JoinSeq is like [Join], but takes a sequence of any spannable type.
Expand Down
16 changes: 8 additions & 8 deletions experimental/token/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
package token_test

import (
"slices"
"testing"

"github.com/stretchr/testify/assert"

"github.com/bufbuild/protocompile/experimental/report"
"github.com/bufbuild/protocompile/experimental/token"
"github.com/bufbuild/protocompile/experimental/token/keyword"
"github.com/bufbuild/protocompile/internal/ext/slicesx"
)

type Context struct {
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestLeafTokens(t *testing.T) {
assert.True(tok.IsLeaf())
assert.Equal(text, tok.Text())
assert.Equal(token.Ident, abc.Kind())
tokensEq(t, slicesx.Collect(tok.Children().Rest()))
tokensEq(t, slices.Collect(tok.Children().Rest()))
}

assertIdent(abc, 0, 3, "abc")
Expand All @@ -88,7 +88,7 @@ func TestLeafTokens(t *testing.T) {
assert.True(jkl.IsLeaf())
assert.True(jkl.IsSynthetic())
assert.Equal("jkl", jkl.Text())
tokensEq(t, slicesx.Collect(jkl.Children().Rest()))
tokensEq(t, slices.Collect(jkl.Children().Rest()))
}

func TestTreeTokens(t *testing.T) {
Expand Down Expand Up @@ -135,11 +135,11 @@ func TestTreeTokens(t *testing.T) {
tokenEq(t, start, open)
tokenEq(t, end, close)

tokensEq(t, slicesx.Collect(open2.Children().Rest()), x)
tokensEq(t, slicesx.Collect(close2.Children().Rest()), x)
tokensEq(t, slices.Collect(open2.Children().Rest()), x)
tokensEq(t, slices.Collect(close2.Children().Rest()), x)

tokensEq(t, slicesx.Collect(open.Children().Rest()), def, open2, comma, message)
tokensEq(t, slicesx.Collect(close.Children().Rest()), def, open2, comma, message)
tokensEq(t, slices.Collect(open.Children().Rest()), def, open2, comma, message)
tokensEq(t, slices.Collect(close.Children().Rest()), def, open2, comma, message)

open3 := s.NewPunct("(")
close3 := s.NewPunct(")")
Expand All @@ -157,7 +157,7 @@ func TestTreeTokens(t *testing.T) {
tokenEq(t, start, open3)
tokenEq(t, end, close3)

tokensEq(t, slicesx.Collect(close3.Children().Rest()), def, open2)
tokensEq(t, slices.Collect(close3.Children().Rest()), def, open2)
}

// tokenEq is the singular version of tokensEq.
Expand Down
18 changes: 2 additions & 16 deletions internal/ext/mapsx/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,13 @@ package mapsx

import "iter"

// Collect polyfills [maps.Collect].
func Collect[K comparable, V any](seq iter.Seq2[K, V]) map[K]V {
return Insert(make(map[K]V), seq)
}

// Insert polyfills [maps.Insert].
func Insert[M ~map[K]V, K comparable, V any](m M, seq iter.Seq2[K, V]) M {
seq(func(k K, v V) bool {
m[k] = v
return true
})
return m
}

// CollectSet is like [Collect], but it implicitly fills in each map value
// CollectSet is like [maps.Collect], but it implicitly fills in each map value
// with a struct{} value.
func CollectSet[K comparable](seq iter.Seq[K]) map[K]struct{} {
return InsertKeys(make(map[K]struct{}), seq)
}

// InsertKeys is like [Insert], but it implicitly fills in each map value
// InsertKeys is like [maps.Insert], but it implicitly fills in each map value
// with the zero value.
func InsertKeys[M ~map[K]V, K comparable, V any](m M, seq iter.Seq[K]) M {
seq(func(k K) bool {
Expand Down
13 changes: 0 additions & 13 deletions internal/ext/mapsx/mapsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,6 @@
// package mapsx contains extensions to Go's package maps.
package mapsx

import "iter"

// Keys polyfills [maps.Keys].
func Keys[M ~map[K]V, K comparable, V any](m M) iter.Seq[K] {
return func(yield func(k K) bool) {
for k := range m {
if !yield(k) {
return
}
}
}
}

// KeySet returns a copy of m, with its values replaced with empty structs.
func KeySet[M ~map[K]V, K comparable, V any](m M) map[K]struct{} {
// return CollectSet(Keys(m))
Expand Down
3 changes: 2 additions & 1 deletion internal/ext/slicesx/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package slicesx

import (
"slices"
"unsafe"

"github.com/bufbuild/protocompile/internal/ext/iterx"
Expand All @@ -24,7 +25,7 @@ import (
// IndexFunc is like [slices.IndexFunc], but also takes the index of the element
// being examined as an input to the predicate.
func IndexFunc[S ~[]E, E any](s S, p func(int, E) bool) int {
return iterx.Index2(All(s), p)
return iterx.Index2(slices.All(s), p)
}

// PointerIndex returns an integer n such that p == &s[n], or -1 if there is
Expand Down
50 changes: 2 additions & 48 deletions internal/ext/slicesx/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,61 +16,15 @@ package slicesx

import (
"iter"
"slices"

"github.com/bufbuild/protocompile/internal/ext/iterx"
)

// All is a polyfill for [slices.All].
func All[S ~[]E, E any](s S) iter.Seq2[int, E] {
return func(yield func(int, E) bool) {
for i, v := range s {
if !yield(i, v) {
return
}
}
}
}

// Backward is a polyfill for [slices.Backward].
func Backward[S ~[]E, E any](s S) iter.Seq2[int, E] {
return func(yield func(int, E) bool) {
for i := len(s) - 1; i >= 0; i-- {
if !yield(i, s[i]) {
return
}
}
}
}

// Values is a polyfill for [slices.Values].
func Values[S ~[]E, E any](s S) iter.Seq[E] {
return func(yield func(E) bool) {
for _, v := range s {
if !yield(v) {
return
}
}
}
}

// Collect polyfills [slices.Collect].
func Collect[E any](seq iter.Seq[E]) []E {
return AppendSeq[[]E](nil, seq)
}

// AppendSeq polyfills [slices.AppendSeq].
func AppendSeq[S ~[]E, E any](s S, seq iter.Seq[E]) []E {
seq(func(v E) bool {
s = append(s, v)
return true
})
return s
}

// Map is a helper for generating a mapped iterator over a slice, to avoid
// a noisy call to [Values].
func Map[S ~[]E, E, U any](s S, f func(E) U) iter.Seq[U] {
return iterx.Map(Values(s), f)
return iterx.Map(slices.Values(s), f)
}

// PartitionFunc returns an iterator of the largest substrings of s of equal
Expand Down
8 changes: 5 additions & 3 deletions internal/ext/stringsx/stringsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ func Bytes(s string) iter.Seq[byte] {
}
}

// Split is like [strings.Split], but returning an iterator instead of a slice.
// Split polyfills [strings.SplitSeq].
//
// Remove in go 1.24.
func Split[Sep string | rune](s string, sep Sep) iter.Seq[string] {
r := string(sep)
return func(yield func(string) bool) {
Expand All @@ -126,9 +128,9 @@ func Split[Sep string | rune](s string, sep Sep) iter.Seq[string] {
}
}

// Lines returns an iterator over the lines in the given string.
// Split polyfills [strings.Lines].
//
// It is equivalent to Split(s, '\n').
// Remove in go 1.24.
func Lines(s string) iter.Seq[string] {
return Split(s, '\n')
}
Expand Down

0 comments on commit 4c3f337

Please sign in to comment.