package character

import (
	"math/rand"
	"regexp"
	"strings"
	"time"
)

func RandStringRunes(n int) string {
	var letterRunes = []rune("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")

	rand.Seed(time.Now().UnixNano())
	b := make([]rune, n)
	for i := range b {
		b[i] = letterRunes[rand.Intn(len(letterRunes))]
	}
	return string(b)
}

// SubstrAfter 截取指定字符后的内容
func SubstrAfter(str string, symbol string) string {
	comma := strings.Index(str, symbol)
	if comma < 0 { // -1 不存在
		return ""
	}
	pos := strings.Index(str[comma:], symbol)
	if comma+pos+1 > len(str) {
		return ""
	}
	return str[comma+pos+1:]
}

// IsLetterNumber 是否是数字和字母
func IsLetterNumber(str string) bool {
	reg, _ := regexp.Compile("[^a-zA-Z0-9]+")
	p := reg.ReplaceAllString(str, "")
	return p == str
}

// InSlice 元素是否存在于切片中
func InSlice[T int | int32 | int64 | string](slice []T, key T) bool {
	if len(slice) == 0 {
		return false
	}
	for i := 0; i < len(slice); i++ {
		if slice[i] == key {
			return true
		}
	}
	return false
}
