go.mway.dev/x@v0.0.0-20240520034138-950aede9a3fb/unsafe/strings.go (about) 1 // Copyright (c) 2022 Matt Way 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to 5 // deal in the Software without restriction, including without limitation the 6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 // sell copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 // IN THE THE SOFTWARE. 20 21 package unsafe 22 23 import ( 24 "math" 25 "unsafe" 26 ) 27 28 // StringToBytes returns the underlying byte storage of x. The returned storage 29 // must not be modified: in the best case, it will cause undefined behavior; in 30 // the worst case, the program will segfault. 31 // 32 // The lifetime of the resulting storage is inextricable from that of x: they 33 // are both guaranteed to live for as long as there is a live reference to 34 // either. 35 // 36 // Do not use this function lightly, and never leak the returned storage 37 // outside of the lifetime of the caller. 38 func StringToBytes(x string) []byte { 39 if len(x) == 0 { 40 return nil 41 } 42 43 const max = math.MaxInt32 - math.MaxInt16 44 if len(x) > max { 45 return []byte(x) 46 } 47 48 return (*[max]byte)((*stringHeader)(unsafe.Pointer(&x)).Data)[:len(x):len(x)] 49 } 50 51 // BytesToString returns a string that uses x as its underlying storage 52 // verbatim. The provided bytes must not be modified: in the best case, it will 53 // create subtle bugs; in the worst case, the program will segfault. 54 // 55 // The lifetime of the resulting string is inextricable from that of x: they 56 // are both guaranteed to live for as long as there is a live reference to 57 // either. 58 // 59 // Do not use this function lightly, and never leak the returned string outside 60 // of the lifetime of the caller. 61 func BytesToString(x []byte) string { 62 if len(x) == 0 { 63 return "" 64 } 65 66 var ( 67 slice = (*sliceHeader)(unsafe.Pointer(&x)) 68 str = stringHeader{ 69 Data: slice.Data, 70 Len: slice.Len, 71 } 72 ) 73 74 return *(*string)(unsafe.Pointer(&str)) 75 } 76 77 type sliceHeader struct { 78 Data unsafe.Pointer 79 Len int 80 Cap int 81 } 82 83 type stringHeader struct { 84 Data unsafe.Pointer 85 Len int 86 }