package byteutil
import (
"bytes"
"crypto/md5"
"crypto/rand"
"encoding/hex"
"fmt"
"strconv"
"time"
)
func Md5 (src any ) []byte {
bs := Md5Sum (src )
dst := make ([]byte , hex .EncodedLen (len (bs )))
hex .Encode (dst , bs )
return dst
}
func Md5Sum (src any ) []byte {
h := md5 .New ()
switch val := src .(type ) {
case []byte :
h .Write (val )
case string :
h .Write ([]byte (val ))
default :
h .Write ([]byte (fmt .Sprint (src )))
}
return h .Sum (nil )
}
func ShortMd5 (src any ) []byte { return Md5 (src )[8 :24 ] }
func Random (length int ) ([]byte , error ) {
b := make ([]byte , length )
if _ , err := rand .Read (b ); err != nil {
return nil , err
}
return b , nil
}
func FirstLine (bs []byte ) []byte {
if i := bytes .IndexByte (bs , '\n' ); i >= 0 {
return bs [0 :i ]
}
return bs
}
func AppendAny (dst []byte , v any ) []byte {
if v == nil {
return append (dst , "<nil>" ...)
}
switch val := v .(type ) {
case []byte :
dst = append (dst , val ...)
case string :
dst = append (dst , val ...)
case int :
dst = strconv .AppendInt (dst , int64 (val ), 10 )
case int8 :
dst = strconv .AppendInt (dst , int64 (val ), 10 )
case int16 :
dst = strconv .AppendInt (dst , int64 (val ), 10 )
case int32 :
dst = strconv .AppendInt (dst , int64 (val ), 10 )
case int64 :
dst = strconv .AppendInt (dst , val , 10 )
case uint :
dst = strconv .AppendUint (dst , uint64 (val ), 10 )
case uint8 :
dst = strconv .AppendUint (dst , uint64 (val ), 10 )
case uint16 :
dst = strconv .AppendUint (dst , uint64 (val ), 10 )
case uint32 :
dst = strconv .AppendUint (dst , uint64 (val ), 10 )
case uint64 :
dst = strconv .AppendUint (dst , val , 10 )
case float32 :
dst = strconv .AppendFloat (dst , float64 (val ), 'f' , -1 , 32 )
case float64 :
dst = strconv .AppendFloat (dst , val , 'f' , -1 , 64 )
case bool :
dst = strconv .AppendBool (dst , val )
case time .Time :
dst = val .AppendFormat (dst , time .RFC3339 )
case time .Duration :
dst = strconv .AppendInt (dst , int64 (val ), 10 )
case error :
dst = append (dst , val .Error()...)
case fmt .Stringer :
dst = append (dst , val .String ()...)
default :
dst = append (dst , fmt .Sprint (v )...)
}
return dst
}
func Cut (bs []byte , sep byte ) (before , after []byte , found bool ) {
return bytes .Cut (bs , []byte {sep })
}
func SafeCut (bs []byte , sep byte ) (before , after []byte ) {
before , after , _ = bytes .Cut (bs , []byte {sep })
return
}
func SafeCuts (bs []byte , sep []byte ) (before , after []byte ) {
before , after , _ = bytes .Cut (bs , sep )
return
}
The pages are generated with Golds v0.8.4 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .