-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.go
150 lines (140 loc) · 2.41 KB
/
utils.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
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2011, Shelby Ramsey. All rights reserved.
// Copyright 2018, Eugen Biegler. All rights reserved.
// Use of this code is governed by a BSD license that can be
// found in the LICENSE.txt file.
package sipparser
// Imports from go standard library
import (
"strings"
)
func cleanWs(s string) string {
if s == "" {
return s
}
PREFIXWS:
if len(s) > 0 {
if s[0] == ' ' || s[0] == '\t' {
s = s[1:]
goto PREFIXWS
}
}
SUFFIXWS:
if len(s) > 0 {
if s[len(s)-1] == ' ' || s[len(s)-1] == '\t' {
s = s[0 : len(s)-1]
goto SUFFIXWS
}
}
return s
}
func cleanWsOld(s string) (ns string) {
if s == "" {
return ""
}
v := strings.Split(strings.TrimSpace(s), " ")
if len(v) == 1 {
return v[0]
}
ns = v[0]
for i := 1; i < len(v); i++ {
switch {
case v[i] != "" && v[i-1] != "":
ns = ns + " " + v[i]
case v[i] != "" && v[i-1] == "":
ns = ns + v[i]
case v[i] == "" && v[i-1] != "":
ns = ns + " " + v[i]
}
}
return ns
}
func cleanBrack(s string) string {
if s == "" {
return ""
}
sLen := len(s)
var n string
switch {
case sLen > 0 && s[0] == '<':
n = s[1:]
default:
n = s
}
for i := range n {
if n[i] == '>' {
if len(n)-1 > i+1 {
if n[i+1] == ';' {
n = n[0:i] + n[i+1:]
return n
}
}
if i == len(n)-1 {
n = n[0:i]
return n
}
}
}
return n
}
func getQuoteChars(s string) (one int, two int, chk bool) {
ct := 0
for i := range s {
if s[i] == '"' {
switch {
case ct == 0:
one = i
ct = 1
case ct == 1:
two = i
return one, two, true
default:
return one, two, false
}
}
}
return 0, 0, false
}
func getBracks(s string) (one int, two int, chk bool) {
one = strings.IndexRune(s, '<')
if one == -1 {
return 0, 0, false
}
two = strings.IndexRune(s, '>')
if two == -1 {
return 0, 0, false
}
if two < one {
return 0, 0, false
}
return one, two, true
}
func getName(s string) (name string, end int) {
if s == "" {
return "", 0
}
posOne, posTwo, chk := getQuoteChars(s)
if chk == true {
if len(s)-1 > posTwo {
return cleanWs(s[posOne+1 : posTwo]), posTwo
}
return "", 0
}
posOne = strings.IndexRune(s, '<')
if posOne == -1 {
return "", 0
}
if posOne == 0 {
return "", 0
}
return cleanWs(s[0:posOne]), posOne
}
func getCommaSeperated(str string) []string {
s := strings.Split(str, ",")
if len(s) == 1 {
return nil
}
for i := range s {
s[i] = cleanWs(s[i])
}
return s
}