utils.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package config
  2. import (
  3. "math"
  4. "regexp"
  5. )
  6. func anyRegexMatch(f string, res []*regexp.Regexp) bool {
  7. for _, re := range res {
  8. if regexMatched(f, re) {
  9. return true
  10. }
  11. }
  12. return false
  13. }
  14. func regexMatched(f string, re *regexp.Regexp) bool {
  15. if re == nil {
  16. return false
  17. }
  18. if re.FindString(f) != "" {
  19. return true
  20. }
  21. return false
  22. }
  23. func containsDigit(s string) bool {
  24. for _, c := range s {
  25. switch c {
  26. case '1', '2', '3', '4', '5', '6', '7', '8', '9':
  27. return true
  28. }
  29. }
  30. return false
  31. }
  32. // shannonEntropy calculates the entropy of data using the formula defined here:
  33. // https://en.wiktionary.org/wiki/Shannon_entropy
  34. // Another way to think about what this is doing is calculating the number of bits
  35. // needed to on average encode the data. So, the higher the entropy, the more random the data, the
  36. // more bits needed to encode that data.
  37. func shannonEntropy(data string) (entropy float64) {
  38. if data == "" {
  39. return 0
  40. }
  41. charCounts := make(map[rune]int)
  42. for _, char := range data {
  43. charCounts[char]++
  44. }
  45. invLength := 1.0 / float64(len(data))
  46. for _, count := range charCounts {
  47. freq := float64(count) * invLength
  48. entropy -= freq * math.Log2(freq)
  49. }
  50. return entropy
  51. }