File: util.go

package info (click to toggle)
golang-github-shenwei356-util 0.0~git20201231.861956c-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 192 kB
  • sloc: makefile: 2
file content (325 lines) | stat: -rw-r--r-- 6,433 bytes parent folder | download
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package byteutil

import (
	"bytes"
	// "fmt"
	"unsafe"

	"github.com/shenwei356/bpool"
)

// ReverseByteSlice reverses a byte slice
func ReverseByteSlice(s []byte) []byte {
	// make a copy of s
	l := len(s)
	t := make([]byte, l)
	for i := 0; i < l; i++ {
		t[i] = s[i]
	}

	// reverse
	for i, j := 0, l-1; i < j; i, j = i+1, j-1 {
		t[i], t[j] = t[j], t[i]
	}
	return t
}

// ReverseByteSliceInplace reverses a byte slice
func ReverseByteSliceInplace(s []byte) {
	// reverse
	for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
		s[i], s[j] = s[j], s[i]
	}
}

// WrapByteSlice wraps byte slice
func WrapByteSlice(s []byte, width int) []byte {
	if width < 1 {
		return s
	}
	l := len(s)
	if l == 0 {
		return s
	}
	var lines int
	if l%width == 0 {
		lines = l/width - 1
	} else {
		lines = int(l / width)
	}
	// var buffer bytes.Buffer
	buffer := bytes.NewBuffer(make([]byte, 0, l+lines))
	var start, end int
	for i := 0; i <= lines; i++ {
		start = i * width
		end = (i + 1) * width
		if end > l {
			end = l
		}

		buffer.Write(s[start:end])
		if i < lines {
			buffer.WriteString("\n")
		}
	}
	return buffer.Bytes()
}

// BufferedByteSliceWrapper is used to wrap byte slice,
// using a buffer of bytes.Buffer to reduce GC
type BufferedByteSliceWrapper struct {
	pool *bpool.SizedBufferPool
}

// NewBufferedByteSliceWrapper create a new BufferedByteSliceWrapper
func NewBufferedByteSliceWrapper(size, alloc int) *BufferedByteSliceWrapper {
	if size < 1 {
		panic("buffer number should be > 0")
	}
	if alloc < 1 {
		panic("buffer size should be > 0")
	}
	return &BufferedByteSliceWrapper{bpool.NewSizedBufferPool(size, alloc)}
}

// NewBufferedByteSliceWrapper2 could pre-alloc space according to length of slice and width
func NewBufferedByteSliceWrapper2(size int, length, width int) *BufferedByteSliceWrapper {
	if size < 1 {
		// panic("buffer number should be > 0")
		size = 1
	}
	if length < 1 {
		// panic("buffer size should be > 0")
		length = 1
	}
	if width <= 0 {
		return NewBufferedByteSliceWrapper(size, length)
	}
	var lines int
	if length%width == 0 {
		lines = length/width - 1
	} else {
		lines = int(length / width)
	}
	return &BufferedByteSliceWrapper{bpool.NewSizedBufferPool(size, length+lines)}
}

// Recycle a buffer
func (w *BufferedByteSliceWrapper) Recycle(b *bytes.Buffer) {
	w.pool.Put(b)
}

// Wrap a byte slice. DO NOT FORGET call Recycle() with the returned buffer
func (w *BufferedByteSliceWrapper) Wrap(s []byte, width int) ([]byte, *bytes.Buffer) {
	if width < 1 {
		return s, nil
	}
	l := len(s)
	if l == 0 {
		return s, nil
	}
	var lines int
	if l%width == 0 {
		lines = l/width - 1
	} else {
		lines = int(l / width)
	}
	// var buffer bytes.Buffer
	// buffer := bytes.NewBuffer(make([]byte, 0, l+lines))

	buffer := w.pool.Get()

	var start, end int
	for i := 0; i <= lines; i++ {
		start = i * width
		end = (i + 1) * width
		if end > l {
			end = l
		}

		buffer.Write(s[start:end])
		if i < lines {
			buffer.WriteString("\n")
		}
	}
	return buffer.Bytes(), buffer
}

