github.com/moontrade/nogc@v0.1.7/pointer_fat.go (about) 1 package nogc 2 3 import "unsafe" 4 5 type FatPointer struct { 6 Pointer 7 len uintptr 8 } 9 10 func (fp *FatPointer) Len() uintptr { 11 return fp.len 12 } 13 14 func FatPointerOfString(s string) FatPointer { 15 return PointerOfString(s).ToFat(len(s)) 16 } 17 18 func FatPointerOfStringRef(s *string) FatPointer { 19 if s == nil { 20 return FatPointer{} 21 } 22 return PointerOfString(*s).ToFat(len(*s)) 23 } 24 25 func FatPointerOfBytes(v []byte) FatPointer { 26 if v == nil { 27 return FatPointer{} 28 } 29 return FatPointer{ 30 Pointer: Pointer(unsafe.Pointer(&v[0])), 31 len: uintptr(len(v)), 32 } 33 } 34 35 func FatPointerOfBytesRef(v *[]byte) FatPointer { 36 if v == nil { 37 return FatPointer{} 38 } 39 vv := *v 40 if vv == nil { 41 return FatPointer{} 42 } 43 return FatPointer{ 44 Pointer: Pointer(unsafe.Pointer(&vv[0])), 45 len: uintptr(len(vv)), 46 } 47 } 48 49 func FatPointerOfBool(v *bool) FatPointer { 50 return FatPointer{ 51 Pointer: Pointer(unsafe.Pointer(v)), 52 len: 1, 53 } 54 } 55 56 func FatPointerOfInt8(v *int8) FatPointer { 57 return FatPointer{ 58 Pointer: Pointer(unsafe.Pointer(v)), 59 len: 1, 60 } 61 } 62 63 func FatPointerOfUInt8(v *uint8) FatPointer { 64 return FatPointer{ 65 Pointer: Pointer(unsafe.Pointer(v)), 66 len: 1, 67 } 68 } 69 70 func FatPointerOfInt16(v *int16) FatPointer { 71 return FatPointer{ 72 Pointer: Pointer(unsafe.Pointer(v)), 73 len: 2, 74 } 75 } 76 77 func FatPointerOfUInt16(v *uint16) FatPointer { 78 return FatPointer{ 79 Pointer: Pointer(unsafe.Pointer(v)), 80 len: 2, 81 } 82 } 83 84 func FatPointerOfInt32(v *int32) FatPointer { 85 return FatPointer{ 86 Pointer: Pointer(unsafe.Pointer(v)), 87 len: 4, 88 } 89 } 90 91 func FatPointerOfUInt32(v *uint32) FatPointer { 92 return FatPointer{ 93 Pointer: Pointer(unsafe.Pointer(v)), 94 len: 4, 95 } 96 } 97 98 func FatPointerOfInt64(v *int64) FatPointer { 99 return FatPointer{ 100 Pointer: Pointer(unsafe.Pointer(v)), 101 len: 8, 102 } 103 } 104 105 func FatPointerOfUInt64(v *uint64) FatPointer { 106 return FatPointer{ 107 Pointer: Pointer(unsafe.Pointer(v)), 108 len: 8, 109 } 110 } 111 112 func FatPointerOfFloat32(v *float32) FatPointer { 113 return FatPointer{ 114 Pointer: Pointer(unsafe.Pointer(v)), 115 len: 4, 116 } 117 } 118 119 func FatPointerOfFloat64(v *float64) FatPointer { 120 return FatPointer{ 121 Pointer: Pointer(unsafe.Pointer(v)), 122 len: 8, 123 } 124 } 125 126 func FatPointerOf(p Pointer, length uintptr) FatPointer { 127 return FatPointer{Pointer: p, len: length} 128 } 129 130 func (fp FatPointer) String() string { 131 return fp.Pointer.String(0, int(fp.len)) 132 } 133 134 func (fp FatPointer) Bytes() []byte { 135 return fp.Pointer.Bytes(0, int(fp.len), int(fp.len)) 136 } 137 138 func (fp FatPointer) Clone() FatPointer { 139 b := Alloc(fp.len) 140 Copy(b.Unsafe(), fp.Unsafe(), fp.len) 141 return FatPointer{Pointer: b, len: fp.len} 142 } 143 144 func (fp FatPointer) CloneAsBytes() Bytes { 145 b := AllocBytes(fp.len) 146 Copy(b.Unsafe(), fp.Unsafe(), fp.len) 147 b.setLen(int(fp.len)) 148 return b 149 }