utils.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2025 BackendServerTemplate Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package resource
  5. import (
  6. "regexp"
  7. "strings"
  8. )
  9. const semVerRegexStr = `^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*)?(\+[0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*)?$`
  10. var semVerRegex = regexp.MustCompile(semVerRegexStr)
  11. // IsSemanticVersion checks if the given string is a valid semantic version.
  12. func utilsIsSemanticVersion(version string) bool {
  13. return semVerRegex.MatchString(version)
  14. }
  15. func utilsClenFileData(data string) (res string) {
  16. res = utilsCheckAndRemoveBOM(data)
  17. res = strings.Replace(res, "\r", "", -1)
  18. res = strings.Split(res, "\n")[0]
  19. res = strings.TrimSpace(res)
  20. return res
  21. }
  22. func utilsClenFileDataMoreLine(data string) (res string) {
  23. res = utilsCheckAndRemoveBOM(data)
  24. res = strings.Replace(res, "\r", "", -1)
  25. res = strings.TrimRight(res, "\n")
  26. res = strings.TrimSpace(res)
  27. return res
  28. }
  29. func utilsCheckAndRemoveBOM(s string) string {
  30. // UTF-8 BOM 的字节序列为 0xEF, 0xBB, 0xBF
  31. bom := []byte{0xEF, 0xBB, 0xBF}
  32. // 将字符串转换为字节切片
  33. bytes := []byte(s)
  34. // 检查前三个字节是否是 BOM
  35. if len(bytes) >= 3 && bytes[0] == bom[0] && bytes[1] == bom[1] && bytes[2] == bom[2] {
  36. // 如果存在 BOM,则删除它
  37. return string(bytes[3:])
  38. }
  39. // 如果不存在 BOM,则返回原始字符串
  40. return s
  41. }