-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstrings.go
71 lines (61 loc) · 1.75 KB
/
strings.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
package tbcomctl
import (
"crypto/rand"
"io"
"golang.org/x/text/language"
"golang.org/x/text/message"
tb "gopkg.in/telebot.v3"
)
// Nvlstring returns the first non-empty string from the list.
func Nvlstring(s string, ss ...string) string {
if s != "" {
return s
}
for _, alt := range ss {
if alt != "" {
return alt
}
}
return ""
}
// PrinterContext returns the Message Printer set to the language of the sender.
// It is a convenience wrapper around Printer.
func PrinterContext(c tb.Context, fallback ...string) *message.Printer {
return Printer(c.Sender().LanguageCode, fallback...)
}
// Printer returns the Message Printer for the desired lang. If the lang is not
// valid, the fallback languages will be used, if set.
func Printer(lang string, fallback ...string) *message.Printer {
tag, err := language.Parse(lang)
if err != nil {
if len(fallback) > 0 && fallback[0] != "" {
tag = language.MustParse(fallback[0])
} else {
tag = language.MustParse(FallbackLang)
}
}
return message.NewPrinter(tag)
}
const (
randStringCharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
randStringSz = len(randStringCharset)
)
var randReader = rand.Reader
// randString returns a random string of n size.
func randString(n int) string {
var buf = make([]byte, n)
if x, err := randRead(buf); err != nil || x != n {
panic("error reading from crypto source")
}
var ret = make([]byte, n)
for i := range buf {
ret[i] = randStringCharset[buf[i]%byte(randStringSz)]
}
return string(ret)
}
// randRead is a helper function that calls Reader.Read using io.ReadFull.
// On return, n == len(b) if and only if err == nil.
// (copy of crypto/rand.Read)
func randRead(b []byte) (n int, err error) {
return io.ReadFull(randReader, b)
}