ident.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package flect
  2. import (
  3. "encoding"
  4. "strings"
  5. "unicode"
  6. "unicode/utf8"
  7. )
  8. // Ident represents the string and it's parts
  9. type Ident struct {
  10. Original string
  11. Parts []string
  12. }
  13. // String implements fmt.Stringer and returns the original string
  14. func (i Ident) String() string {
  15. return i.Original
  16. }
  17. // New creates a new Ident from the string
  18. func New(s string) Ident {
  19. i := Ident{
  20. Original: s,
  21. Parts: toParts(s),
  22. }
  23. return i
  24. }
  25. func toParts(s string) []string {
  26. parts := []string{}
  27. s = strings.TrimSpace(s)
  28. if len(s) == 0 {
  29. return parts
  30. }
  31. if _, ok := baseAcronyms[strings.ToUpper(s)]; ok {
  32. return []string{strings.ToUpper(s)}
  33. }
  34. var prev rune
  35. var x strings.Builder
  36. x.Grow(len(s))
  37. for _, c := range s {
  38. // fmt.Println("### cs ->", cs)
  39. // fmt.Println("### unicode.IsControl(c) ->", unicode.IsControl(c))
  40. // fmt.Println("### unicode.IsDigit(c) ->", unicode.IsDigit(c))
  41. // fmt.Println("### unicode.IsGraphic(c) ->", unicode.IsGraphic(c))
  42. // fmt.Println("### unicode.IsLetter(c) ->", unicode.IsLetter(c))
  43. // fmt.Println("### unicode.IsLower(c) ->", unicode.IsLower(c))
  44. // fmt.Println("### unicode.IsMark(c) ->", unicode.IsMark(c))
  45. // fmt.Println("### unicode.IsPrint(c) ->", unicode.IsPrint(c))
  46. // fmt.Println("### unicode.IsPunct(c) ->", unicode.IsPunct(c))
  47. // fmt.Println("### unicode.IsSpace(c) ->", unicode.IsSpace(c))
  48. // fmt.Println("### unicode.IsTitle(c) ->", unicode.IsTitle(c))
  49. // fmt.Println("### unicode.IsUpper(c) ->", unicode.IsUpper(c))
  50. if !utf8.ValidRune(c) {
  51. continue
  52. }
  53. if isSpace(c) {
  54. parts = xappend(parts, x.String())
  55. x.Reset()
  56. x.WriteRune(c)
  57. prev = c
  58. continue
  59. }
  60. if unicode.IsUpper(c) && !unicode.IsUpper(prev) {
  61. parts = xappend(parts, x.String())
  62. x.Reset()
  63. x.WriteRune(c)
  64. prev = c
  65. continue
  66. }
  67. if unicode.IsUpper(c) && baseAcronyms[strings.ToUpper(x.String())] {
  68. parts = xappend(parts, x.String())
  69. x.Reset()
  70. x.WriteRune(c)
  71. prev = c
  72. continue
  73. }
  74. if unicode.IsLetter(c) || unicode.IsDigit(c) || unicode.IsPunct(c) || c == '`' {
  75. prev = c
  76. x.WriteRune(c)
  77. continue
  78. }
  79. parts = xappend(parts, x.String())
  80. x.Reset()
  81. prev = c
  82. }
  83. parts = xappend(parts, x.String())
  84. return parts
  85. }
  86. var _ encoding.TextUnmarshaler = &Ident{}
  87. var _ encoding.TextMarshaler = &Ident{}
  88. // LastPart returns the last part/word of the original string
  89. func (i *Ident) LastPart() string {
  90. if len(i.Parts) == 0 {
  91. return ""
  92. }
  93. return i.Parts[len(i.Parts)-1]
  94. }
  95. // ReplaceSuffix creates a new Ident with the original suffix replaced by new
  96. func (i Ident) ReplaceSuffix(orig, new string) Ident {
  97. return New(strings.TrimSuffix(i.Original, orig) + new)
  98. }
  99. //UnmarshalText unmarshalls byte array into the Ident
  100. func (i *Ident) UnmarshalText(data []byte) error {
  101. (*i) = New(string(data))
  102. return nil
  103. }
  104. //MarshalText marshals Ident into byte array
  105. func (i Ident) MarshalText() ([]byte, error) {
  106. return []byte(i.Original), nil
  107. }