-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
80 lines (75 loc) · 1.72 KB
/
utils.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package myanimelist
import (
"crypto/rand"
"log"
)
func makeList(objects []string) map[string]struct{} {
var list = make(map[string]struct{}, len(objects))
if len(objects) < 1 {
return list
}
for _, v := range objects {
list[v] = struct{}{}
}
return list
}
func makeListInt(objects []int) map[int]struct{} {
var list = make(map[int]struct{}, len(objects))
if len(objects) < 1 {
return list
}
for _, v := range objects {
list[v] = struct{}{}
}
return list
}
// fixSorting is small internal helper function.
// Main purpose is to avoid creating duplicate variables
// such as SortAnimeListByStartTime && SortMangaListByStartTime
func fixSorting(sort string, kind string) string {
var prefix string
if kind == "anime" {
prefix = "anime_"
} else if kind == "manga" {
prefix = "manga_"
} else {
// undefined kind
return sort
}
if sort == SortListByTitle || sort == SortListByStartDate || sort == SortListByID {
return prefix + sort
}
return sort
}
func randomString(length int, chars []byte) string {
if length == 0 {
return ""
}
charsAmount := len(chars)
if charsAmount < 2 || charsAmount > 256 {
log.Println("randomString() wrong length")
return ""
}
maxNumber := 255 - (256 % charsAmount)
resultStorage := make([]byte, length)
bytesStorage := make([]byte, length+(length/4))
i := 0
for {
if _, err := rand.Read(bytesStorage); err != nil {
log.Println("randomString() error reading random bytes: ", err)
return ""
}
for _, rb := range bytesStorage {
randomInt := int(rb)
if randomInt > maxNumber {
// Skip this number to avoid modulo bias.
continue
}
resultStorage[i] = chars[randomInt%charsAmount]
i++
if i == length {
return string(resultStorage)
}
}
}
}