utils.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 utilsClenFileDataMoreLine(data string) (res string) {
  16. res = utilsCheckAndRemoveBOM(data)
  17. res = strings.Replace(res, "\r", "", -1)
  18. return res
  19. }
  20. func utilsCheckAndRemoveBOM(s string) string {
  21. // UTF-8 BOM 的字节序列为 0xEF, 0xBB, 0xBF
  22. bom := []byte{0xEF, 0xBB, 0xBF}
  23. // 将字符串转换为字节切片
  24. bytes := []byte(s)
  25. // 检查前三个字节是否是 BOM
  26. if len(bytes) >= 3 && bytes[0] == bom[0] && bytes[1] == bom[1] && bytes[2] == bom[2] {
  27. // 如果存在 BOM,则删除它
  28. return string(bytes[3:])
  29. }
  30. // 如果不存在 BOM,则返回原始字符串
  31. return s
  32. }