Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions Makefile

This file was deleted.

27 changes: 4 additions & 23 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,17 @@ and 5 UUIDs as specified in RFC 4122.

Installation
------------
Use the `goinstall` tool:
Use the `go` tool:

$ goinstall github.com/nu7hatch/gouuid

... or install it manually:

$ git clone git://github.com/nu7hatch/gouuid.git
$ cd gouuid
$ make install
$ go get github.com/nu7hatch/gouuid

Usage
-----

package main

import uuid "github.com/nu7hatch/gouuid"

func main() {
// generating v4
u4, _ := uuid.NewV4()

// generating v5 (or v3...)
u5, _ := uuid.NewV5(uuid.NamespaceURL, "nu7hat.ch")

// parsing
u, _ := uuid.ParseHex("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
}
See http://godoc.org/github.com/nu7hatch/gouuid for documentation and examples.

Copyright
---------
Copyright (C) 2011 by Krzysztof Kowalik <chris@nu7hat.ch>

See COPYING file for details.
See COPYING file for details.
33 changes: 33 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package uuid_test

import (
"fmt"
"github.com/nu7hatch/gouuid"
)

func ExampleNewV4() {
u4, err := uuid.NewV4()
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(u4)
}

func ExampleNewV5() {
u5, err := uuid.NewV5(uuid.NamespaceURL, []byte("nu7hat.ch"))
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(u5)
}

func ExampleParseHex() {
u, err := uuid.ParseHex("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(u)
}
26 changes: 19 additions & 7 deletions uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,25 +133,37 @@ func (u *UUID) setBytesFromHash(hash hash.Hash, ns, name []byte) {
func (u *UUID) setVariant(v byte) {
switch v {
case ReservedNCS:
u[8] = (u[8] | ReservedNCS) & 0xBF
// unset bit 7

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not totally sure if I follow, the original implementation is compliant with the specs and RFC. Can you be more specific and show me what's wrong, which your patch is fixing (examples)?

u[8] &= ^byte(0x80)
case ReservedRFC4122:
u[8] = (u[8] | ReservedRFC4122) & 0x7F
// set bit 7
u[8] |= 0x80
// unset bit 6
u[8] &= ^byte(0x40)
case ReservedMicrosoft:
u[8] = (u[8] | ReservedMicrosoft) & 0x3F
// set bits 6 & 7
u[8] |= 0x80 | 0x40
// unset bit 5
u[8] &= ^byte(0x20)
}
}

// Variant returns the UUID Variant, which determines the internal
// layout of the UUID. This will be one of the constants: RESERVED_NCS,
// RFC_4122, RESERVED_MICROSOFT, RESERVED_FUTURE.
// See rfc4122 section 4.1.1: http://www.ietf.org/rfc/rfc4122.txt
func (u *UUID) Variant() byte {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes do exactly the same job, in a more unreadable way.

if u[8] & ReservedNCS == ReservedNCS {
if u[8] & 0x80 == 0 {
// 0 x x
return ReservedNCS
} else if u[8] & ReservedRFC4122 == ReservedRFC4122 {
} else if u[8] & 0x40 == 0 {
// 1 0 x
return ReservedRFC4122
} else if u[8] & ReservedMicrosoft == ReservedMicrosoft {
} else if u[8] & 0x20 == 0 {
// 1 1 x
return ReservedMicrosoft
}
}
// 1 1 1
return ReservedFuture
}

Expand Down
2 changes: 1 addition & 1 deletion uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"testing"
)

const format = "^[a-z0-9]{8}-[a-z0-9]{4}-[1-5][a-z0-9]{3}-[a-z0-9]{4}-[a-z0-9]{12}$"
const format = "^[[:xdigit:]]{8}-[[:xdigit:]]{4}-[1-5][[:xdigit:]]{3}-[89ab][[:xdigit:]]{3}-[[:xdigit:]]{12}$"

func TestParse(t *testing.T) {
_, err := Parse([]byte{1,2,3,4,5})
Expand Down