| 1 |
package match |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
import ( |
| 6 |
"fmt" |
| 7 |
"strings" |
| 8 |
) |
| 9 |
|
| 10 |
const lenOne = 1 |
| 11 |
const lenZero = 0 |
| 12 |
const lenNo = -1 |
| 13 |
|
| 14 |
type Matcher interface { |
| 15 |
Match(string) bool |
| 16 |
Index(string) (int, []int) |
| 17 |
Len() int |
| 18 |
String() string |
| 19 |
} |
| 20 |
|
| 21 |
type Matchers []Matcher |
| 22 |
|
| 23 |
func (m Matchers) String() string { |
| 24 |
var s []string |
| 25 |
for _, matcher := range m { |
| 26 |
s = append(s, fmt.Sprint(matcher)) |
| 27 |
} |
| 28 |
|
| 29 |
return fmt.Sprintf("%s", strings.Join(s, ",")) |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
func appendMerge(target, sub []int) []int { |
| 34 |
lt, ls := len(target), len(sub) |
| 35 |
out := make([]int, 0, lt+ls) |
| 36 |
|
| 37 |
for x, y := 0, 0; x < lt || y < ls; { |
| 38 |
if x >= lt { |
| 39 |
out = append(out, sub[y:]...) |
| 40 |
break |
| 41 |
} |
| 42 |
|
| 43 |
if y >= ls { |
| 44 |
out = append(out, target[x:]...) |
| 45 |
break |
| 46 |
} |
| 47 |
|
| 48 |
xValue := target[x] |
| 49 |
yValue := sub[y] |
| 50 |
|
| 51 |
switch { |
| 52 |
|
| 53 |
case xValue == yValue: |
| 54 |
out = append(out, xValue) |
| 55 |
x++ |
| 56 |
y++ |
| 57 |
|
| 58 |
case xValue < yValue: |
| 59 |
out = append(out, xValue) |
| 60 |
x++ |
| 61 |
|
| 62 |
case yValue < xValue: |
| 63 |
out = append(out, yValue) |
| 64 |
y++ |
| 65 |
|
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
target = append(target[:0], out...) |
| 70 |
|
| 71 |
return target |
| 72 |
} |
| 73 |
|
| 74 |
func reverseSegments(input []int) { |
| 75 |
l := len(input) |
| 76 |
m := l / 2 |
| 77 |
|
| 78 |
for i := 0; i < m; i++ { |
| 79 |
input[i], input[l-i-1] = input[l-i-1], input[i] |
| 80 |
} |
| 81 |
} |