Port calls to slices.Sort functions since they now need a cmp() function rather than a less() function

Also rename os.SEEK_* to io.Seek* as the former has been deprecated
This commit is contained in:
Kovid Goyal 2023-08-04 22:50:13 +05:30
parent 18d48c8dcd
commit 341d845b9a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
18 changed files with 60 additions and 34 deletions

View File

@ -87,7 +87,7 @@ func preread_stdin() (data_src io.Reader, tempfile *os.File, err error) {
if err != nil {
return nil, nil, fmt.Errorf("Failed to copy data from STDIN pipe to temp file with error: %w", err)
}
tempfile.Seek(0, os.SEEK_SET)
tempfile.Seek(0, io.SeekStart)
data_src = tempfile
} else if stdin_data != nil {
data_src = bytes.NewBuffer(stdin_data)

View File

@ -7,6 +7,7 @@ import (
"encoding/base64"
"fmt"
"image"
"io"
"os"
"path/filepath"
"strings"
@ -110,7 +111,7 @@ func (self *Output) commit() {
return
}
if self.image_needs_conversion {
self.dest.Seek(0, os.SEEK_SET)
self.dest.Seek(0, io.SeekStart)
img, _, err := image.Decode(self.dest)
self.dest.Close()
os.Remove(self.dest.Name())

View File

@ -6,6 +6,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"kitty/tools/utils"
@ -42,8 +43,8 @@ func TestDiffCollectWalk(t *testing.T) {
t.Fatal(err)
}
if diff := cmp.Diff(
utils.Sort(expected_names.AsSlice(), func(a, b string) bool { return a < b }),
utils.Sort(names.AsSlice(), func(a, b string) bool { return a < b }),
utils.Sort(expected_names.AsSlice(), strings.Compare),
utils.Sort(names.AsSlice(), strings.Compare),
); diff != "" {
t.Fatal(diff)
}

View File

@ -124,7 +124,7 @@ func (self *Search) search(logical_lines *LogicalLines) {
}
})
for _, spans := range self.matches {
slices.SortFunc(spans, func(a, b Span) bool { return a.start < b.start })
slices.SortFunc(spans, func(a, b Span) int { return a.start - b.start })
}
}

View File

