package utils import ( "bytes" "encoding/base64" "encoding/gob" "math/rand" "reflect" "strings" "time" "unsafe" ) //THESE ARE FROM Go Authors var htmlReplacer = strings.NewReplacer( "&", "&", "<", "<", ">", ">", // """ is shorter than """. `"`, """, // "'" is shorter than "'" and apos was not in HTML until HTML5. "'", "'", ) // HtmlEscape returns a string which has no valid html code func HtmlEscape(s string) string { return htmlReplacer.Replace(s) } // FindLower returns the smaller number between a and b func FindLower(a, b int) int { if a <= b { return a } return b } // BytesToString accepts bytes and returns their string presentation // instead of string() this method doesn't generate memory allocations, // BUT it is not safe to use anywhere because it points // this helps on 0 memory allocations func BytesToString(b []byte) string { bh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) sh := reflect.StringHeader{bh.Data, bh.Len} return *(*string)(unsafe.Pointer(&sh)) } // StringToBytes accepts string and returns their []byte presentation // instead of byte() this method doesn't generate memory allocations, // BUT it is not safe to use anywhere because it points // this helps on 0 memory allocations func StringToBytes(s string) []byte { sh := (*reflect.StringHeader)(unsafe.Pointer(&s)) bh := reflect.SliceHeader{sh.Data, sh.Len, 0} return *(*[]byte)(unsafe.Pointer(&bh)) } // const ( letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" letterIdxBits = 6 // 6 bits to represent a letter index letterIdxMask = 1<= 0; { if remain == 0 { cache, remain = src.Int63(), letterIdxMax } if idx := int(cache & letterIdxMask); idx < len(letterBytes) { b[i] = letterBytes[idx] i-- } cache >>= letterIdxBits remain-- } return b } // RandomString accepts a number(10 for example) and returns a random string using simple but fairly safe random algorithm func RandomString(n int) string { return string(Random(n)) } // Serialize serialize any type to gob bytes and after returns its the base64 encoded string func Serialize(m interface{}) (string, error) { b := bytes.Buffer{} encoder := gob.NewEncoder(&b) err := encoder.Encode(m) if err != nil { return "", err } return base64.StdEncoding.EncodeToString(b.Bytes()), nil } // Deserialize accepts an encoded string and a data struct which will be filled with the desierialized string // using gob decoder func Deserialize(str string, m interface{}) error { by, err := base64.StdEncoding.DecodeString(str) if err != nil { return err } b := bytes.Buffer{} b.Write(by) d := gob.NewDecoder(&b) // d := gob.NewDecoder(bytes.NewBufferString(str)) err = d.Decode(&m) if err != nil { return err } return nil }