github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/ekastr/conversions.go (about) 1 // The MIT License (MIT) 2 // 3 // Copyright (c) 2015-present Aliaksandr Valialkin, VertaMedia, Kirill Danshin, 4 // Erik Dubbelboer, FastHTTP Authors 5 // 6 // Permission is hereby granted, free of charge, to any person obtaining 7 // a copy of this software and associated documentation files (the "Software"), 8 // to deal in the Software without restriction, 9 // including without limitation the rights to use, copy, modify, merge, publish, 10 // distribute, sublicense, and/or sell copies of the Software 11 // and to permit persons to whom the Software is furnished to do so, 12 // subject to the following conditions: 13 // The above copyright notice and this permission notice shall be included 14 // in all copies or substantial portions of the Software. 15 // 16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 20 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 22 // OR OTHER DEALINGS IN THE SOFTWARE. 23 24 package ekastr 25 26 // From: 27 // https://github.com/valyala/fasthttp/blob/master/bytesconv.go 28 29 import ( 30 "reflect" 31 "unsafe" 32 ) 33 34 /* 35 B2S converts byte slice to a string without memory allocation. 36 See https://groups.google.com/forum/#!msg/Golang-Nuts/ENgbUzYvCuU/90yGx7GUAgAJ . 37 38 Note it may break if string and/or slice header will change 39 in the future go versions. 40 */ 41 func B2S(b []byte) string { 42 /* #nosec G103 */ 43 return *(*string)(unsafe.Pointer(&b)) 44 } 45 46 /* 47 S2B converts string to a byte slice without memory allocation. 48 49 Note it may break if string and/or slice header will change 50 in the future go versions. 51 */ 52 func S2B(s string) (b []byte) { 53 /* #nosec G103 */ 54 bh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) 55 /* #nosec G103 */ 56 sh := *(*reflect.StringHeader)(unsafe.Pointer(&s)) 57 bh.Data = sh.Data 58 bh.Len = sh.Len 59 bh.Cap = sh.Len 60 return b 61 }