-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrange_processor.go
61 lines (54 loc) · 1.34 KB
/
range_processor.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
// Copyright 2017 Γεράσιμος Μαρόπουλος. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package unis
// NewRange accepts "begin" and "end" indexes.
// Returns a new processor which tries to
// return the "original[begin:end]".
func NewRange(begin, end int) ProcessorFunc {
if begin <= -1 || end <= -1 {
return OriginProcessor
}
return func(str string) string {
l := len(str)
if begin < l && end < l {
str = str[begin:end]
}
return str
}
}
// NewRangeBegin almost same as NewRange but it
// accepts only a "begin" index, that means that
// it assumes that the "end" index is the last of the "original" string.
//
// Returns the "original[begin:]".
func NewRangeBegin(begin int) ProcessorFunc {
if begin <= -1 {
return OriginProcessor
}
return func(str string) string {
l := len(str)
if begin < l {
str = str[begin:]
}
return str
}
}
// NewRangeEnd almost same as NewRange but it
// accepts only an "end" index, that means that
// it assumes that the "start" index is 0 of the "original".
//
// Returns the "original[0:end]".
func NewRangeEnd(end int) ProcessorFunc {
// end should be > 0
if end <= 0 {
return OriginProcessor
}
return func(str string) string {
l := len(str)
if end < l {
str = str[0:end]
}
return str
}
}