// Package dnsutil contains function that are useful in the context of working with the DNS.
package dnsutil

import "crypto/rand"

// Trim removes the zone component from s. It returns the trimmed name or the empty string if z is longer than s.
// The trimmed name will be returned without a trailing dot.
// s and z must be syntactically valid domain names, see [IsName] and [IsFqdn].
func Trim(s, z string) string {
	i, overshot := Prev(s, Labels(z))
	if overshot || i-1 < 0 {
		return ""
	}
	// This includes the '.', remove on return.
	return s[:i-1]
}

// IsBelow checks if child sits below parent in the DNS tree, i.e. check if the child is a sub-domain of
// parent. If child and parent are at the same level, true is returned as well.
func IsBelow(parent, child string) bool { return Common(parent, child) == Labels(parent) }

// Randomize randomizes the string s, it randomly upper or lower cases each letter. If a letter is already
// uppercase it is downcased.
func Randomize(s string) string {
	b := []byte(s)
	r := [1]byte{}
	for i := range b {
		rand.Read(r[:])
		if r[0]%2 == 0 {
			if b[i] >= 'A' && b[i] <= 'Z' {
				b[i] += 0x20
				continue
			}
			if b[i] >= 'a' && b[i] <= 'z' {
				b[i] -= 0x20
			}
		}
	}
	return string(b)
}
