gopkg.in/alecthomas/gometalinter.v3@v3.0.0/_linters/src/github.com/client9/misspell/case.go (about) 1 package misspell 2 3 import ( 4 "strings" 5 ) 6 7 // WordCase is an enum of various word casing styles 8 type WordCase int 9 10 // Various WordCase types.. likely to be not correct 11 const ( 12 CaseUnknown WordCase = iota 13 CaseLower 14 CaseUpper 15 CaseTitle 16 ) 17 18 // CaseStyle returns what case style a word is in 19 func CaseStyle(word string) WordCase { 20 upperCount := 0 21 lowerCount := 0 22 23 // this iterates over RUNES not BYTES 24 for i := 0; i < len(word); i++ { 25 ch := word[i] 26 switch { 27 case ch >= 'a' && ch <= 'z': 28 lowerCount++ 29 case ch >= 'A' && ch <= 'Z': 30 upperCount++ 31 } 32 } 33 34 switch { 35 case upperCount != 0 && lowerCount == 0: 36 return CaseUpper 37 case upperCount == 0 && lowerCount != 0: 38 return CaseLower 39 case upperCount == 1 && lowerCount > 0 && word[0] >= 'A' && word[0] <= 'Z': 40 return CaseTitle 41 } 42 return CaseUnknown 43 } 44 45 // CaseVariations returns 46 // If AllUpper or First-Letter-Only is upcased: add the all upper case version 47 // If AllLower, add the original, the title and upcase forms 48 // If Mixed, return the original, and the all upcase form 49 // 50 func CaseVariations(word string, style WordCase) []string { 51 switch style { 52 case CaseLower: 53 return []string{word, strings.ToUpper(word[0:1]) + word[1:], strings.ToUpper(word)} 54 case CaseUpper: 55 return []string{strings.ToUpper(word)} 56 default: 57 return []string{word, strings.ToUpper(word)} 58 } 59 }