Documentation.

This commit is contained in:
Moriyoshi Koizumi 2011-04-03 19:09:14 +09:00
parent 64fe90aabb
commit d354f2e8c2
2 changed files with 34 additions and 1 deletions

View File

@ -7,6 +7,7 @@ import (
"reflect"
);
// Packs a given value and writes it into the specified writer.
func PackUint8(writer io.Writer, value uint8) (n int, err os.Error) {
if value < 128 {
return writer.Write([]byte { byte(value) })
@ -14,6 +15,7 @@ func PackUint8(writer io.Writer, value uint8) (n int, err os.Error) {
return writer.Write([]byte { 0xcc, byte(value) })
}
// Packs a given value and writes it into the specified writer.
func PackUint16(writer io.Writer, value uint16) (n int, err os.Error) {
if value < 128 {
return writer.Write([]byte { byte(value) })
@ -23,6 +25,7 @@ func PackUint16(writer io.Writer, value uint16) (n int, err os.Error) {
return writer.Write([]byte { 0xcd, byte(value >> 8), byte(value) })
}
// Packs a given value and writes it into the specified writer.
func PackUint32(writer io.Writer, value uint32) (n int, err os.Error) {
if value < 128 {
return writer.Write([]byte { byte(value) })
@ -34,6 +37,7 @@ func PackUint32(writer io.Writer, value uint32) (n int, err os.Error) {
return writer.Write([]byte { 0xce, byte(value >> 24), byte(value >> 16), byte(value >> 8), byte(value) })
}
// Packs a given value and writes it into the specified writer.
func PackUint64(writer io.Writer, value uint64) (n int, err os.Error) {
if value < 128 {
return writer.Write([]byte { byte(value) })
@ -48,6 +52,7 @@ func PackUint64(writer io.Writer, value uint64) (n int, err os.Error) {
}
func PackUint(writer io.Writer, value uint) (n int, err os.Error) {
// Packs a given value and writes it into the specified writer.
switch unsafe.Sizeof(value) {
case 4:
return PackUint32(writer, *(*uint32)(unsafe.Pointer(&value)))
@ -57,7 +62,7 @@ func PackUint(writer io.Writer, value uint) (n int, err os.Error) {
return 0, os.ENOENT // never get here
}
// Packs a given value and writes it into the specified writer.
func PackInt8(writer io.Writer, value int8) (n int, err os.Error) {
if value < -32 {
return writer.Write([]byte { 0xd0, byte(value) })
@ -65,6 +70,7 @@ func PackInt8(writer io.Writer, value int8) (n int, err os.Error) {
return writer.Write([]byte { byte(value) })
}
// Packs a given value and writes it into the specified writer.
func PackInt16(writer io.Writer, value int16) (n int, err os.Error) {
if value < -128 || value >= 128 {
return writer.Write([]byte { 0xd1, byte(uint16(value) >> 8), byte(value) })
@ -74,6 +80,7 @@ func PackInt16(writer io.Writer, value int16) (n int, err os.Error) {
return writer.Write([]byte { byte(value) })
}
// Packs a given value and writes it into the specified writer.
func PackInt32(writer io.Writer, value int32) (n int, err os.Error) {
if value < -32768 || value >= 32768 {
return writer.Write([]byte { 0xd2, byte(uint32(value) >> 24), byte(uint32(value) >> 16), byte(uint32(value) >> 8), byte(value) })
@ -87,6 +94,7 @@ func PackInt32(writer io.Writer, value int32) (n int, err os.Error) {
return writer.Write([]byte { byte(value) })
}
// Packs a given value and writes it into the specified writer.
func PackInt64(writer io.Writer, value int64) (n int, err os.Error) {
if value < -2147483648 || value >= 2147483648 {
return writer.Write([]byte { 0xd3, byte(uint64(value) >> 56), byte(uint64(value) >> 48), byte(uint64(value) >> 40), byte(uint64(value) >> 32), byte(uint64(value) >> 24), byte(uint64(value) >> 16), byte(uint64(value) >> 8), byte(value) })
@ -100,6 +108,7 @@ func PackInt64(writer io.Writer, value int64) (n int, err os.Error) {
return writer.Write([]byte { byte(value) })
}
// Packs a given value and writes it into the specified writer.
func PackInt(writer io.Writer, value int) (n int, err os.Error) {
switch unsafe.Sizeof(value) {
case 4:
@ -110,10 +119,12 @@ func PackInt(writer io.Writer, value int) (n int, err os.Error) {
return 0, os.ENOENT // never get here
}
// Packs a given value and writes it into the specified writer.
func PackNil(writer io.Writer) (n int, err os.Error) {
return writer.Write([]byte{ 0xc0 })
}
// Packs a given value and writes it into the specified writer.
func PackBool(writer io.Writer, value bool) (n int, err os.Error) {
var code byte;
if value {
@ -124,14 +135,17 @@ func PackBool(writer io.Writer, value bool) (n int, err os.Error) {
return writer.Write([]byte{ code })
}
// Packs a given value and writes it into the specified writer.
func PackFloat32(writer io.Writer, value float32) (n int, err os.Error) {
return PackUint32(writer, *(*uint32)(unsafe.Pointer(&value)))
}
// Packs a given value and writes it into the specified writer.
func PackFloat64(writer io.Writer, value float64) (n int, err os.Error) {
return PackUint64(writer, *(*uint64)(unsafe.Pointer(&value)))
}
// Packs a given value and writes it into the specified writer.
func PackBytes(writer io.Writer, value []byte) (n int, err os.Error) {
if len(value) < 32 {
n1, err := writer.Write([]byte { 0xa0 | uint8(len(value)) })
@ -150,6 +164,7 @@ func PackBytes(writer io.Writer, value []byte) (n int, err os.Error) {
return n1 + n2, err
}
// Packs a given value and writes it into the specified writer.
func PackUint16Array(writer io.Writer, value []uint16) (n int, err os.Error) {
if len(value) < 16 {
n, err := writer.Write([]byte { 0x90 | byte(len(value)) })
@ -179,6 +194,7 @@ func PackUint16Array(writer io.Writer, value []uint16) (n int, err os.Error) {
return n, nil
}
// Packs a given value and writes it into the specified writer.
func PackUint32Array(writer io.Writer, value []uint32) (n int, err os.Error) {
if len(value) < 16 {
n, err := writer.Write([]byte { 0x90 | byte(len(value)) })
@ -208,6 +224,7 @@ func PackUint32Array(writer io.Writer, value []uint32) (n int, err os.Error) {
return n, nil
}
// Packs a given value and writes it into the specified writer.
func PackUint64Array(writer io.Writer, value []uint64) (n int, err os.Error) {
if len(value) < 16 {
n, err := writer.Write([]byte { 0x90 | byte(len(value)) })
@ -237,6 +254,7 @@ func PackUint64Array(writer io.Writer, value []uint64) (n int, err os.Error) {
return n, nil
}
// Packs a given value and writes it into the specified writer.
func PackUintArray(writer io.Writer, value []uint) (n int, err os.Error) {
switch unsafe.Sizeof(0) {
case 4:
@ -247,6 +265,7 @@ func PackUintArray(writer io.Writer, value []uint) (n int, err os.Error) {
return 0, os.ENOENT // never get here
}
// Packs a given value and writes it into the specified writer.
func PackInt8Array(writer io.Writer, value []int8) (n int, err os.Error) {
if len(value) < 16 {
n, err := writer.Write([]byte { 0x90 | byte(len(value)) })
@ -276,6 +295,7 @@ func PackInt8Array(writer io.Writer, value []int8) (n int, err os.Error) {
return n, nil
}
// Packs a given value and writes it into the specified writer.
func PackInt16Array(writer io.Writer, value []int16) (n int, err os.Error) {
if len(value) < 16 {
n, err := writer.Write([]byte { 0x90 | byte(len(value)) })
@ -305,6 +325,7 @@ func PackInt16Array(writer io.Writer, value []int16) (n int, err os.Error) {
return n, nil
}
// Packs a given value and writes it into the specified writer.
func PackInt32Array(writer io.Writer, value []int32) (n int, err os.Error) {
if len(value) < 16 {
n, err := writer.Write([]byte { 0x90 | byte(len(value)) })
@ -334,6 +355,7 @@ func PackInt32Array(writer io.Writer, value []int32) (n int, err os.Error) {
return n, nil
}
// Packs a given value and writes it into the specified writer.
func PackInt64Array(writer io.Writer, value []int64) (n int, err os.Error) {
if len(value) < 16 {
n, err := writer.Write([]byte { 0x90 | byte(len(value)) })
@ -363,6 +385,7 @@ func PackInt64Array(writer io.Writer, value []int64) (n int, err os.Error) {
return n, nil
}
// Packs a given value and writes it into the specified writer.
func PackIntArray(writer io.Writer, value []int) (n int, err os.Error) {
switch unsafe.Sizeof(0) {
case 4:
@ -373,6 +396,7 @@ func PackIntArray(writer io.Writer, value []int) (n int, err os.Error) {
return 0, os.ENOENT // never get here
}
// Packs a given value and writes it into the specified writer.
func PackFloat32Array(writer io.Writer, value []float32) (n int, err os.Error) {
if len(value) < 16 {
n, err := writer.Write([]byte { 0x90 | byte(len(value)) })
@ -402,6 +426,7 @@ func PackFloat32Array(writer io.Writer, value []float32) (n int, err os.Error) {
return n, nil
}
// Packs a given value and writes it into the specified writer.
func PackFloat64Array(writer io.Writer, value []float64) (n int, err os.Error) {
if len(value) < 16 {
n, err := writer.Write([]byte { 0x90 | byte(len(value)) })
@ -431,6 +456,7 @@ func PackFloat64Array(writer io.Writer, value []float64) (n int, err os.Error) {
return n, nil
}
// Packs a given value and writes it into the specified writer.
func PackArray(writer io.Writer, value reflect.ArrayOrSliceValue) (n int, err os.Error) {
{
elemType, ok := value.Type().(reflect.ArrayOrSliceType).Elem().(*reflect.UintType)
@ -468,6 +494,7 @@ func PackArray(writer io.Writer, value reflect.ArrayOrSliceValue) (n int, err os
return n, nil
}
// Packs a given value and writes it into the specified writer.
func PackMap(writer io.Writer, value *reflect.MapValue) (n int, err os.Error) {
keys := value.Keys()
if value.Len() < 16 {
@ -507,6 +534,7 @@ func PackMap(writer io.Writer, value *reflect.MapValue) (n int, err os.Error) {
return n, nil
}
// Packs a given value and writes it into the specified writer.
func PackValue(writer io.Writer, value reflect.Value) (n int, err os.Error) {
if value == nil || value.Type() == nil { return PackNil(writer) }
switch _value := value.(type) {
@ -527,6 +555,7 @@ func PackValue(writer io.Writer, value reflect.Value) (n int, err os.Error) {
panic("unsupported type: " + value.Type().String())
}
// Packs a given value and writes it into the specified writer.
func Pack(writer io.Writer, value interface{}) (n int, err os.Error) {
if value == nil { return PackNil(writer) }
switch _value := value.(type) {

View File

@ -272,10 +272,14 @@ func unpack(reader io.Reader, reflected bool) (v reflect.Value, n int, err os.Er
return retval, nbytesread, nil
}
// Reads a value from the reader, unpack and returns it.
func Unpack(reader io.Reader) (v reflect.Value, n int, err os.Error) {
return unpack(reader, false)
}
// Reads unpack a value from the reader, unpack and returns it. When the
// value is an array or map, leaves the elements wrapped by corresponding
// wrapper objects defined in reflect package.
func UnpackReflected(reader io.Reader) (v reflect.Value, n int, err os.Error) {
return unpack(reader, true)
}