-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinkring.go
49 lines (43 loc) · 1.01 KB
/
linkring.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"net/url"
"unicode/utf8"
"github.com/29-FYI/twentynine"
)
type linkring struct {
links [twentynine.TwentyNine]twentynine.Link
i int
}
func (lr linkring) LinkLink(link twentynine.Link) (nlr linkring, ok bool) {
if len(link.Headline) < 8 || len(link.URL) < 8 || len(link.Headline) > 128 || len(link.URL) > 128 {
return
}
if !utf8.ValidString(link.Headline) || !utf8.ValidString(link.URL) {
return
}
if _, err := url.Parse(link.URL); err != nil {
return
}
lr.links[lr.i] = link
lr.i = (lr.i + 1) % twentynine.TwentyNine
return lr, true
}
func (lr linkring) Link(i int) (link twentynine.Link, ok bool) {
if i > twentynine.TwentyNine {
return
}
link = lr.links[(twentynine.TwentyNine-i-1+lr.i)%twentynine.TwentyNine]
ok = true
return
}
func (lr linkring) Links() (links []twentynine.Link) {
links = make([]twentynine.Link, 0)
for i := 0; i < twentynine.TwentyNine; i++ {
link, _ := lr.Link(i)
if link.Headline == "" {
break
}
links = append(links, link)
}
return
}