-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype.go
75 lines (66 loc) · 1.21 KB
/
type.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package endian
import (
"unsafe"
)
// Type represents an endianness.
//
// Example Usage
//
// var endianness endian.Type
//
// endianness = endian.NativeEndianness()
//
// switch endianness {
// case endian.Little():
// //@TODO
// case endian.Big():
// //@TODO
// case endian.Unknown():
// //@TODO
// default:
// //@TODO
// }
type Type struct {
value uint8
}
// None Endianness.
//
// I.e., a variable of type endian.Type has not been assigned a value yet.
func None() Type {
return Type{}
}
// Little Endian
func Little() Type {
return Type{
value: 'l',
}
}
// Big Endian
func Big() Type {
return Type{
value: 'B',
}
}
// Unknown Endianness
//
// I.e., a variable of type endian.Type has been set to a value that means that it is not known what the endianness is.
//
// This could be considered to do somthing similar to an error.
func Unknown() Type {
return Type{
value: '?',
}
}
// NativeEndianness will figure out the native endianness of the computer system it is run on, and return that.
func NativeEndianness() Type {
var i uint16 = 0x0100
ptr := unsafe.Pointer(&i)
switch {
case 0x01 == *(*uint8)(ptr):
return Big()
case 0x00 == *(*uint8)(ptr):
return Little()
default:
return Unknown()
}
}