gitee.com/wgliang/goreporter@v0.0.0-20180902115603-df1b20f7c5d0/linters/spellcheck/misspell/case.go (about) 1 package misspell 2 3 import ( 4 "strings" 5 "unicode" 6 ) 7 8 // WordCase is an enum of various word casing styles 9 type WordCase int 10 11 // Various WordCase types.. likely to be not correct 12 const ( 13 AllLower WordCase = iota 14 AllUpper 15 Title 16 Mixed 17 Camel 18 ) 19 20 // CaseStyle returns what case style a word is in 21 func CaseStyle(word string) WordCase { 22 hasTitle := false 23 upperCount := 0 24 lowerCount := 0 25 runeCount := 0 26 27 // this iterates over RUNES not BYTES 28 for _, r := range word { 29 // ASCII apostrophe doesn't count 30 // want words like "don't" to have 31 // upper case forms when adding to dictionary 32 if r == 0x0027 { 33 continue 34 } 35 runeCount++ 36 if unicode.IsLower(r) { 37 lowerCount++ 38 continue 39 } 40 if unicode.IsUpper(r) { 41 if runeCount == 1 { 42 hasTitle = true 43 } 44 upperCount++ 45 continue 46 } 47 48 //??? 49 } 50 51 switch { 52 case runeCount == lowerCount: 53 return AllLower 54 case runeCount == upperCount: 55 return AllUpper 56 case hasTitle && runeCount-1 == lowerCount: 57 return Title 58 default: 59 return Mixed 60 } 61 } 62 63 // CaseVariations returns 64 // If AllUpper or First-Letter-Only is upcased: add the all upper case version 65 // If AllLower, add the original, the title and upcase forms 66 // If Mixed, return the original, and the all upcase form 67 // 68 func CaseVariations(word string, style WordCase) []string { 69 switch style { 70 case AllLower: 71 return []string{word, strings.ToUpper(word[0:1]) + word[1:], strings.ToUpper(word)} 72 case AllUpper: 73 return []string{strings.ToUpper(word)} 74 default: 75 return []string{word, strings.ToUpper(word)} 76 } 77 }