github.com/XiaoMi/Gaea@v1.2.5/util/hack/hack.go (about) 1 // Copyright 2016 The kingshard Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 // not use this file except in compliance with the License. You may obtain 5 // a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations 13 // under the License. 14 15 package hack 16 17 import ( 18 "reflect" 19 "strconv" 20 "strings" 21 "unsafe" 22 ) 23 24 // String no copy to change slice to string 25 // use your own risk 26 func String(b []byte) (s string) { 27 pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b)) 28 pstring := (*reflect.StringHeader)(unsafe.Pointer(&s)) 29 pstring.Data = pbytes.Data 30 pstring.Len = pbytes.Len 31 return 32 } 33 34 // Slice no copy to change string to slice 35 // use your own risk 36 func Slice(s string) (b []byte) { 37 pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b)) 38 pstring := (*reflect.StringHeader)(unsafe.Pointer(&s)) 39 pbytes.Data = pstring.Data 40 pbytes.Len = pstring.Len 41 pbytes.Cap = pstring.Len 42 return 43 } 44 45 // IsSQLSep check if sql seperation 46 func IsSQLSep(r rune) bool { 47 return r == ' ' || r == ',' || 48 r == '\t' || r == '/' || 49 r == '\n' || r == '\r' 50 } 51 52 // ArrayToString translate array to string 53 func ArrayToString(array []int) string { 54 if len(array) == 0 { 55 return "" 56 } 57 var strArray []string 58 for _, v := range array { 59 strArray = append(strArray, strconv.FormatInt(int64(v), 10)) 60 } 61 62 return strings.Join(strArray, ", ") 63 } 64 65 // Abs int64 abs 66 func Abs(n int64) int64 { 67 y := n >> 63 68 return (n ^ y) - y 69 }