@ -52,7 +52,7 @@ type filesystem_file struct {
}
func (ff *filesystem_file) tell() (int64, error) {
return ff.f.Seek(0, os.SEEK_CUR)
return ff.f.Seek(0, io.SeekCurrent)
}
func (ff *filesystem_file) close() error {
@ -78,7 +78,7 @@ func (pf *patch_file) tell() (int64, error) {
s, err := os.Stat(pf.path)
return s.Size(), err
}
return pf.temp.Seek(0, os.SEEK_CUR)
return pf.temp.Seek(0, io.SeekCurrent)
}
func (pf *patch_file) close() (err error) {
@ -685,7 +685,7 @@ func files_for_receive(opts *Options, dest string, files []*remote_file, remote_
spec_paths := make([]string, len(specs))
for i := range specs {
// use the shortest path as the path for the spec
slices.SortStableFunc(spec_map[i], func(a, b *remote_file) bool { return len(a.remote_path) < len(b.remote_path) })
slices.SortStableFunc(spec_map[i], func(a, b *remote_file) int { return len(a.remote_path) - len(b.remote_path) })
spec_paths[i] = spec_map[i][0].remote_path
}
if opts.Mode == "mirror" {

View File

@ -951,7 +951,7 @@ func (self *File) next_chunk() (ans string, asz int, err error) {
}
if n <= 0 {
is_last = true
} else if pos, _ := self.actual_file.Seek(0, os.SEEK_CUR); pos >= self.file_size {
} else if pos, _ := self.actual_file.Seek(0, io.SeekCurrent); pos >= self.file_size {
is_last = true
}
chunk = chunk[:n]

View File

@ -308,12 +308,12 @@ func (self *Command) GetVisibleOptions() ([]string, map[string][]*Option) {
}
func sort_levenshtein_matches(q string, matches []string) {
utils.StableSort(matches, func(a, b string) bool {
utils.StableSort(matches, func(a, b string) int {
la, lb := utils.LevenshteinDistance(a, q, true), utils.LevenshteinDistance(b, q, true)
if la != lb {
return la < lb
return la - lb
}
return a < b
return strings.Compare(a, b)
})
}

View File

@ -15,7 +15,6 @@ import (
"fmt"
"hash"
"io"
"os"
"strconv"
"github.com/zeebo/xxh3"
@ -291,7 +290,7 @@ func (r *rsync) ApplyDelta(output io.Writer, target io.ReadSeeker, op Operation)
return err
}
write_block := func(op Operation) (err error) {
if _, err = target.Seek(int64(r.BlockSize*int(op.BlockIndex)), os.SEEK_SET); err != nil {
if _, err = target.Seek(int64(r.BlockSize*int(op.BlockIndex)), io.SeekStart); err != nil {
return err
}
n, err = io.ReadAtLeast(target, buffer, r.BlockSize)

View File

@ -909,7 +909,15 @@ func (self *Themes) ThemeByName(name string) *Theme {
func match(expression string, items []string) []*subseq.Match {
matches := subseq.ScoreItems(expression, items, subseq.Options{Level1: " "})
matches = utils.StableSort(matches, func(a, b *subseq.Match) bool { return a.Score > b.Score })
matches = utils.StableSort(matches, func(a, b *subseq.Match) int {
if b.Score < a.Score {
return -1
}
if b.Score > a.Score {
return 1
}
return 0
})
return matches
}

View File

@ -81,5 +81,5 @@ func (self *Loop) dispatch_timers(now time.Time) error {
}
func (self *Loop) sort_timers() {
slices.SortStableFunc(self.timers, func(a, b *timer) bool { return a.deadline.Before(b.deadline) })
slices.SortStableFunc(self.timers, func(a, b *timer) int { return a.deadline.Compare(b.deadline) })
}

View File

@ -90,8 +90,8 @@ func (self *History) merge_items(items ...HistoryItem) {
if !changed {
return
}
self.items = utils.StableSort(self.items, func(a, b HistoryItem) bool {
return a.Timestamp.Before(b.Timestamp)
self.items = utils.StableSort(self.items, func(a, b HistoryItem) int {
return a.Timestamp.Compare(b.Timestamp)
})
if len(self.items) > self.max_items {
self.items = self.items[len(self.items)-self.max_items:]

View File

@ -447,7 +447,7 @@ func InsertFormatting(text string, spans ...*Span) string {
var in_span *Span
ans := make([]byte, 0, 2*len(text))
var overall_sgr_state SGR
slices.SortFunc(spans, func(a, b *Span) bool { return a.Offset < b.Offset })
slices.SortFunc(spans, func(a, b *Span) int { return a.Offset - b.Offset })
text_len := 0
var ep *wcswidth.EscapeCodeParser

View File

@ -21,7 +21,15 @@ func TestSubseq(t *testing.T) {
simple := func(items, query string, expected ...string) {
matches := ScoreItems(query, utils.Splitlines(items), Options{})
if sort_by_score {
matches = utils.StableSort(matches, func(a, b *Match) bool { return a.Score > b.Score })
matches = utils.StableSort(matches, func(a, b *Match) int {
if b.Score < a.Score {
return -1
}
if b.Score > a.Score {
return 1
}
return 0
})
}
actual := make([]string, 0, len(matches))
actual_positions := make([][]int, 0, len(matches))

View File

@ -18,9 +18,9 @@ func TestUnicodeInputQueries(t *testing.T) {
if expected == nil {
expected = make([]rune, 0)
}
expected = utils.Sort(expected, func(a, b rune) bool { return a < b })
expected = utils.Sort(expected, func(a, b rune) int { return int(a) - int(b) })
actual := CodePointsForQuery(query)
actual = utils.Sort(actual, func(a, b rune) bool { return a < b })
actual = utils.Sort(actual, func(a, b rune) int { return int(a) - int(b) })
diff := cmp.Diff(expected, actual)
if diff != "" {
t.Fatalf("Failed query: %#v\n%s", query, diff)

View File

@ -244,7 +244,7 @@ func OpenNativeImageFromReader(f io.ReadSeeker) (ans *ImageData, err error) {
if err != nil {
return nil, err
}
f.Seek(0, os.SEEK_SET)
f.Seek(0, io.SeekStart)
ans = &ImageData{Width: c.Width, Height: c.Height, Format_uppercase: strings.ToUpper(fmt)}
if ans.Format_uppercase == "GIF" {
@ -563,7 +563,7 @@ func RenderWithMagick(path string, ro *RenderOptions, frames []IdentifyRecord) (
err = fmt.Errorf("Failed to render %d out of %d frames", len(frames)-len(ans), len(frames))
return
}
slices.SortFunc(ans, func(a, b *ImageFrame) bool { return a.Number < b.Number })
slices.SortFunc(ans, func(a, b *ImageFrame) int { return a.Number - b.Number })
anchor_frame := 1
for i, frame := range ans {
anchor_frame, frame.Compose_onto = SetGIFFrameDisposal(frame.Number, anchor_frame, byte(frames[i].Disposal))

View File

@ -79,13 +79,13 @@ func Repeat[T any](x T, n int) []T {
return ans
}
func Sort[T any](s []T, less func(a, b T) bool) []T {
slices.SortFunc(s, less)
func Sort[T any](s []T, cmp func(a, b T) int) []T {
slices.SortFunc(s, cmp)
return s
}
func StableSort[T any](s []T, less func(a, b T) bool) []T {
slices.SortStableFunc(s, less)
func StableSort[T any](s []T, cmp func(a, b T) int) []T {
slices.SortStableFunc(s, cmp)
return s
}
@ -98,10 +98,19 @@ func sort_with_key[T any, C constraints.Ordered](stable bool, s []T, key func(a
for i, x := range s {
temp[i].val, temp[i].key = x, key(x)
}
cmp := func(a, b t) int {
if a.key < b.key {
return -1
}
if a.key > b.key {
return 1
}
return 0
}
if stable {
slices.SortStableFunc(temp, func(a, b t) bool { return a.key < b.key })
slices.SortStableFunc(temp, cmp)
} else {
slices.SortFunc(temp, func(a, b t) bool { return a.key < b.key })
slices.SortFunc(temp, cmp)
}
for i, x := range temp {
s[i] = x.val

View File

@ -48,9 +48,9 @@ func (self *file_based_mmap) Seek(offset int64, whence int) (ret int64, err erro
switch whence {
case io.SeekStart:
self.pos = offset
case os.SEEK_END:
case io.SeekEnd:
self.pos = int64(len(self.region)) + offset
case os.SEEK_CUR:
case io.SeekCurrent:
self.pos += offset
}
return self.pos, nil

View File

@ -123,9 +123,9 @@ func (self *syscall_based_mmap) Seek(offset int64, whence int) (ret int64, err e
switch whence {
case io.SeekStart:
self.pos = offset
case os.SEEK_END:
case io.SeekEnd:
self.pos = int64(len(self.region)) + offset
case os.SEEK_CUR:
case io.SeekCurrent:
self.pos += offset
}
return self.pos, nil