소스 검색

Merge pull request #174 from cwandrews/OptimizeUUIDString

changed the String() method for UUID's
Christoph Hack 11 년 전
부모
커밋
f6fb3e6795
1개의 변경된 파일13개의 추가작업 그리고 2개의 파일을 삭제
  1. 13 2
      uuid.go

+ 13 - 2
uuid.go

@@ -146,8 +146,19 @@ func UUIDFromTime(aTime time.Time) UUID {
 // String returns the UUID in it's canonical form, a 32 digit hexadecimal
 // number in the form of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
 func (u UUID) String() string {
-	return fmt.Sprintf("%x-%x-%x-%x-%x",
-		u[0:4], u[4:6], u[6:8], u[8:10], u[10:16])
+	var offsets = [...]int{0, 2, 4, 6, 9, 11, 14, 16, 19, 21, 24, 26, 28, 30, 32, 34}
+	const hexString = "0123456789abcdef"
+	r := make([]byte, 36)
+	for i, b := range u {
+                r[offsets[i]] = hexString[b>>4]
+		r[offsets[i]+1] = hexString[b&0xF]
+	}
+	r[8] = '-'
+	r[13] = '-'
+	r[18] = '-'
+	r[23] = '-'
+	return string(r)
+
 }
 
 // Bytes returns the raw byte slice for this UUID. A UUID is always 128 bits