// Variant returns the variant of this UUID. This package will only generate
// UUIDs in the IETF variant.
340519
func (u UUID) Variant() int {
	x := u[8]
	if x&0x80 == 0 {
		return VariantNCSCompat
	}
	if x&0x40 == 0 {
		return VariantIETF
	}
	if x&0x20 == 0 {
		return VariantMicrosoft
	}
	return VariantFuture
}
840062
func SomeUUID(u ble.UUID) Field {
	return func(p *Packet) error {
		if u.Len() == 2 {
			return p.append(someUUID16, u)
		}
		if u.Len() == 4 {
			return p.append(someUUID32, u)
		}
		return p.append(someUUID128, u)
	}
}
337969
func NewV4() (UUID, error) {
	u := UUID{}
	if _, err := randomReader.Read(u[:]); err != nil {
		return Nil, err
	}
	u.setVersion(V4)
	u.setVariant(VariantRFC4122)

	return u, nil
}
613633
func (vd *VariantDictionary) Get(key string) *VariantDictionaryItem {
	for _, item := range vd.Items {
		if string(item.Name) == key {
			return item
		}
	}
	return nil
}
43606
func UUID() (string, error) {
	u := make([]byte, 16)
	_, err := rand.Read(u)
	if err != nil {
		return "", err
	}

	u[6] = (u[6] & 0x0F) | 0x40 // Version 4
	u[8] = (u[8] & 0x3F) | 0x80 // Variant is 10

	uuid := fmt.Sprintf("{%08x-%04x-%04x-%04x-%012x}",
		u[:4], u[4:6], u[6:8], u[8:10], u[10:])

	return uuid, nil
}
238993
func readVariant(r io.Reader, numParams int32) (*Variant, error) {
	name, err := readPstring(r)
	if err != nil {
		return nil, err
	}
	paramValues := make([]float32, numParams)
	for i := 0; int32(i) < numParams; i++ {
		if err := binary.Read(r, byteOrder, &paramValues[i]); err != nil {
			return nil, err
		}
	}
	v := Variant{name.String(), paramValues}
	return &v, nil
}
1005172
func SetVersion(uuid []byte, version Version) {
	switch version {
	case VersionTimeBased:
		uuid[6] = (uuid[6] | 0x10) & 0x1f
	case VersionDCESecurity:
		uuid[6] = (uuid[6] | 0x20) & 0x2f
	case VersionNameBasedMD5:
		uuid[6] = (uuid[6] | 0x30) & 0x3f
	case VersionRandom:
		uuid[6] = (uuid[6] | 0x40) & 0x4f
	case VersionNameBasedSHA1:
		uuid[6] = (uuid[6] | 0x50) & 0x5f
	default:
		panic("version is unknown")
	}
}
759686
func (s *PutTemplateService) Version(version int) *PutTemplateService {
	s.version = &version
	return s
}
930143
func (s *ProductionVariantSummary) SetVariantName(v string) *ProductionVariantSummary {
	s.VariantName = &v
	return s
}
183868
func (this *Response) Vary(field string) {
	vary := this.Get("vary")
	if len(vary) == 0 {
		this.Set("Vary", field)
		return
	}
	list := regexp.MustCompile(" *, *").Split(vary, -1)
	for _, v := range list {
		if v == field {
			return
		}
	}
	this.Set("Vary", vary+", "+field)
}
21929
func generateUUID(t bool) string {
	u := uuid.New()
	if !t {
		return strings.Replace(u, "-", "", -1)
	}
	return u
}
1337603
func Variant3(namespace UUID, name string) (u UUID) {
	h := md5.New()
	h.Write([]byte(namespace))
	h.Write([]byte(name))
	u = h.Sum(nil)[0:16]
	u[6] = (u[6] & 0x0f) | 0x30
	u[8] = (u[8] & 0x1f) | 0x80
	return u
}
466447
func uuidgen() string {
	b := uuidgenBytes()
	return fmt.Sprintf("%x-%x-%x-%x-%x",
		b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
}
53338
func (v Variant) format() (string, bool) {
	switch v.sig.str[0] {
	case 'b', 'i':
		return fmt.Sprint(v.value), true
	case 'n', 'q', 'u', 'x', 't', 'd', 'h':
		return fmt.Sprint(v.value), false
	case 's':
		return strconv.Quote(v.value.(string)), true
	case 'o':
		return strconv.Quote(string(v.value.(ObjectPath))), false
	case 'g':
		return strconv.Quote(v.value.(Signature).str), false
	case 'v':
		s, unamb := v.value.(Variant).format()
		if !unamb {
			return "<@" + v.value.(Variant).sig.str + " " + s + ">", true
		}
		return "<" + s + ">", true
	case 'y':
		return fmt.Sprintf("%#x", v.value.(byte)), false
	}
	rv := reflect.ValueOf(v.value)
	switch rv.Kind() {
	case reflect.Slice:
		if rv.Len() == 0 {
			return "[]", false
		}
		unamb := true
		buf := bytes.NewBuffer([]byte("["))
		for i := 0; i < rv.Len(); i++ {
			// TODO: slooow
			s, b := MakeVariant(rv.Index(i).Interface()).format()
			unamb = unamb && b
			buf.WriteString(s)
			if i != rv.Len()-1 {
				buf.WriteString(", ")
			}
		}
		buf.WriteByte(']')
		return buf.String(), unamb
	case reflect.Map:
		if rv.Len() == 0 {
			return "{}", false
		}
		unamb := true
		var buf bytes.Buffer
		kvs := make([]string, rv.Len())
		for i, k := range rv.MapKeys() {
			s, b := MakeVariant(k.Interface()).format()
			unamb = unamb && b
			buf.Reset()
			buf.WriteString(s)
			buf.WriteString(": ")
			s, b = MakeVariant(rv.MapIndex(k).Interface()).format()
			unamb = unamb && b
			buf.WriteString(s)
			kvs[i] = buf.String()
		}
		buf.Reset()
		buf.WriteByte('{')
		sort.Strings(kvs)
		for i, kv := range kvs {
			if i > 0 {
				buf.WriteString(", ")
			}
			buf.WriteString(kv)
		}
		buf.WriteByte('}')
		return buf.String(), unamb
	}
	return `"INVALID"`, true
}
1325614
func (u *UUID) SetVariant(v byte) {
	switch v {
	case VariantNCS:
		u[8] = (u[8]&(0xff>>1) | (0x00 << 7))
	case VariantRFC4122:
		u[8] = (u[8]&(0xff>>2) | (0x02 << 6))
	case VariantMicrosoft:
		u[8] = (u[8]&(0xff>>3) | (0x06 << 5))
	case VariantFuture:
		fallthrough
	default:
		u[8] = (u[8]&(0xff>>3) | (0x07 << 5))
	}
}
209860
func readSynthdefVariants(r io.Reader, numParams int) ([]*Variant, error) {
	// read number of variants
	var numVariants int16
	if err := binary.Read(r, byteOrder, &numVariants); err != nil {
		return nil, err
	}
	// read variants
	variants := make([]*Variant, numVariants)
	for i := 0; int16(i) < numVariants; i++ {
		v, err := readVariant(r, int32(numParams))
		if err != nil {
			return nil, err
		}
		variants[i] = v
	}
	return variants, nil
}
364955
func (u UUID) Compare(o UUID) int {
	return bytes.Compare(u, o)
}