2014-10-21 20:30:19 +00:00
|
|
|
package util
|
2014-11-13 10:55:51 +00:00
|
|
|
|
2014-10-21 20:30:19 +00:00
|
|
|
import "strings"
|
|
|
|
|
2014-11-27 20:36:21 +00:00
|
|
|
// Split a domain name a.b.c.d.e into parts e (the head) and a.b.c.d (the rest).
|
2014-12-01 00:44:06 +00:00
|
|
|
func SplitDomainHead(name string) (head, rest string) {
|
2014-11-27 20:36:21 +00:00
|
|
|
if len(name) > 0 && name[len(name)-1] == '.' {
|
|
|
|
name = name[0 : len(name)-1]
|
|
|
|
}
|
|
|
|
|
2014-11-13 10:55:51 +00:00
|
|
|
parts := strings.Split(name, ".")
|
2014-10-21 20:30:19 +00:00
|
|
|
|
2014-11-13 10:55:51 +00:00
|
|
|
head = parts[len(parts)-1]
|
2014-10-21 20:30:19 +00:00
|
|
|
|
2014-11-13 10:55:51 +00:00
|
|
|
if len(parts) >= 2 {
|
|
|
|
rest = strings.Join(parts[0:len(parts)-1], ".")
|
|
|
|
}
|
2014-10-21 20:30:19 +00:00
|
|
|
|
2014-11-13 10:55:51 +00:00
|
|
|
return
|
2014-10-21 20:30:19 +00:00
|
|
|
}
|
2014-11-11 10:35:48 +00:00
|
|
|
|
2014-12-01 00:44:06 +00:00
|
|
|
// Split a domain name a.b.c.d.e into parts a (the tail) and b.c.d.e (the rest).
|
|
|
|
func SplitDomainTail(name string) (tail, rest string) {
|
|
|
|
s := strings.SplitN(name, ".", 2)
|
|
|
|
|
|
|
|
if len(s) == 1 {
|
|
|
|
return s[0], ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return s[0], s[1]
|
|
|
|
}
|
|
|
|
|
2014-11-11 10:35:48 +00:00
|
|
|
// © 2014 Hugo Landau <hlandau@devever.net> GPLv3 or later
|