func (r *Range) adjust(ranger Ranger) error {
	count := ranger.Count()
	if r.From > count {
		return RequestedRangeNotSatisfiable(&ContentRange{Total: count})
	}
	r.To = uint64(math.Min(float64(r.To), float64(count-1)))
	return nil
}
/*
adjust will correct r to fall within the boundaries of ranger. If r does not
overlap the current extend of ranger, a RequestedRangeNotSatifiable error will
be returned.

Range entities are always adjusted before they are passed to Ranger.Range
implementer.
package rst
import (
	"errors"
	"fmt"
	"math"
	"net/http"
	"regexp"
	"sort"
	"strconv"
	"strings"
)
func addVary(header http.Header, value string) {
	value = http.CanonicalHeaderKey(value)
	if values, ok := header[http.CanonicalHeaderKey("Vary")]; ok {
		for _, v := range values {
			if v == value {
				return
			}
		}
	}
	header.Add("Vary", value)
}
type AcceptClause struct {
	Type, SubType string
	Q             float64
	Params        map[string]string
}
type Accept []AcceptClause
func (accept Accept) Len() int {
	return len(accept)
}
func (accept Accept) Less(i, j int) bool {
	ai, aj := accept[i], accept[j]
	if ai.Q > aj.Q {
		return true
	}
	if ai.Type != "*" && aj.Type == "*" {
		return true
	}
	if ai.SubType != "*" && aj.SubType == "*" {
		return true
	}
	return false
}
func (accept Accept) Swap(i, j int) {
	accept[i], accept[j] = accept[j], accept[i]
}
func ParseAccept(header string) Accept {
	accept := make(Accept, 0)
	for _, part := range strings.Split(header, ",") {
		part := strings.Trim(part, " ")
		a := AcceptClause{}
		a.Params = make(map[string]string)
		a.Q = 1.0
		mrp := strings.Split(part, ";")
		mediaRange := mrp[0]
		sp := strings.Split(mediaRange, "/")
		a.Type = strings.Trim(sp[0], " ")
		switch {
		case len(sp) == 1 && a.Type == "*":
			a.SubType = "*"
		case len(sp) == 2:
			a.SubType = strings.Trim(sp[1], " ")
		default:
			continue
		}
		if len(mrp) == 1 {
			accept = append(accept, a)
			continue
		}
		for _, param := range mrp[1:] {
			sp := strings.SplitN(param, "=", 2)
			if len(sp) != 2 {
				continue
			}
			token := strings.Trim(sp[0], " ")
			if token == "q" {
				a.Q, _ = strconv.ParseFloat(sp[1], 32)
			} else {
				a.Params[token] = strings.Trim(sp[1], " ")
			}
		}
		accept = append(accept, a)
	}
	sort.Sort(accept)
	return accept
}
func (accept Accept) Negotiate(alternatives ...string) (contentType string) {
	asp := make([][]string, 0, len(alternatives))
	for _, ctype := range alternatives {
		asp = append(asp, strings.SplitN(ctype, "/", 2))
	}
	for _, clause := range accept {
		for i, ctsp := range asp {
			if clause.Type == ctsp[0] && clause.SubType == ctsp[1] {
				contentType = alternatives[i]
				return
			}
			if clause.Type == ctsp[0] && clause.SubType == "*" {
				contentType = alternatives[i]
				return
			}
			if clause.Type == "*" && clause.SubType == "*" {
				contentType = alternatives[i]
				return
			}
		}
	}
	return
}
var (
	rangeRe = regexp.MustCompile("^(\\w+)=(\\d+)-(\\d+)?$")
)
type Range struct {
	Unit string
	From uint64
	To   uint64
}
func (r *Range) Len() uint64 {
	return r.To - r.From
}
func (r *Range) validate(ranger Ranger) error {
	for _, u := range ranger.Units() {
		if strings.EqualFold(r.Unit, u) {
			return nil
		}
	}
	return fmt.Errorf("unsupported range unit %s", r.Unit)
}