Skip to content

Commit

Permalink
stringutil.Capitalize
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Jan 12, 2025
1 parent ef37901 commit 5e9ce17
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions stringutil/transform.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright 2025 Qiniu Limited (qiniu.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package stringutil

import (
"unicode"
"unicode/utf8"
)

// Capitalize returns a copy of the string str with the first letter mapped to
// its upper case.
func Capitalize(str string) string {
c, nc := utf8.DecodeRuneInString(str)
if c == utf8.RuneError || unicode.IsUpper(c) {
return str
}
ret := make([]byte, len(str))
nr := utf8.EncodeRune(ret, unicode.ToUpper(c))
ret = append(ret[:nr], str[nc:]...)
return String(ret)

Check warning on line 34 in stringutil/transform.go

View check run for this annotation

Codecov / codecov/patch

stringutil/transform.go#L26-L34

Added lines #L26 - L34 were not covered by tests
}

0 comments on commit 5e9ce17

Please sign in to comment.