Skip to content

Commit

Permalink
fix SpaceGoCase for "HTTPServer" -> "HTTP Server"
Browse files Browse the repository at this point in the history
  • Loading branch information
ungerik committed Aug 5, 2024
1 parent 508fc3c commit eeb10f0
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 6 deletions.
10 changes: 5 additions & 5 deletions exceltable/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ require (
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/richardlehane/mscfb v1.0.4 // indirect
github.com/richardlehane/msoleps v1.0.3 // indirect
github.com/xuri/efp v0.0.0-20231025114914-d1ff6096ae53 // indirect
github.com/xuri/nfp v0.0.0-20230919160717-d98342af3f05 // indirect
golang.org/x/crypto v0.20.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/text v0.14.0 // indirect
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d // indirect
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/text v0.16.0 // indirect
)
10 changes: 10 additions & 0 deletions exceltable/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,27 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/xuri/efp v0.0.0-20231025114914-d1ff6096ae53 h1:Chd9DkqERQQuHpXjR/HSV1jLZA6uaoiwwH3vSuF3IW0=
github.com/xuri/efp v0.0.0-20231025114914-d1ff6096ae53/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d h1:llb0neMWDQe87IzJLS4Ci7psK/lVsjIS2otl+1WyRyY=
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
github.com/xuri/excelize/v2 v2.8.1 h1:pZLMEwK8ep+CLIUWpWmvW8IWE/yxqG0I1xcN6cVMGuQ=
github.com/xuri/excelize/v2 v2.8.1/go.mod h1:oli1E4C3Pa5RXg1TBXn4ENCXDV5JUMlBluUhG7c+CEE=
github.com/xuri/nfp v0.0.0-20230919160717-d98342af3f05 h1:qhbILQo1K3mphbwKh1vNm4oGezE1eF9fQWmNiIpSfI4=
github.com/xuri/nfp v0.0.0-20230919160717-d98342af3f05/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 h1:hPVCafDV85blFTabnqKgNhDCkJX25eik94Si9cTER4A=
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
golang.org/x/crypto v0.20.0 h1:jmAMJJZXr5KiCw05dfYK9QnqaqKLYXijU23lsEdcQqg=
golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4=
golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
5 changes: 5 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
49 changes: 49 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,55 @@ func SpacePascalCase(name string) string {
return strings.TrimSpace(b.String())
}

// SpaceGoCase inserts spaces before upper case
// characters within Go case like names.
// The last upper case character in a sequence of upper case
// characters is interpreted as the start of a new word
// and a space is inserted before it.
// It also replaces underscore '_' characters with spaces.
// Usable for ReflectColumnTitles.UntaggedTitle
func SpaceGoCase(name string) string {
var b strings.Builder
b.Grow(len(name) + 4)
beforeLastWasUpper := false
lastWasUpper := true
lastWasSpace := true
for _, r := range name {
if r == '_' {
if !lastWasSpace {
b.WriteByte(' ')
}
lastWasUpper = false
lastWasSpace = true
continue
}
isUpper := unicode.IsUpper(r)
switch {
case isUpper && !lastWasUpper && !lastWasSpace:
// First upper case rune after lower case non-space rune
// "CamelCase" -> "Camel Case"
b.WriteByte(' ')
case !isUpper && lastWasUpper && beforeLastWasUpper:
// First lower case rune after two upper case runes assumes that
// the upper case part before the last upper case rune is an all upper case acronym
// "HTTPServer" -> "HTTP Server"
s := b.String()
lastR, lenLastR := utf8.DecodeLastRuneInString(s)
if lastR != utf8.RuneError {
b.Reset()
b.WriteString(s[:len(s)-lenLastR])
b.WriteByte(' ')
b.WriteRune(lastR)
}
}
b.WriteRune(r)
beforeLastWasUpper = lastWasUpper
lastWasUpper = isUpper
lastWasSpace = unicode.IsSpace(r)
}
return strings.TrimSpace(b.String())
}

// StringColumnWidths returns the column widths of the passed
// table as count of UTF-8 runes.
// maxCols limits the number of columns to consider,
Expand Down
29 changes: 28 additions & 1 deletion utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,38 @@ func TestSpacePascalCase(t *testing.T) {
{testName: "helloWorld_", name: "helloWorld_", want: "hello World"},
{testName: "ThisHasMoreSpacesForSure", name: "ThisHasMoreSpacesForSure", want: "This Has More Spaces For Sure"},
{testName: "ThisHasMore_Spaces__ForSure", name: "ThisHasMore_Spaces__ForSure", want: "This Has More Spaces For Sure"},
{testName: "HTTPServer", name: "HTTPServer", want: "HTTPServer"},
}
for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
if got := SpacePascalCase(tt.name); got != tt.want {
t.Errorf("SpacePascalCase() = %q, want %q", got, tt.want)
t.Errorf("SpacePascalCase(%#v) = %#v, want %#v", tt.name, got, tt.want)
}
})
}
}

func TestSpaceGoCase(t *testing.T) {
tests := []struct {
testName string
name string
want string
}{
{testName: "", name: "", want: ""},
{testName: "HelloWorld", name: "HelloWorld", want: "Hello World"},
{testName: "_Hello_World", name: "_Hello_World", want: "Hello World"},
{testName: "helloWorld", name: "helloWorld", want: "hello World"},
{testName: "helloWorld_", name: "helloWorld_", want: "hello World"},
{testName: "ThisHasMoreSpacesForSure", name: "ThisHasMoreSpacesForSure", want: "This Has More Spaces For Sure"},
{testName: "ThisHasMore_Spaces__ForSure", name: "ThisHasMore_Spaces__ForSure", want: "This Has More Spaces For Sure"},
{testName: "HTTPServer", name: "HTTPServer", want: "HTTP Server"},
{testName: "MyXMLFile", name: "MyXMLFile", want: "My XML File"},
{testName: "wantJPEG", name: "wantJPEG", want: "want JPEG"},
}
for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
if got := SpaceGoCase(tt.name); got != tt.want {
t.Errorf("SpaceGoCase(%#v) = %#v, want %#v", tt.name, got, tt.want)
}
})
}
Expand Down

0 comments on commit eeb10f0

Please sign in to comment.