wfc-server/common/strings.go
Sketch 27d72ff08b
Merge pull request #22 from MikeIsAStar/prevent-invalid-packets-from-stopping-server
NATNEG / SB: Prevent invalid packets from stopping the server
2023-12-08 21:39:53 -05:00

57 lines
1.0 KiB
Go

package common
import (
"bytes"
"errors"
"math/rand"
)
var letterRunes = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
func RandomString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
var hexRunes = []rune("0123456789abcdefabcdef")
func RandomHexString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(hexRunes))]
}
return string(b)
}
func GetString(buf []byte) (string, error) {
nullTerminator := bytes.IndexByte(buf, 0)
if nullTerminator == -1 {
return "", errors.New("buf is not null-terminated")
}
return string(buf[:nullTerminator]), nil
}
// IsUppercaseAlphanumeric checks if the given string is composed exclusively of uppercase alphanumeric characters.
func IsUppercaseAlphanumeric(str string) bool {
strLength := len(str)
if strLength == 0 {
return false
}
for i := 0; i < strLength; i++ {
c := str[i]
if (c < '0' || c > '9') && (c < 'A' || c > 'Z') {
return false
}
}
return true
}