Skip to content

Instantly share code, notes, and snippets.

@tmaiaroto
Created November 9, 2014 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmaiaroto/5970ef8918e5074ce6d1 to your computer and use it in GitHub Desktop.
Save tmaiaroto/5970ef8918e5074ce6d1 to your computer and use it in GitHub Desktop.
Golang: somewhat faster string uppercase / lowercase (for certain strings)
// See: https://groups.google.com/forum/#!topic/golang-nuts/Il2DX4xpW3w
// A slightly faster lowercase function.
func toLower(s string) string {
b := make([]byte, len(s))
for i := range b {
c := s[i]
if c >= 'A' && c <= 'Z' {
c += 'a' - 'A'
}
b[i] = c
}
return string(b)
}
// A slightly faster uppercase function.
func toUpper(s string) string {
b := make([]byte, len(s))
for i := range b {
c := s[i]
if c >= 'a' && c <= 'z' {
c -= 'a' - 'A'
}
b[i] = c
}
return string(b)
}
@kalashnikovisme
Copy link

Unfortunately, it can be used only with latin symbols...

@tmaiaroto
Copy link
Author

Depends on what ya need 🤷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment