| 1 |
package dnsutil |
| 2 |
|
| 3 |
import ( |
| 4 |
"iter" |
| 5 |
"strings" |
| 6 |
) |
| 7 |
|
| 8 |
// Join joins the labels in s to form a fully qualified domain name. |
| 9 |
func Join(ls ...string) string { |
| 10 |
if len(ls) == 0 { |
| 11 |
return "" |
| 12 |
} |
| 13 |
|
| 14 |
if ls[len(ls)-1] == "." { |
| 15 |
return Fqdn(strings.Join(ls[:len(ls)-1], ".")) |
| 16 |
} |
| 17 |
return Fqdn(strings.Join(ls, ".")) |
| 18 |
} |
| 19 |
|
| 20 |
// Split splits the name s in its labels. See [Forward] and [Next] for allocationless alternatives. |
| 21 |
func Split(s string) []string { |
| 22 |
if s == "." { |
| 23 |
return []string{"."} |
| 24 |
} |
| 25 |
|
| 26 |
if IsFqdn(s) { |
| 27 |
return strings.Split(s[:len(s)-1], ".") |
| 28 |
} |
| 29 |
return strings.Split(s, ".") |
| 30 |
} |
| 31 |
|
| 32 |
// Forward allows ranging over an name s on a per label basis. The empty string returns nothing a single root |
| 33 |
// label returns only that label. See [Next]. |
| 34 |
func Forward(s string) iter.Seq[string] { |
| 35 |
return func(yield func(string) bool) { |
| 36 |
if s == "" { |
| 37 |
return |
| 38 |
} |
| 39 |
if s == "." { |
| 40 |
yield(".") |
| 41 |
return |
| 42 |
} |
| 43 |
|
| 44 |
offset := 0 |
| 45 |
for { |
| 46 |
offset1, end := Next(s, offset) |
| 47 |
if !yield(s[offset : offset1-1]) { |
| 48 |
return |
| 49 |
} |
| 50 |
if end { |
| 51 |
return |
| 52 |
} |
| 53 |
|
| 54 |
offset = offset1 |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
// Backward allows ranging over an name s on a per label basis in reverse. The empty string returns nothing, a single root |
| 60 |
// label returns only that label. See [Prev]. |
| 61 |
func Backward(s string) iter.Seq[string] { |
| 62 |
return func(yield func(string) bool) { |
| 63 |
if s == "" { |
| 64 |
return |
| 65 |
} |
| 66 |
if s == "." { |
| 67 |
yield(".") |
| 68 |
return |
| 69 |
} |
| 70 |
|
| 71 |
offset := len(s) |
| 72 |
for { |
| 73 |
offset1, end := Prev(s, offset) |
| 74 |
if !yield(s[offset1 : offset-1]) { |
| 75 |
return |
| 76 |
} |
| 77 |
if end { |
| 78 |
return |
| 79 |
} |
| 80 |
|
| 81 |
offset = offset1 |
| 82 |
} |
| 83 |
} |
| 84 |
} |