// parseUUID converts a string UUID in standard form to a byte array.
184578
func parseUUID(src string) (dst [16]byte, err error) {
	if len(src) < 36 {
		return dst, errors.Errorf("cannot parse UUID %v", src)
	}
	src = src[0:8] + src[9:13] + src[14:18] + src[19:23] + src[24:]
	buf, err := hex.DecodeString(src)
	if err != nil {
		return dst, err
	}

	copy(dst[:], buf)
	return dst, err
}
238756
func GenerateUUID() string {
	buf := make([]byte, 16)
	if _, err := rand.Read(buf); err != nil {
		panic(fmt.Errorf("failed to read random bytes: %v", err))
	}

	return fmt.Sprintf("%08x-%04x-%04x-%04x-%12x",
		buf[0:4],
		buf[4:6],
		buf[6:8],
		buf[8:10],
		buf[10:16])
}
212577
func uuid() []byte {
	uuid := make([]byte, 16)
	_, err := rand.Read(uuid)
	if err != nil {
		panic("cue/hosted: uuid() failed to read random bytes")
	}

	// The following bit twiddling is outlined in RFC 4122.  In short, it
	// identifies the UUID as a v4 random UUID.
	uuid[6] = (4 << 4) | (0xf & uuid[6])
	uuid[8] = (8 << 4) | (0x3f & uuid[8])
	return uuid
}
1188282
func formatVirtualDiskUUID(uuid string) string {
	uuidwithNoSpace := strings.Replace(uuid, " ", "", -1)
	uuidWithNoHypens := strings.Replace(uuidwithNoSpace, "-", "", -1)
	return strings.ToLower(uuidWithNoHypens)
}
327247
func (o Immutable) UUID() UUID {
	id := UUID{}
	id.unmarshal(o.Bytes())
	return id
}
470460
func IsUUID(str string) bool {
	const uuidLen = 36
	if len(str) != uuidLen {
		return false
	}

	return validUUID.MatchString(str)
}
1046012
func parseUserId(id string) (name, comment, email string) {
	var n, c, e struct {
		start, end int
	}
	var state int

	for offset, rune := range id {
		switch state {
		case 0:
			// Entering name
			n.start = offset
			state = 1
			fallthrough
		case 1:
			// In name
			if rune == '(' {
				state = 2
				n.end = offset
			} else if rune == '<' {
				state = 5
				n.end = offset
			}
		case 2:
			// Entering comment
			c.start = offset
			state = 3
			fallthrough
		case 3:
			// In comment
			if rune == ')' {
				state = 4
				c.end = offset
			}
		case 4:
			// Between comment and email
			if rune == '<' {
				state = 5
			}
		case 5:
			// Entering email
			e.start = offset
			state = 6
			fallthrough
		case 6:
			// In email
			if rune == '>' {
				state = 7
				e.end = offset
			}
		default:
			// After email
		}
	}
	switch state {
	case 1:
		// ended in the name
		n.end = len(id)
	case 3:
		// ended in comment
		c.end = len(id)
	case 6:
		// ended in email
		e.end = len(id)
	}

	name = strings.TrimSpace(id[n.start:n.end])
	comment = strings.TrimSpace(id[c.start:c.end])
	email = strings.TrimSpace(id[e.start:e.end])
	return
}
1366315
func RandomUUID() (UUID, error) {
	var u UUID
	_, err := io.ReadFull(rand.Reader, u[:])
	if err != nil {
		return u, err
	}
	u[6] &= 0x0F // clear version
	u[6] |= 0x40 // set version to 4 (random uuid)
	u[8] &= 0x3F // clear variant
	u[8] |= 0x80 // set to IETF variant
	return u, 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
}
911263
func Parse(s string) UUID {
	gu, err := guuid.Parse(s)
	if err == nil {
		return gu[:]
	}
	return nil
}
1226937
func newUUID() (string, error) {
	uuid := make([]byte, 16)
	if _, err := io.ReadFull(rand.Reader, uuid); err != nil {
		return "", err
	}
	// variant bits; see section 4.1.1
	uuid[8] = uuid[8]&^0xc0 | 0x80
	// version 4 (pseudo-random); see section 4.1.3
	uuid[6] = uuid[6]&^0xf0 | 0x40
	return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil
}
589754
func UUID4() (u UUID) {
	// s := make([]byte, 16)
	// n, err := rand.Read(s)
	// if n != 16 || err != nil {
	// 	return nil
	// }

	for i, _ := range u {
		// u[i] = v
		u[i] = byte(rand.Int31n(256))
	}

	// Set the four most significant bits (bits 12 through 15) of the
	// time_hi_and_version field to the 4-bit version number from Section 4.1.3.
	u[6] = u[6]&0x0f | 0x40

	// Set the two most significant bits (bits 6 and 7) of the
	// clock_seq_hi_and_reserved to zero and one, respectively.
	u[8] = u[8]&0x3f | 0x80

	return u
}
360679
func parseDUID(p []byte) (DUID, error) {
	b := buffer.New(p)
	// DUID must have enough bytes to determine its type
	if b.Len() < 2 {
		return nil, io.ErrUnexpectedEOF
	}

	var d DUID
	switch DUIDType(b.Read16()) {
	case DUIDTypeLLT:
		d = new(DUIDLLT)
	case DUIDTypeEN:
		d = new(DUIDEN)
	case DUIDTypeLL:
		d = new(DUIDLL)
	case DUIDTypeUUID:
		d = new(DUIDUUID)
	default:
		return nil, errUnknownDUID
	}

	return d, d.UnmarshalBinary(p)
}
1175043
func NewHex(uuid string) UUID {
	o := UUID{}
	o.unmarshal(fromHex(uuid))
	return o
}
1240478
func (o *Object) UUID() uuid.UUID {
	switch {
	case o.l != nil:
		return o.l.UUID()
	case o.p != nil:
		return o.p.UUID()
	default:
		return o.n.UUID()
	}
}
1134352
func Parse(b []byte) (u *UUID, err error) {
	if len(b) != 16 {
		err = errors.New("Given slice is not valid UUID sequence")
		return
	}
	u = new(UUID)
	copy(u[:], b)
	return
}
845391
func GenerateUUID() (string, error) {
	buf, err := GenerateRandomBytes(uuidLen)
	if err != nil {
		return "", err
	}
	return FormatUUID(buf)
}