equal
deleted
inserted
replaced
27 |
27 |
28 import ( |
28 import ( |
29 "bytes" |
29 "bytes" |
30 "strings" |
30 "strings" |
31 "unsafe" |
31 "unsafe" |
32 |
|
33 "golang.org/x/sys/internal/unsafeheader" |
|
34 ) |
32 ) |
35 |
33 |
36 // ByteSliceFromString returns a NUL-terminated slice of bytes |
34 // ByteSliceFromString returns a NUL-terminated slice of bytes |
37 // containing the text of s. If s contains a NUL byte at any |
35 // containing the text of s. If s contains a NUL byte at any |
38 // location, it returns (nil, EINVAL). |
36 // location, it returns (nil, EINVAL). |
80 n := 0 |
78 n := 0 |
81 for ptr := unsafe.Pointer(p); *(*byte)(ptr) != 0; n++ { |
79 for ptr := unsafe.Pointer(p); *(*byte)(ptr) != 0; n++ { |
82 ptr = unsafe.Pointer(uintptr(ptr) + 1) |
80 ptr = unsafe.Pointer(uintptr(ptr) + 1) |
83 } |
81 } |
84 |
82 |
85 var s []byte |
83 return string(unsafe.Slice(p, n)) |
86 h := (*unsafeheader.Slice)(unsafe.Pointer(&s)) |
|
87 h.Data = unsafe.Pointer(p) |
|
88 h.Len = n |
|
89 h.Cap = n |
|
90 |
|
91 return string(s) |
|
92 } |
84 } |
93 |
85 |
94 // Single-word zero for use when we need a valid pointer to 0 bytes. |
86 // Single-word zero for use when we need a valid pointer to 0 bytes. |
95 var _zero uintptr |
87 var _zero uintptr |