Skip to content

Commit 515f273

Browse files
committed
Modernize code a little
1 parent b16fff1 commit 515f273

File tree

4 files changed

+13
-17
lines changed

4 files changed

+13
-17
lines changed

html_meta_parser_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func TestExtractData_full(t *testing.T) {
9090
}
9191

9292
func BenchmarkExtractData(b *testing.B) {
93-
for j := 0; j < b.N; j++ {
93+
for b.Loop() {
9494
for i, c := range titleTestCases {
9595
title, _, err := extractData([]byte(c.body), "text/html")
9696
if err != nil {

unfurlist.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,19 @@ package unfurlist
6161

6262
import (
6363
"bytes"
64+
"cmp"
6465
"compress/zlib"
6566
"context"
6667
"crypto/sha1"
6768
_ "embed"
69+
"encoding/hex"
6870
"encoding/json"
6971
"errors"
70-
"fmt"
7172
"io"
7273
"log"
7374
"net/http"
7475
"net/url"
75-
"sort"
76+
"slices"
7677
"strings"
7778
"time"
7879

@@ -173,12 +174,6 @@ func (u *unfurlResult) Merge(u2 *unfurlResult) {
173174
}
174175
}
175176

176-
type unfurlResults []*unfurlResult
177-
178-
func (rs unfurlResults) Len() int { return len(rs) }
179-
func (rs unfurlResults) Less(i, j int) bool { return rs[i].idx < rs[j].idx }
180-
func (rs unfurlResults) Swap(i, j int) { rs[i], rs[j] = rs[j], rs[i] }
181-
182177
// ConfFunc is used to configure new unfurl handler; such functions should be
183178
// used as arguments to New function
184179
type ConfFunc func(*unfurlHandler) *unfurlHandler
@@ -241,7 +236,7 @@ func (h *unfurlHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
241236
}
242237

243238
jobResults := make(chan *unfurlResult, 1)
244-
results := make(unfurlResults, 0, len(urls))
239+
results := make([]*unfurlResult, 0, len(urls))
245240
ctx := r.Context()
246241

247242
for i, r := range urls {
@@ -252,7 +247,7 @@ func (h *unfurlHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
252247
}
253248
}(ctx, i, r, jobResults)
254249
}
255-
for i := 0; i < len(urls); i++ {
250+
for range urls {
256251
select {
257252
case <-ctx.Done():
258253
return
@@ -261,7 +256,7 @@ func (h *unfurlHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
261256
}
262257
}
263258

264-
sort.Sort(results)
259+
slices.SortFunc(results, func(a, b *unfurlResult) int { return cmp.Compare(a.idx, b.idx) })
265260
for _, r := range results {
266261
r.normalize()
267262
}
@@ -535,7 +530,8 @@ probeDefaultIcon:
535530
// mcKey returns string of hex representation of sha1 sum of string provided.
536531
// Used to get safe keys to use with memcached
537532
func mcKey(s string) string {
538-
return fmt.Sprintf("%x", sha1.Sum([]byte(s)))
533+
sum := sha1.Sum([]byte(s))
534+
return hex.EncodeToString(sum[:])
539535
}
540536

541537
func blocklisted(blocklilst []string, title string) bool {

unfurlist_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func TestUnfurlist__singleInFlightRequest(t *testing.T) {
118118

119119
var wg sync.WaitGroup
120120
barrier := make(chan struct{})
121-
for i := 0; i < 3; i++ {
121+
for range 3 {
122122
wg.Add(1)
123123
go func() {
124124
w := httptest.NewRecorder()

url_parser_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestBasicURLs(t *testing.T) {
5252
if len(got) != len(want) {
5353
t.Errorf("Length not the same got: %d != want: %d", len(got), len(want))
5454
} else {
55-
for i := 0; i < len(want); i++ {
55+
for i := range want {
5656
if got[i] != want[i] {
5757
t.Errorf("%q != %s", got, want)
5858
}
@@ -67,7 +67,7 @@ func TestBugURL(t *testing.T) {
6767
if len(got) != len(want) {
6868
t.Errorf("Length not the same got: %d != want: %d", len(got), len(want))
6969
} else {
70-
for i := 0; i < len(want); i++ {
70+
for i := range want {
7171
if got[i] != want[i] {
7272
t.Errorf("%q != %s", got, want)
7373
}
@@ -128,7 +128,7 @@ Another paragraph with implicit link http://example.com/5.
128128
`
129129
b.ReportAllocs()
130130
b.SetBytes(int64(len(text)))
131-
for i := 0; i < b.N; i++ {
131+
for b.Loop() {
132132
escape = parseMarkdownURLs(text, 10)
133133
}
134134
}

0 commit comments

Comments
 (0)