You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
3.8 KiB

11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
  1. package binary
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "reflect"
  7. "time"
  8. )
  9. type Encoder func(o interface{}, w io.Writer, n *int64, err *error)
  10. type Decoder func(r io.Reader, n *int64, err *error) interface{}
  11. type Comparator func(o1 interface{}, o2 interface{}) int
  12. type Codec struct {
  13. Encode Encoder
  14. Decode Decoder
  15. Compare Comparator
  16. }
  17. const (
  18. typeByte = byte(0x01)
  19. typeInt8 = byte(0x02)
  20. // typeUint8 = byte(0x03)
  21. typeInt16 = byte(0x04)
  22. typeUint16 = byte(0x05)
  23. typeInt32 = byte(0x06)
  24. typeUint32 = byte(0x07)
  25. typeInt64 = byte(0x08)
  26. typeUint64 = byte(0x09)
  27. typeVarint = byte(0x0A)
  28. typeUvarint = byte(0x0B)
  29. typeString = byte(0x10)
  30. typeByteSlice = byte(0x11)
  31. typeTime = byte(0x20)
  32. )
  33. func BasicCodecEncoder(o interface{}, w io.Writer, n *int64, err *error) {
  34. switch o := o.(type) {
  35. case nil:
  36. panic("nil type unsupported")
  37. case byte:
  38. WriteByte(typeByte, w, n, err)
  39. WriteByte(o, w, n, err)
  40. case int8:
  41. WriteByte(typeInt8, w, n, err)
  42. WriteInt8(o, w, n, err)
  43. //case uint8:
  44. // WriteByte( typeUint8, w, n, err)
  45. // WriteUint8( o, w, n, err)
  46. case int16:
  47. WriteByte(typeInt16, w, n, err)
  48. WriteInt16(o, w, n, err)
  49. case uint16:
  50. WriteByte(typeUint16, w, n, err)
  51. WriteUint16(o, w, n, err)
  52. case int32:
  53. WriteByte(typeInt32, w, n, err)
  54. WriteInt32(o, w, n, err)
  55. case uint32:
  56. WriteByte(typeUint32, w, n, err)
  57. WriteUint32(o, w, n, err)
  58. case int64:
  59. WriteByte(typeInt64, w, n, err)
  60. WriteInt64(o, w, n, err)
  61. case uint64:
  62. WriteByte(typeUint64, w, n, err)
  63. WriteUint64(o, w, n, err)
  64. case int:
  65. WriteByte(typeVarint, w, n, err)
  66. WriteVarint(o, w, n, err)
  67. case uint:
  68. WriteByte(typeUvarint, w, n, err)
  69. WriteUvarint(o, w, n, err)
  70. case string:
  71. WriteByte(typeString, w, n, err)
  72. WriteString(o, w, n, err)
  73. case []byte:
  74. WriteByte(typeByteSlice, w, n, err)
  75. WriteByteSlice(o, w, n, err)
  76. case time.Time:
  77. WriteByte(typeTime, w, n, err)
  78. WriteTime(o, w, n, err)
  79. default:
  80. panic(fmt.Sprintf("Unsupported type: %v", reflect.TypeOf(o)))
  81. }
  82. }
  83. func BasicCodecDecoder(r io.Reader, n *int64, err *error) (o interface{}) {
  84. type_ := ReadByte(r, n, err)
  85. switch type_ {
  86. case typeByte:
  87. o = ReadByte(r, n, err)
  88. case typeInt8:
  89. o = ReadInt8(r, n, err)
  90. //case typeUint8:
  91. // o = ReadUint8(r, n, err)
  92. case typeInt16:
  93. o = ReadInt16(r, n, err)
  94. case typeUint16:
  95. o = ReadUint16(r, n, err)
  96. case typeInt32:
  97. o = ReadInt32(r, n, err)
  98. case typeUint32:
  99. o = ReadUint32(r, n, err)
  100. case typeInt64:
  101. o = ReadInt64(r, n, err)
  102. case typeUint64:
  103. o = ReadUint64(r, n, err)
  104. case typeVarint:
  105. o = ReadVarint(r, n, err)
  106. case typeUvarint:
  107. o = ReadUvarint(r, n, err)
  108. case typeString:
  109. o = ReadString(r, n, err)
  110. case typeByteSlice:
  111. o = ReadByteSlice(r, n, err)
  112. case typeTime:
  113. o = ReadTime(r, n, err)
  114. default:
  115. if *err != nil {
  116. panic(*err)
  117. } else {
  118. panic(fmt.Sprintf("Unsupported type byte: %X", type_))
  119. }
  120. }
  121. return o
  122. }
  123. func BasicCodecComparator(o1 interface{}, o2 interface{}) int {
  124. switch o1.(type) {
  125. case byte:
  126. return int(o1.(byte) - o2.(byte))
  127. case int8:
  128. return int(o1.(int8) - o2.(int8))
  129. //case uint8:
  130. case int16:
  131. return int(o1.(int16) - o2.(int16))
  132. case uint16:
  133. return int(o1.(uint16) - o2.(uint16))
  134. case int32:
  135. return int(o1.(int32) - o2.(int32))
  136. case uint32:
  137. return int(o1.(uint32) - o2.(uint32))
  138. case int64:
  139. return int(o1.(int64) - o2.(int64))
  140. case uint64:
  141. return int(o1.(uint64) - o2.(uint64))
  142. case int:
  143. return o1.(int) - o2.(int)
  144. case uint:
  145. return int(o1.(uint)) - int(o2.(uint))
  146. case string:
  147. return bytes.Compare([]byte(o1.(string)), []byte(o2.(string)))
  148. case []byte:
  149. return bytes.Compare(o1.([]byte), o2.([]byte))
  150. case time.Time:
  151. return int(o1.(time.Time).UnixNano() - o2.(time.Time).UnixNano())
  152. default:
  153. panic(fmt.Sprintf("Unsupported type: %v", reflect.TypeOf(o1)))
  154. }
  155. }
  156. var BasicCodec = Codec{
  157. Encode: BasicCodecEncoder,
  158. Decode: BasicCodecDecoder,
  159. Compare: BasicCodecComparator,
  160. }