diff --git a/kittens/choose_files/scan_test.go b/kittens/choose_files/scan_test.go index 146cc8ed0..0d230a7ac 100644 --- a/kittens/choose_files/scan_test.go +++ b/kittens/choose_files/scan_test.go @@ -94,7 +94,7 @@ func (n node) ReadFile(name string) ([]byte, error) { return nil, fs.ErrNotExist } p := &n - for _, x := range strings.Split(strings.Trim(name, string(os.PathSeparator)), string(os.PathSeparator)) { + for x := range strings.SplitSeq(strings.Trim(name, string(os.PathSeparator)), string(os.PathSeparator)) { c, found := p.children[x] if !found { return nil, fs.ErrNotExist @@ -109,7 +109,7 @@ func (n node) ReadDir(name string) ([]fs.DirEntry, error) { return n.dir_entries(), nil } p := &n - for _, x := range strings.Split(strings.Trim(name, string(os.PathSeparator)), string(os.PathSeparator)) { + for x := range strings.SplitSeq(strings.Trim(name, string(os.PathSeparator)), string(os.PathSeparator)) { c, found := p.children[x] if !found { return nil, fs.ErrNotExist @@ -334,11 +334,11 @@ func TestSortedResults(t *testing.T) { } func run_scoring(b *testing.B, depth, breadth int, query string) { - b.StopTimer() + root := node{name: string(os.PathSeparator)} root.generate_random_tree(depth, breadth) - b.StartTimer() - for range b.N { + + for b.Loop() { b.StopTimer() wg := sync.WaitGroup{} wg.Add(1) diff --git a/kittens/choose_fonts/face.go b/kittens/choose_fonts/face.go index 477a10289..aaa6dd0d7 100644 --- a/kittens/choose_fonts/face.go +++ b/kittens/choose_fonts/face.go @@ -101,7 +101,7 @@ func (self *face_panel) draw_axis(sz loop.ScreenSize, y int, ax VariableAxis, ax } frac := (min(axis_value, ax.Maximum) - ax.Minimum) / (ax.Maximum - ax.Minimum) current_cell := int(math.Floor(frac * float64(num_of_cells-1))) - for i := 0; i < num_of_cells; i++ { + for i := range num_of_cells { buf.WriteString(utils.IfElse(i == current_cell, lp.SprintStyled(current_val_style, `⬤`), tui.InternalHyperlink("•", fmt.Sprintf("axis:%d/%d:%s", i, num_of_cells-1, ax.Tag)))) } diff --git a/kittens/clipboard/read.go b/kittens/clipboard/read.go index 663433671..e2e138caf 100644 --- a/kittens/clipboard/read.go +++ b/kittens/clipboard/read.go @@ -258,7 +258,7 @@ func parse_escape_code(etype loop.EscapeCodeType, data []byte) (metadata map[str } } if len(parts) > 1 { - for _, record := range bytes.Split(parts[1], utils.UnsafeStringToBytes(":")) { + for record := range bytes.SplitSeq(parts[1], utils.UnsafeStringToBytes(":")) { rp := bytes.SplitN(record, utils.UnsafeStringToBytes("="), 2) v := "" if len(rp) == 2 { diff --git a/kittens/desktop_ui/portal.go b/kittens/desktop_ui/portal.go index 9f010906e..c0595c0e4 100644 --- a/kittens/desktop_ui/portal.go +++ b/kittens/desktop_ui/portal.go @@ -452,7 +452,7 @@ Exec=%s desktop-ui run-server patched_file := "" desktops := utils.Filter(strings.Split(d, ":"), func(x string) bool { return x != "" }) desktops = append(desktops, "") - for _, x := range strings.Split(d, ":") { + for x := range strings.SplitSeq(d, ":") { q := filepath.Join(cf, utils.IfElse(x == "", "portals.conf", fmt.Sprintf("%s-portals.conf", strings.ToLower(x)))) if text, err := os.ReadFile(q); err == nil { text := patch_portals_conf(text) diff --git a/kittens/diff/diff.go b/kittens/diff/diff.go index da903b3a1..ca9b6667c 100644 --- a/kittens/diff/diff.go +++ b/kittens/diff/diff.go @@ -119,10 +119,7 @@ func Diff(oldName, old, newName, new string, num_of_context_lines int) []byte { // End chunk with common lines for context. if len(ctext) > 0 { - n := end.x - start.x - if n > C { - n = C - } + n := min(end.x-start.x, C) for _, s := range x[start.x : start.x+n] { ctext = append(ctext, " "+s) count.x++ @@ -237,7 +234,7 @@ func tgs(x, y []string) []pair { for i := range T { T[i] = n + 1 } - for i := 0; i < n; i++ { + for i := range n { k := sort.Search(n, func(k int) bool { return T[k] >= J[i] }) diff --git a/kittens/hints/marks.go b/kittens/hints/marks.go index 1dab50887..b24da2f9d 100644 --- a/kittens/hints/marks.go +++ b/kittens/hints/marks.go @@ -79,7 +79,7 @@ func process_escape_codes(text string) (ans string, hyperlinks []Mark) { active_hyperlink_url = url active_hyperlink_start_offset = start if metadata != "" { - for _, entry := range strings.Split(metadata, ":") { + for entry := range strings.SplitSeq(metadata, ":") { if strings.HasPrefix(entry, "id=") && len(entry) > 3 { active_hyperlink_id = entry[3:] } diff --git a/kittens/hyperlinked_grep/main.go b/kittens/hyperlinked_grep/main.go index c83f77da2..38bab8a4b 100644 --- a/kittens/hyperlinked_grep/main.go +++ b/kittens/hyperlinked_grep/main.go @@ -47,7 +47,7 @@ func get_options_for_rg() (expecting_args map[string]bool, alias_map map[string] expecting_arg := strings.Contains(s, "=") single_letter_aliases := make([]string, 0, 1) long_option_names := make([]string, 0, 1) - for _, x := range strings.Split(s, ",") { + for x := range strings.SplitSeq(s, ",") { x = strings.TrimSpace(x) if strings.HasPrefix(x, "--") { lon, _, _ := strings.Cut(x[2:], "=") @@ -136,7 +136,7 @@ func parse_args(args ...string) (delegate_to_rg bool, sanitized_args []string, k if !found || k != "hyperlink" { return fmt.Errorf("Unknown --kitten option: %s", val) } - for _, x := range strings.Split(v, ",") { + for x := range strings.SplitSeq(v, ",") { switch x { case "none": kitten_opts.context_lines = false diff --git a/kittens/notify/main.go b/kittens/notify/main.go index ff99c8918..594f09ea8 100644 --- a/kittens/notify/main.go +++ b/kittens/notify/main.go @@ -137,7 +137,7 @@ func (p *parsed_data) run_loop() (err error) { raw := utils.UnsafeBytesToString(data[len(ESC_CODE_PREFIX[2:]):]) metadata, payload, _ := strings.Cut(raw, ";") sent_identifier, payload_type := "", "" - for _, x := range strings.Split(metadata, ":") { + for x := range strings.SplitSeq(metadata, ":") { key, val, _ := strings.Cut(x, "=") switch key { case "i": diff --git a/kittens/transfer/ftc_test.go b/kittens/transfer/ftc_test.go index 9880b4396..67f62bde1 100644 --- a/kittens/transfer/ftc_test.go +++ b/kittens/transfer/ftc_test.go @@ -18,11 +18,11 @@ func TestFTCSerialization(t *testing.T) { q := func(expected string) { actual := ftc.Serialize() ad := make(map[string]bool) - for _, x := range strings.Split(actual, ";") { + for x := range strings.SplitSeq(actual, ";") { ad[x] = true } ed := make(map[string]bool) - for _, x := range strings.Split(expected, ";") { + for x := range strings.SplitSeq(expected, ";") { ed[x] = true } if diff := cmp.Diff(ed, ad); diff != "" { diff --git a/kittens/transfer/send_test.go b/kittens/transfer/send_test.go index 89969121b..0290a1c05 100644 --- a/kittens/transfer/send_test.go +++ b/kittens/transfer/send_test.go @@ -45,7 +45,7 @@ func TestPathMappingSend(t *testing.T) { actual[f.expanded_local_path] = f.remote_path } e := make(map[string]string, len(actual)) - for _, rec := range strings.Split(expected, " ") { + for rec := range strings.SplitSeq(expected, " ") { k, v, _ := strings.Cut(rec, ":") e[mp(k, false)] = mp(v, true) } diff --git a/tools/cli/completion.go b/tools/cli/completion.go index f502c57be..f78b1aea6 100644 --- a/tools/cli/completion.go +++ b/tools/cli/completion.go @@ -105,7 +105,7 @@ type Delegate struct { type Completions struct { Groups []*MatchGroup `json:"groups,omitempty"` - Delegate Delegate `json:"delegate,omitempty"` + Delegate Delegate `json:"delegate"` CurrentCmd *Command `json:"-"` AllWords []string `json:"-"` // all words passed to parse_args() diff --git a/tools/cli/files.go b/tools/cli/files.go index cdb135ef7..72e256f2b 100644 --- a/tools/cli/files.go +++ b/tools/cli/files.go @@ -7,6 +7,7 @@ import ( "mime" "os" "path/filepath" + "slices" "strings" "golang.org/x/sys/unix" @@ -185,13 +186,7 @@ func complete_by_fnmatch(prefix, cwd string, patterns []string) []string { } func complete_by_mimepat(prefix, cwd string, patterns []string) []string { - all_allowed := false - for _, p := range patterns { - if p == "*" { - all_allowed = true - break - } - } + all_allowed := slices.Contains(patterns, "*") return fname_based_completer(prefix, cwd, func(name string) bool { if all_allowed { return true diff --git a/tools/cli/markup/prettify.go b/tools/cli/markup/prettify.go index c6129f5c5..a2d541396 100644 --- a/tools/cli/markup/prettify.go +++ b/tools/cli/markup/prettify.go @@ -19,7 +19,7 @@ var _ = fmt.Print type Context struct { fmt_ctx style.Context - Cyan, Green, Blue, Magenta, Red, BrightRed, Yellow, Italic, Bold, Dim, Title, Exe, Opt, Emph, Err, Code func(args ...interface{}) string + Cyan, Green, Blue, Magenta, Red, BrightRed, Yellow, Italic, Bold, Dim, Title, Exe, Opt, Emph, Err, Code func(args ...any) string Url func(string, string) string } diff --git a/tools/cmd/at/main.go b/tools/cmd/at/main.go index f58210c49..7ee108b39 100644 --- a/tools/cmd/at/main.go +++ b/tools/cmd/at/main.go @@ -184,7 +184,7 @@ func (self *ResponseData) UnmarshalJSON(data []byte) error { type Response struct { Ok bool `json:"ok"` - Data ResponseData `json:"data,omitempty"` + Data ResponseData `json:"data"` Error string `json:"error,omitempty"` Traceback string `json:"tb,omitempty"` } diff --git a/tools/cmd/at/send_text.go b/tools/cmd/at/send_text.go index 12eed10e6..d86d77588 100644 --- a/tools/cmd/at/send_text.go +++ b/tools/cmd/at/send_text.go @@ -39,10 +39,7 @@ func parse_send_text(io_data *rc_io_data, args []string) error { } text := strings.Join(args, " ") text_gen := func(io_data *rc_io_data) (bool, error) { - limit := len(text) - if limit > 2048 { - limit = 2048 - } + limit := min(len(text), 2048) set_payload_data(io_data, "base64:"+base64.StdEncoding.EncodeToString(utils.UnsafeStringToBytes(text[:limit]))) text = text[limit:] return len(text) == 0, nil diff --git a/tools/config/utils.go b/tools/config/utils.go index f83c4cbc4..48df04ef3 100644 --- a/tools/config/utils.go +++ b/tools/config/utils.go @@ -18,7 +18,7 @@ var _ = fmt.Print func ParseStrDict(val, record_sep, field_sep string) (map[string]string, error) { ans := make(map[string]string) - for _, record := range strings.Split(val, record_sep) { + for record := range strings.SplitSeq(val, record_sep) { key, val, found := strings.Cut(record, field_sep) if found { ans[key] = val diff --git a/tools/rsync/api_test.go b/tools/rsync/api_test.go index 63bffb028..6d2a3a261 100644 --- a/tools/rsync/api_test.go +++ b/tools/rsync/api_test.go @@ -135,7 +135,7 @@ func generate_data(block_size, num_of_blocks int, extra ...string) []byte { e := strings.Join(extra, "") ans := make([]byte, num_of_blocks*block_size+len(e)) utils.Memset(ans, '_') - for i := 0; i < num_of_blocks; i++ { + for i := range num_of_blocks { offset := i * block_size copy(ans[offset:], strconv.Itoa(i)) } diff --git a/tools/simdstring/benchmarks_test.go b/tools/simdstring/benchmarks_test.go index d445d3beb..37587962b 100644 --- a/tools/simdstring/benchmarks_test.go +++ b/tools/simdstring/benchmarks_test.go @@ -33,7 +33,7 @@ func BenchmarkIndexByte(b *testing.B) { f = bytes.IndexByte } b.Run(fmt.Sprintf("%s_sz=%d", which, pos), func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { f(data, 'q') } }) @@ -54,7 +54,7 @@ func BenchmarkIndexByte2(b *testing.B) { f = index_byte2_scalar } b.Run(fmt.Sprintf("%s_sz=%d", which, pos), func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { f(data, 'q', 'x') } }) diff --git a/tools/simdstring/intrinsics_test.go b/tools/simdstring/intrinsics_test.go index b0eedef49..23f29aec3 100644 --- a/tools/simdstring/intrinsics_test.go +++ b/tools/simdstring/intrinsics_test.go @@ -48,7 +48,7 @@ func test_cmpeq_epi8(a, b []byte) []byte { func test_cmplt_epi8(t *testing.T, a, b []byte) []byte { ans := make([]byte, len(a)) var prev []byte - for which := 0; which < 3; which++ { + for which := range 3 { if len(ans) == 16 { test_cmplt_epi8_asm_128(a, b, which, ans) } else { @@ -161,7 +161,7 @@ func TestSIMDStringOps(t *testing.T) { } // test alignment issues q := []byte("abc") - for sz := 0; sz < 32; sz++ { + for sz := range 32 { test(q, '<', '>', sz) test(q, ' ', 'b', sz) test(q, '<', 'a', sz) @@ -172,7 +172,7 @@ func TestSIMDStringOps(t *testing.T) { tests := func(h string, a, b byte) { for _, sz := range []int{0, 16, 32, 64, 79} { q := strings.Repeat(" ", sz) + h - for sz := 0; sz < 32; sz++ { + for sz := range 32 { test([]byte(q), a, b, sz) } } @@ -326,7 +326,7 @@ func TestIntrinsics(t *testing.T) { if e := test_jump_if_zero(a); e != 0 { t.Fatalf("Did not detect zero register") } - for i := 0; i < sz; i++ { + for i := range sz { a = make([]byte, sz) a[i] = 1 if e := test_jump_if_zero(a); e != 1 { @@ -340,7 +340,7 @@ func TestIntrinsics(t *testing.T) { if e := test_count_to_match(a, 77); e != -1 { t.Fatalf("Unexpectedly found byte at: %d", e) } - for i := 0; i < sz; i++ { + for i := range sz { if e := test_count_to_match(a, byte(i)); e != i { t.Fatalf("Failed to find the byte: %d (%d != %d)", i, i, e) } diff --git a/tools/simdstring/scalar.go b/tools/simdstring/scalar.go index 5320aebe8..3044740ce 100644 --- a/tools/simdstring/scalar.go +++ b/tools/simdstring/scalar.go @@ -41,7 +41,7 @@ func index_byte2_string_scalar(data string, a, b byte) int { } func index_c0_scalar(data []byte) int { - for i := 0; i < len(data); i++ { + for i := range data { if data[i] == 0x7f || data[i] < ' ' { return i } diff --git a/tools/themes/collection.go b/tools/themes/collection.go index 94022f2ed..6191c92e6 100644 --- a/tools/themes/collection.go +++ b/tools/themes/collection.go @@ -688,7 +688,7 @@ func ColorSettingsAsEscapeCodes(settings map[string]string) string { set_default_color("selection_foreground", style.DefaultColors.SelectionFg, loop.SELECTION_FG) w.WriteString("\033]4") - for i := 0; i < 256; i++ { + for i := range 256 { key := "color" + strconv.Itoa(i) val := settings[key] if val != "" { diff --git a/tools/tui/run.go b/tools/tui/run.go index ff4abb4a8..223972772 100644 --- a/tools/tui/run.go +++ b/tools/tui/run.go @@ -137,7 +137,7 @@ func get_shell_name(argv0 string) (ans string) { func rc_modification_allowed(ksi string) (allowed bool, set_ksi_env_var bool) { allowed = ksi != "" set_ksi_env_var = true - for _, x := range strings.Split(ksi, " ") { + for x := range strings.SplitSeq(ksi, " ") { switch x { case "disabled": allowed = false diff --git a/tools/tui/shell_integration/api.go b/tools/tui/shell_integration/api.go index 1927a357c..283523302 100644 --- a/tools/tui/shell_integration/api.go +++ b/tools/tui/shell_integration/api.go @@ -110,7 +110,7 @@ func PathToTerminfoDb(term string) (ans string) { return ans } if td := os.Getenv("TERMINFO_DIRS"); td != "" { - for _, q := range strings.Split(td, string(os.PathListSeparator)) { + for q := range strings.SplitSeq(td, string(os.PathListSeparator)) { if q == "" { q = "/usr/share/terminfo" } diff --git a/tools/utils/base85/base85.go b/tools/utils/base85/base85.go index afbe98a73..d31199ce0 100644 --- a/tools/utils/base85/base85.go +++ b/tools/utils/base85/base85.go @@ -45,7 +45,7 @@ var decoder_array = sync.OnceValue(func() *[256]byte { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, } - for i := 0; i < len(encode); i++ { + for i := range len(encode) { decode[encode[i]] = byte(i) } return &decode @@ -101,7 +101,7 @@ func decodeChunk(decode *[256]byte, dst, src []byte) (int, int) { var val uint32 m := DecodedLen(len(src)) buf := [5]byte{84, 84, 84, 84, 84} - for i := 0; i < len(src); i++ { + for i := range src { e := decode[src[i]] if e == 0xFF { return 0, i + 1 @@ -109,7 +109,7 @@ func decodeChunk(decode *[256]byte, dst, src []byte) (int, int) { buf[i] = e } - for i := 0; i < 5; i++ { + for i := range 5 { r := buf[i] val += uint32(r) * decode_base[i] } diff --git a/tools/utils/humanize/times.go b/tools/utils/humanize/times.go index 04eb6085b..72b5bce70 100644 --- a/tools/utils/humanize/times.go +++ b/tools/utils/humanize/times.go @@ -100,7 +100,7 @@ func CustomRelTime(a, b time.Time, albl, blbl string, magnitudes []RelTimeMagnit n = len(magnitudes) - 1 } mag := magnitudes[n] - args := []interface{}{} + args := []any{} escaped := false for _, ch := range mag.Format { if escaped { diff --git a/tools/utils/longest-common.go b/tools/utils/longest-common.go index 1551395b0..cbf46b862 100644 --- a/tools/utils/longest-common.go +++ b/tools/utils/longest-common.go @@ -58,14 +58,14 @@ func LongestCommon(next func() (string, bool), prefix bool) string { return "" } if prefix { - for i := 0; i < max_len; i++ { + for i := range max_len { if xfix[i] != q[i] { xfix = xfix[:i] break } } } else { - for i := 0; i < max_len; i++ { + for i := range max_len { xi := xfix_len - i - 1 si := q_len - i - 1 if xfix[xi] != q[si] { diff --git a/tools/utils/paths.go b/tools/utils/paths.go index 3c7e7e027..3dbced37d 100644 --- a/tools/utils/paths.go +++ b/tools/utils/paths.go @@ -103,7 +103,7 @@ func ConfigDirForName(name string) (config_dir string) { add(xh) } if dirs := os.Getenv("XDG_CONFIG_DIRS"); dirs != "" { - for _, candidate := range strings.Split(dirs, ":") { + for candidate := range strings.SplitSeq(dirs, ":") { add(candidate) } } diff --git a/tools/utils/short-uuid.go b/tools/utils/short-uuid.go index ebdd43b6b..c625a233e 100644 --- a/tools/utils/short-uuid.go +++ b/tools/utils/short-uuid.go @@ -21,10 +21,7 @@ func num_to_string(number *big.Int, alphabet []rune, alphabet_len *big.Int, pad_ if number.Sign() < 0 { *number = zero } - capacity := 64 - if pad_to_length > capacity { - capacity = pad_to_length - } + capacity := max(pad_to_length, 64) ans := make([]rune, 0, capacity) for number.Cmp(&zero) == 1 { number.DivMod(number, alphabet_len, &digit) diff --git a/tools/vt/linebuf.go b/tools/vt/linebuf.go index 73d02f1b9..3cac1dbd4 100644 --- a/tools/vt/linebuf.go +++ b/tools/vt/linebuf.go @@ -18,7 +18,7 @@ type LineBuf struct { func NewLineBuf(xnum, ynum uint) *LineBuf { lm := make([]uint, ynum, ynum+extra_capacity) var i uint - for i = 0; i < ynum; i++ { + for i = range ynum { lm[i] = i } return &LineBuf{ diff --git a/tools/wcswidth/wcswidth.go b/tools/wcswidth/wcswidth.go index 02ea80a32..332ac08f6 100644 --- a/tools/wcswidth/wcswidth.go +++ b/tools/wcswidth/wcswidth.go @@ -49,7 +49,7 @@ func (self *WCWidthIterator) handle_csi(csi []byte) error { num_string := utils.UnsafeBytesToString(csi[:len(csi)-1]) n, err := strconv.Atoi(num_string) if err == nil && n > 0 { - for i := 0; i < n; i++ { + for range n { err = self.handle_rune(self.prev_ch) if err != nil { return err