File tree Expand file tree Collapse file tree 1 file changed +44
-2
lines changed
Expand file tree Collapse file tree 1 file changed +44
-2
lines changed Original file line number Diff line number Diff line change @@ -87,8 +87,50 @@ type SharedData struct {
8787 ShipNames ShipNames
8888}
8989
90- func ToTitle (s string ) string { return strings .Map (unicode .ToTitle , s ) }
90+ // COPY PASTE FROM https://cs.opensource.google/go/go/+/refs/tags/go1.21.10:src/strings/strings.go;l=752
91+ // isSeparator reports whether the rune could mark a word boundary.
92+ // TODO: update when package unicode captures more of the properties.
93+ func isSeparator (r rune ) bool {
94+ // ASCII alphanumerics and underscore are not separators
95+ if r <= 0x7F {
96+ switch {
97+ case '0' <= r && r <= '9' :
98+ return false
99+ case 'a' <= r && r <= 'z' :
100+ return false
101+ case 'A' <= r && r <= 'Z' :
102+ return false
103+ case r == '_' :
104+ return false
105+ }
106+ return true
107+ }
108+ // Letters and digits are not separators
109+ if unicode .IsLetter (r ) || unicode .IsDigit (r ) {
110+ return false
111+ }
112+ // Otherwise, all we can do for now is treat spaces as separators.
113+ return unicode .IsSpace (r )
114+ }
115+
116+ // COPY PASTE FROM https://cs.opensource.google/go/go/+/refs/tags/go1.21.10:src/strings/strings.go;l=782
117+ func Title (s string ) string {
118+ // Use a closure here to remember state.
119+ // Hackish but effective. Depends on Map scanning in order and calling
120+ // the closure once per rune.
121+ prev := ' '
122+ return strings .Map (
123+ func (r rune ) rune {
124+ if isSeparator (prev ) {
125+ prev = r
126+ return unicode .ToTitle (r )
127+ }
128+ prev = r
129+ return r
130+ },
131+ s )
132+ }
91133
92134func ToCapital (value string ) string {
93- return ToTitle (value )
135+ return Title (value )
94136}
You can’t perform that action at this time.
0 commit comments