| 1 |
package dnsutil |
| 2 |
|
| 3 |
type skip uint |
| 4 |
|
| 5 |
// These constants are used in the [Skip] function. |
| 6 |
const ( |
| 7 |
SkipForward skip = iota + 1 |
| 8 |
SkipBackward |
| 9 |
) |
| 10 |
|
| 11 |
// Skip skips n labels in s in the desired direction. If the returned bool is true then beginning |
| 12 |
// the name was reached if this is the case s is returned as-is. The direction can be [SkipForward] |
| 13 |
// or [SkipBackward]. |
| 14 |
func Skip(s string, n int, direction skip) (string, bool) { |
| 15 |
switch direction { |
| 16 |
case SkipForward: |
| 17 |
var i, end = 0, false |
| 18 |
for range n { |
| 19 |
i, end = Next(s, i) |
| 20 |
if end { |
| 21 |
break |
| 22 |
} |
| 23 |
} |
| 24 |
return s[i:], end |
| 25 |
case SkipBackward: |
| 26 |
var i, start = len(s), false |
| 27 |
for range n { |
| 28 |
i, start = Prev(s, i) |
| 29 |
if start { |
| 30 |
break |
| 31 |
} |
| 32 |
} |
| 33 |
return s[i:], start |
| 34 |
} |
| 35 |
panic("never reached") |
| 36 |
} |