forked from zikwall/gom3u-content-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
98 lines (80 loc) · 1.68 KB
/
helpers.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package gom3u_content_parser
import (
"io/ioutil"
"net/http"
"strings"
"unicode"
)
func Camelize(s string) string {
words := strings.Split(strings.Replace(s, "-", "_", -1), "_")
for i, word := range words {
words[i] = ucFirst(strings.ToLower(word))
}
return lcFirst(strings.Join(words, ""))
}
func lcFirst(s string) string {
if len(s) <= 1 {
return strings.ToLower(s)
}
return strings.ToLower(s[:1]) + s[1:]
}
func ucFirst(s string) string {
if len(s) <= 1 {
return strings.ToUpper(s)
}
return strings.ToUpper(s[:1]) + s[1:]
}
func ReadStringContentFromFile(source string) string {
b, err := ioutil.ReadFile(source)
if err != nil {
panic(err)
}
return string(b)
}
func ReadStringContentFromRemote(source string) string {
res, err := http.Get(source)
if err != nil {
panic(err)
}
contents, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
panic(err)
}
return string(contents)
}
func Find(slice []string, val string) (int, bool) {
for i, item := range slice {
if item == val {
return i, true
}
}
return -1, false
}
func ParseAttributes(str string) map[string]string {
result := map[string]string{}
lastQuote := rune(0)
f := func(c rune) bool {
switch {
case c == lastQuote:
lastQuote = rune(0)
return false
case lastQuote != rune(0):
return false
case unicode.In(c, unicode.Quotation_Mark):
lastQuote = c
return false
default:
return unicode.IsSpace(c)
}
}
items := strings.FieldsFunc(str, f)
for _, item := range items {
x := strings.Split(item, "=")
if _, exist := Find(availableAttributes, x[0]); !exist {
continue
}
result[x[0]] = strings.Replace(x[1], `"`, "", -1)
}
return result
}