-
Notifications
You must be signed in to change notification settings - Fork 1
/
misc.go
139 lines (136 loc) · 3.98 KB
/
misc.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package ice
import (
"strings"
kit "shylinux.com/x/toolkits"
)
func (m *Message) Split(str string, arg ...string) *Message {
m.Set(MSG_APPEND).Set(MSG_RESULT)
field := kit.Select("", arg, 0)
sp := kit.Select(SP, arg, 1)
nl := kit.Select(NL, arg, 2)
fields, indexs := kit.Split(field, sp, sp, sp), []int{}
for i, l := range kit.Split(str, nl, nl, nl) {
if strings.HasPrefix(l, "Binary") {
continue
}
if strings.TrimSpace(l) == "" {
continue
}
if i == 0 && (field == "" || field == INDEX) {
if fields = kit.Split(l, sp, sp); field == INDEX {
if strings.HasPrefix(l, SP) || strings.HasPrefix(l, TB) {
indexs = append(indexs, 0)
for _, v := range fields {
indexs = append(indexs, strings.Index(l, v)+len(v))
}
indexs = indexs[0 : len(indexs)-1]
} else {
for _, v := range fields {
indexs = append(indexs, strings.Index(l, v))
}
}
}
kit.For(fields, func(i int, v string) { fields[i] = strings.TrimPrefix(strings.TrimSpace(v), "%") })
continue
}
if len(indexs) > 0 {
for i, v := range indexs {
if v >= len(l) {
m.Push(kit.Select(SP, fields, i), "")
} else if i+1 < len(indexs) && indexs[i+1] < len(l) {
m.Push(kit.Select(SP, fields, i), strings.TrimSpace(l[v:indexs[i+1]]))
} else {
m.Push(kit.Select(SP, fields, i), strings.TrimSpace(l[v:]))
}
}
continue
}
ls := kit.Split(l, sp, sp)
for i, v := range ls {
if i == len(fields)-1 {
m.Push(kit.Select(SP, fields, i), strings.Join(ls[i:], sp))
break
}
m.Push(kit.Select(SP, fields, i), v)
}
}
return m
}
func (m *Message) SplitIndex(str string, arg ...string) *Message {
return m.Split(str, kit.Simple(INDEX, arg)...)
}
func (m *Message) SplitIndexReplace(str string, arg ...string) *Message {
return m.SplitIndex(kit.Replace(str, arg...))
}
func (m *Message) SetAppend(arg ...string) *Message {
kit.If(len(arg) == 0, func() { m.OptionFields("") })
return m.Set(MSG_APPEND, arg...)
}
func (m *Message) SetResult(arg ...string) *Message { return m.Set(MSG_RESULT, arg...) }
func (m *Message) PushRecord(value Any, arg ...string) *Message {
return m.Push("", value, kit.Split(kit.Join(arg)))
}
func (m *Message) PushDetail(value Any, arg ...string) *Message {
switch v := value.(type) {
case string:
value = kit.UnMarshal(v)
}
return m.Push(FIELDS_DETAIL, value, kit.Split(kit.Join(arg)))
}
func (m *Message) RenameOption(from, to string) *Message {
return m.Options(to, m.Option(from), from, "")
}
func (m *Message) RenameAppend(arg ...string) *Message {
kit.For(arg, func(from, to string) {
if from == to {
return
}
kit.For(m.meta[MSG_APPEND], func(i int, k string) {
if k == from {
m.meta[MSG_APPEND][i], m.meta[to] = to, m.meta[from]
delete(m.meta, from)
}
})
})
return m
}
func (m *Message) ToLowerAppend(arg ...string) *Message {
kit.For(m.meta[MSG_APPEND], func(k string) { m.RenameAppend(k, strings.ToLower(k)) })
return m
}
func (m *Message) AppendSimple(key ...string) (res []string) {
if len(key) == 0 {
if m.FieldsIsDetail() {
key = append(key, m.meta[KEY]...)
} else {
key = append(key, m.Appendv(MSG_APPEND)...)
}
}
kit.For(kit.Split(kit.Join(key)), func(k string) { res = append(res, k, m.Append(k)) })
return
}
func (m *Message) AppendTrans(cb func(value string, key string, index int) string) *Message {
if m.FieldsIsDetail() {
for i, v := range m.meta[VALUE] {
k := m.meta[KEY][i]
m.meta[VALUE][i] = cb(v, k, 0)
}
return m
}
for _, k := range m.meta[MSG_APPEND] {
for i, v := range m.meta[k] {
m.meta[k][i] = cb(v, k, i)
}
}
return m
}
func (m *Message) CmdAppend(arg ...Any) string {
args := kit.Simple(arg...)
field := kit.Slice(args, -1)[0]
return m._command(kit.Slice(args, 0, -1), OptionFields(field)).Append(field)
}
func (m *Message) CmdMap(arg ...string) map[string]map[string]string {
field, list := kit.Slice(arg, -1)[0], map[string]map[string]string{}
m._command(kit.Slice(arg, 0, -1)).Table(func(value Maps) { list[value[field]] = value })
return list
}