stringutils.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package stringutils
  15. import (
  16. "fmt"
  17. "regexp"
  18. "strings"
  19. "time"
  20. "github.com/golang-plus/uuid"
  21. "yunion.io/x/jsonutils"
  22. "yunion.io/x/pkg/util/timeutils"
  23. )
  24. func ParseNamePattern(name string) (string, string, int) {
  25. const RepChar = '#'
  26. var match string
  27. var pattern string
  28. var patternLen int
  29. start := strings.IndexByte(name, RepChar)
  30. if start >= 0 {
  31. end := start + 1
  32. for end < len(name) && name[end] == RepChar {
  33. end += 1
  34. }
  35. match = fmt.Sprintf("%s%%%s", name[:start], name[end:])
  36. pattern = fmt.Sprintf("%s%%0%dd%s", name[:start], end-start, name[end:])
  37. patternLen = end - start
  38. } else {
  39. match = fmt.Sprintf("%s-%%", name)
  40. pattern = fmt.Sprintf("%s-%%d", name)
  41. }
  42. return match, pattern, patternLen
  43. }
  44. func UUID4() string {
  45. uid, _ := uuid.NewV4()
  46. return uid.String()
  47. }
  48. func Interface2String(val interface{}) string {
  49. if val == nil {
  50. return ""
  51. }
  52. switch vval := val.(type) {
  53. case string:
  54. return vval
  55. case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
  56. return fmt.Sprintf("%d", vval)
  57. case float32, float64:
  58. return fmt.Sprintf("%f", vval)
  59. case bool:
  60. return fmt.Sprintf("%v", vval)
  61. case error:
  62. return vval.Error()
  63. case time.Time:
  64. return timeutils.FullIsoTime(vval)
  65. case fmt.Stringer:
  66. return vval.String()
  67. default:
  68. json := jsonutils.Marshal(val)
  69. return json.String()
  70. }
  71. }
  72. func SplitKeyValue(line string) (string, string) {
  73. return SplitKeyValueBySep(line, ":")
  74. }
  75. func SplitKeyValueBySep(line string, sep string) (string, string) {
  76. pos := strings.Index(line, sep)
  77. if pos > 0 {
  78. key := strings.TrimSpace(line[:pos])
  79. val := strings.TrimSpace(line[pos+1:])
  80. return key, val
  81. }
  82. return "", ""
  83. }
  84. func ContainsWord(str, w string) bool {
  85. reg := regexp.MustCompile(fmt.Sprintf("\\b%s\\b", w))
  86. return reg.MatchString(str)
  87. }
  88. func byte2hex(b byte) byte {
  89. if b >= 0 && b <= 9 {
  90. return b + 0x30
  91. }
  92. if b >= 10 && b <= 15 {
  93. return b - 10 + 0x61
  94. }
  95. return '?'
  96. }
  97. func Bytes2Str(b []byte) string {
  98. buf := strings.Builder{}
  99. for i := range b {
  100. buf.WriteByte(byte2hex((b[i] & 0xf0) >> 4))
  101. buf.WriteByte(byte2hex(b[i] & 0x0f))
  102. }
  103. return buf.String()
  104. }