-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil-public.go
executable file
·148 lines (117 loc) · 3.04 KB
/
util-public.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
package webutil
import (
"bytes"
"fmt"
"io"
"os"
"strings"
)
// IsPortNoValid checks the ragne of an tcp/ip port number.
func (h *HTTP) IsPortNoValid(portno int) bool {
return portno > 0 && portno <= 65535
}
// RemoveCommentsFromByBiteArry removes a block of text from a byte array.
func (h *HTTP) RemoveCommentsFromBiteArry(b []byte, begin string, end string) []byte {
return h.RemoveCommentsFromByBiteArry(b, begin, end)
}
// RemoveCommentsFromByBiteArry removes a block of text from a byte array.
func (h *HTTP) RemoveCommentsFromByBiteArry(b []byte, begin string, end string) []byte {
begin = strings.Trim(begin, " ")
end = strings.Trim(end, " ")
// Avoid recursion by using a label to go through
// many iterations until all target blocks of text are removed.
lblAgain:
i := bytes.Index(b, []byte(begin))
if i < 0 {
// not found
return b
}
left := b[:i]
right := b[len(left):]
j := bytes.Index(right, []byte(end))
right = right[j+len(end):]
b = make([]byte, len(left)+len(right))
k := 0
for k = 0; k < len(left); k++ {
b[k] = left[k]
}
for p := 0; p < len(right); p++ {
b[k] = right[p]
k++
}
i = bytes.Index(b, []byte(begin))
if i > -1 {
goto lblAgain
}
return b
}
//RemoveCommentsFromString removes a block of text from inside an string.
func (h *HTTP) RemoveCommentsFromString(s string, begin string, end string) string {
begin = strings.Trim(begin, " ")
end = strings.Trim(end, " ")
// Avoid recursion by using a label to go through
// many iterations until all target blocks of text are removed.
lblAgain:
i := strings.Index(s, begin)
if i < 0 {
// not found
return s
}
left := s[:i]
right := s[len(left):]
j := strings.Index(right, end)
right = right[j+len(end):]
s = fmt.Sprintf("%s%s", left, right)
i = strings.Index(s, begin)
if i > -1 {
goto lblAgain
}
return s
}
// The following is the same as the Go ReadFile()
// func with the exception of closing the file before
// return.
//
// ../src/os/file.go
// ReadFile reads the named file and returns the contents.
// A successful call returns err == nil, not err == EOF.
// Because ReadFile reads the whole file, it does not treat an EOF from Read
// as an error to be reported.
func ReadFile(name string) ([]byte, error) {
f, err := os.Open(name)
if err != nil {
f.Close()
return nil, err
}
var size int
if info, err := f.Stat(); err == nil {
size64 := info.Size()
if int64(int(size64)) == size64 {
size = int(size64)
}
}
size++ // one byte for final read at EOF
// If a file claims a small size, read at least 512 bytes.
// In particular, files in Linux's /proc claim size 0 but
// then do not work right if read in small pieces,
// so an initial read of 1 byte would not work correctly.
if size < 512 {
size = 512
}
data := make([]byte, 0, size)
for {
if len(data) >= cap(data) {
d := append(data[:cap(data)], 0)
data = d[:len(data)]
}
n, err := f.Read(data[len(data):cap(data)])
data = data[:len(data)+n]
if err != nil {
if err == io.EOF {
err = nil
}
f.Close()
return data, err
}
}
}