-
-
Notifications
You must be signed in to change notification settings - Fork 366
/
strings.go
65 lines (51 loc) · 880 Bytes
/
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
package app
import (
"fmt"
"io"
"strconv"
"strings"
"unsafe"
)
func toString(v interface{}) string {
switch v := v.(type) {
case string:
return v
case []byte:
return btos(v)
case int:
return strconv.Itoa(v)
case float64:
return strconv.FormatFloat(v, 'f', 4, 64)
default:
return fmt.Sprint(v)
}
}
func toPath(v ...interface{}) string {
var b strings.Builder
for _, o := range v {
s := toString(o)
if s == "" {
continue
}
b.WriteByte('/')
b.WriteString(s)
}
return b.String()
}
func writeIndent(w io.Writer, indent int) {
for i := 0; i < indent*4; i++ {
w.Write(stob(" "))
}
}
func ln() []byte {
return stob("\n")
}
func btos(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
func stob(s string) []byte {
return *(*[]byte)(unsafe.Pointer(&s))
}
func pxToString(px int) string {
return strconv.Itoa(px) + "px"
}