Modernize Go code

This commit is contained in:
Kovid Goyal 2026-03-10 14:47:41 +05:30
parent 3d13cf1ca5
commit eddaaed3e3
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
16 changed files with 64 additions and 87 deletions

View file

@ -125,7 +125,7 @@ func run_tests(tests []*test_struct) (err error) {
case 'c':
lp.Quit(0)
case 'R':
if idx := bytes.IndexByte(data, ';'); idx > -1 {
if found := bytes.Contains(data, []byte{';'}); found {
if cpos, err := cpos_from_report(utils.UnsafeBytesToString(data[:len(data)-1])); err != nil {
return err
} else {

View file

@ -6,6 +6,7 @@ import (
"bytes"
"encoding/base64"
"fmt"
"slices"
"strconv"
"strings"
@ -261,7 +262,7 @@ func Run(args []string) (rc int, err error) {
meta, payload, _ := strings.Cut(rest, ";")
// Parse metadata key=value pairs separated by ':'
meta_map := make(map[string]string)
for _, kv := range strings.Split(meta, ":") {
for kv := range strings.SplitSeq(meta, ":") {
k, v, _ := strings.Cut(kv, "=")
if k != "" {
meta_map[k] = v
@ -300,19 +301,15 @@ func Run(args []string) (rc int, err error) {
mimes := strings.Fields(payload)
dnd.drop_mimes = mimes
// Request data for text/plain first, then text/uri-list
for _, m := range mimes {
if m == "text/plain" {
dnd.collecting = "text/plain"
lp.QueueWriteString(dnd_request_data("text/plain"))
return nil
}
if slices.Contains(mimes, "text/plain") {
dnd.collecting = "text/plain"
lp.QueueWriteString(dnd_request_data("text/plain"))
return nil
}
for _, m := range mimes {
if m == "text/uri-list" {
dnd.collecting = "text/uri-list"
lp.QueueWriteString(dnd_request_data("text/uri-list"))
return nil
}
if slices.Contains(mimes, "text/uri-list") {
dnd.collecting = "text/uri-list"
lp.QueueWriteString(dnd_request_data("text/uri-list"))
return nil
}
// Nothing to collect, signal done
lp.QueueWriteString(dnd_finish())
@ -325,25 +322,23 @@ func Run(args []string) (rc int, err error) {
if dnd.collecting == "text/plain" {
text := dnd.collect_buf.String()
// Get first line
if idx := strings.IndexByte(text, '\n'); idx >= 0 {
dnd.plain_text = text[:idx]
if before, _, ok := strings.Cut(text, "\n"); ok {
dnd.plain_text = before
} else {
dnd.plain_text = text
}
dnd.collect_buf.Reset()
// Now request text/uri-list if available
for _, m := range dnd.drop_mimes {
if m == "text/uri-list" {
dnd.collecting = "text/uri-list"
lp.QueueWriteString(dnd_request_data("text/uri-list"))
return nil
}
if slices.Contains(dnd.drop_mimes, "text/uri-list") {
dnd.collecting = "text/uri-list"
lp.QueueWriteString(dnd_request_data("text/uri-list"))
return nil
}
} else if dnd.collecting == "text/uri-list" {
text := dnd.collect_buf.String()
dnd.collect_buf.Reset()
// Parse URI list: lines starting with # are comments
for _, line := range strings.Split(text, "\n") {
for line := range strings.SplitSeq(text, "\n") {
line = strings.TrimRight(line, "\r")
if line != "" && !strings.HasPrefix(line, "#") {
dnd.uri_list = append(dnd.uri_list, line)

View file

@ -646,10 +646,3 @@ func find_hash(hh []BlockHash, hv uint64) (uint64, bool) {
}
return 0, false
}
func min(a, b int) int {
if a < b {
return a
}
return b
}

View file

@ -1,6 +1,5 @@
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
//go:build darwin || freebsd || openbsd || netbsd || dragonfly
// +build darwin freebsd openbsd netbsd dragonfly
package tty

View file

@ -309,8 +309,8 @@ func (self *Readline) history_search_highlighter(text string, x, y int) string {
lines := utils.Splitlines(text)
for _, tok := range self.history_search.tokens {
for i, line := range lines {
if idx := strings.Index(line, tok); idx > -1 {
lines[i] = line[:idx] + self.fmt_ctx.Green(tok) + line[idx+len(tok):]
if before, after, ok := strings.Cut(line, tok); ok {
lines[i] = before + self.fmt_ctx.Green(tok) + after
break
}
}

View file

@ -69,11 +69,11 @@ func ScanFuncForSeparator(sep string) StringScannerScanFunc {
}
return func(data string) (remaining_data, token string) {
idx := strings.Index(data, sep)
if idx < 0 {
before, after, ok := strings.Cut(data, sep)
if !ok {
return "", data
}
return data[idx+len(sep):], data[:idx]
return after, before
}
}

View file

@ -9,50 +9,50 @@ import (
// Benchmark color parsing functions to demonstrate performance
func BenchmarkParseOklch(b *testing.B) {
for i := 0; i < b.N; i++ {
for b.Loop() {
_, _ = parseOklch("0.5 0.1 180")
}
}
func BenchmarkParseLab(b *testing.B) {
for i := 0; i < b.N; i++ {
for b.Loop() {
_, _ = parseLab("50 0 0")
}
}
func BenchmarkParseColorHex(b *testing.B) {
for i := 0; i < b.N; i++ {
for b.Loop() {
_, _ = ParseColor("#ff0000")
}
}
func BenchmarkParseColorOklch(b *testing.B) {
for i := 0; i < b.N; i++ {
for b.Loop() {
_, _ = ParseColor("oklch(0.5 0.1 180)")
}
}
func BenchmarkParseColorLab(b *testing.B) {
for i := 0; i < b.N; i++ {
for b.Loop() {
_, _ = ParseColor("lab(50 0 0)")
}
}
func BenchmarkParseColorWithComment(b *testing.B) {
for i := 0; i < b.N; i++ {
for b.Loop() {
_, _ = ParseColor("oklch(0.5 0.1 180) # vibrant color")
}
}
// Benchmark the gamut mapping algorithm specifically
func BenchmarkOklchToSrgbGamutMap(b *testing.B) {
for i := 0; i < b.N; i++ {
for b.Loop() {
oklchToSrgbGamutMap(0.7, 0.4, 25) // Very saturated color requiring gamut mapping
}
}
func BenchmarkOklchToSrgbGamutMapInGamut(b *testing.B) {
for i := 0; i < b.N; i++ {
for b.Loop() {
oklchToSrgbGamutMap(0.5, 0.05, 180) // Already in gamut
}
}
@ -71,8 +71,7 @@ func BenchmarkParseManyColors(b *testing.B) {
"green",
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
for _, color := range colors {
_, _ = ParseColor(color)
}

View file

@ -388,10 +388,10 @@ var charprops_t3 = [106]CharProps{
// Array accessor function that avoids bounds checking
func charprops_for(x uint32) CharProps {
t1 := uintptr(*(*uint8)(unsafe.Pointer(uintptr(unsafe.Pointer(&charprops_t1[0])) + uintptr(x>>charprops_shift)*1)))
t1 := uintptr(*(*uint8)(unsafe.Add(unsafe.Pointer(&charprops_t1[0]), uintptr(x>>charprops_shift)*1)))
t1_shifted := (t1 << charprops_shift) + (uintptr(x) & charprops_mask)
t2 := uintptr(*(*uint8)(unsafe.Pointer(uintptr(unsafe.Pointer(&charprops_t2[0])) + t1_shifted*1)))
return *(*CharProps)(unsafe.Pointer(uintptr(unsafe.Pointer(&charprops_t3[0])) + t2*4))
t2 := uintptr(*(*uint8)(unsafe.Add(unsafe.Pointer(&charprops_t2[0]), t1_shifted*1)))
return *(*CharProps)(unsafe.Add(unsafe.Pointer(&charprops_t3[0]), t2*4))
}
const graphemesegmentationresult_mask = 15
@ -3285,9 +3285,9 @@ var graphemesegmentationresult_t2 = [2880]GraphemeSegmentationResult{
// Array accessor function that avoids bounds checking
func graphemesegmentationresult_for(x uint16) GraphemeSegmentationResult {
t1 := uintptr(*(*uint8)(unsafe.Pointer(uintptr(unsafe.Pointer(&graphemesegmentationresult_t1[0])) + uintptr(x>>graphemesegmentationresult_shift)*1)))
t1 := uintptr(*(*uint8)(unsafe.Add(unsafe.Pointer(&graphemesegmentationresult_t1[0]), uintptr(x>>graphemesegmentationresult_shift)*1)))
t1_shifted := (t1 << graphemesegmentationresult_shift) + (uintptr(x) & graphemesegmentationresult_mask)
return *(*GraphemeSegmentationResult)(unsafe.Pointer(uintptr(unsafe.Pointer(&graphemesegmentationresult_t2[0])) + t1_shifted*2))
return *(*GraphemeSegmentationResult)(unsafe.Add(unsafe.Pointer(&graphemesegmentationresult_t2[0]), t1_shifted*2))
}
func grapheme_segmentation_key(r GraphemeSegmentationResult, ch CharProps) uint16 {