// WrapByteSliceInplace wraps byte slice in place.
// Sadly, it's too slow. Never use this!
func WrapByteSliceInplace(s []byte, width int) []byte {
	if width < 1 {
		return s
	}
	var l, lines int

	l = len(s)
	if l%width == 0 {
		lines = l/width - 1
	} else {
		lines = int(l / width)
	}

	var end int
	j := 0
	for i := 0; i <= lines; i++ {
		end = (i+1)*width + j
		if end >= l {
			break
		}
		// fmt.Printf("len:%d, lines:%d, i:%d, j:%d, end:%d\n", l, lines, i, j, end)
		if i < lines {

			// https://github.com/golang/go/wiki/SliceTricks
			// Sadly, it's too slow
			// s = append(s, []byte(" ")[0])
			// copy(s[end+1:], s[end:])
			// s[end] = []byte("\n")[0]

			// slow too
			s = append(s[:end], append([]byte("\n"), s[end:]...)...)

			l = len(s)
			if l%width == 0 {
				lines = l/width - 1
			} else {
				lines = int(l / width)
			}

			j++
		}
	}
	return s
}

// SubSlice provides similar slice indexing as python with one exception
// that end could be equal to 0.
// So we could get the last element by SubSlice(s, -1, 0)
// or get the whole element by SubSlice(s, 0, 0)
func SubSlice(slice []byte, start int, end int) []byte {
	if start == 0 && end == 0 {
		return slice
	}
	if start == end || (start < 0 && end > 0) {
		return []byte{}
	}
	l := len(slice)
	s, e := start, end

	if s < 0 {
		s = l + s
		if s < 1 {
			s = 0
		}
	}
	if e < 0 {
		e = l + e
		if e < 0 {
			e = 0
		}
	}
	if e == 0 || e > l {
		e = l
	}
	return slice[s:e]
}

// ByteToLower lowers a byte
func ByteToLower(b byte) byte {
	if b <= '\u007F' {
		if 'A' <= b && b <= 'Z' {
			b += 'a' - 'A'
		}
		return b
	}
	return b
}

// ByteToUpper upper a byte
func ByteToUpper(b byte) byte {
	if b <= '\u007F' {
		if 'a' <= b && b <= 'z' {
			b -= 'a' - 'A'
		}
		return b
	}
	return b
}

// MakeQuerySlice is used to replace map.
// see: http://blog.shenwei.me/map-is-not-the-fastest-in-go/
func MakeQuerySlice(letters []byte) []byte {
	max := -1
	for i := 0; i < len(letters); i++ {
		j := int(letters[i])
		if max < j {
			max = j
		}
	}
	querySlice := make([]byte, max+1)
	for i := 0; i < len(letters); i++ {
		querySlice[int(letters[i])] = letters[i]
	}
	return querySlice
}

// Split splits a byte slice by giveen letters.
// It's much faster than regexp.Split
func Split(slice []byte, letters []byte) [][]byte {
	querySlice := MakeQuerySlice(letters)
	results := [][]byte{}
	tmp := []byte{}

	var j int
	var value byte
	var sliceSize = len(querySlice)
	for _, b := range slice {
		j = int(b)
		if j >= sliceSize { // not delimiter byte
			tmp = append(tmp, b)
			continue
		}
		value = querySlice[j]
		if value == 0 { // not delimiter byte
			tmp = append(tmp, b)
			continue
		} else {
			if len(tmp) > 0 {
				results = append(results, tmp)
				tmp = []byte{}
			}
		}
	}
	if len(tmp) > 0 {
		results = append(results, tmp)
	}
	return results
}

// Bytes2Str convert byte slice to string without GC. Warning: it's unsafe!!!
func Bytes2Str(b []byte) string {
	return *(*string)(unsafe.Pointer(&b))
}

// CountBytes counts given ASCII characters in a byte slice
func CountBytes(seq, letters []byte) int {
	if len(letters) == 0 || len(seq) == 0 {
		return 0
	}

	// do not use map
	querySlice := make([]byte, 256)
	for i := 0; i < len(letters); i++ {
		querySlice[int(letters[i])] = letters[i]
	}

	var g byte
	var n int
	for i := 0; i < len(seq); i++ {
		g = querySlice[int(seq[i])]
		if g > 0 { // not gap
			n++
		}
	}
	return n
}