forked from openatx-archive/go-minitouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.go
50 lines (44 loc) · 960 Bytes
/
core.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
/*
* File: core.go
* Author : bigwavelet
* Description: core file
* Created: 2016-08-26
*/
package minitouch
import (
"math/rand"
"net"
"strconv"
"strings"
"unicode"
)
func strip(str string) string {
return strings.TrimFunc(str, unicode.IsSpace)
}
func splitLines(str string) (result []string) {
tmp := strings.Replace(str, "\r\n", "\n", -1)
tmp = strings.Replace(str, "\r", "", -1)
result = strings.Split(tmp, "\n")
return
}
func randSeq(n int) string {
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func randPort() (port int, err error) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return 0, err
}
defer listener.Close()
addr := listener.Addr().String()
_, portString, err := net.SplitHostPort(addr)
if err != nil {
return 0, err
}
return strconv.Atoi(portString)
}