Created
May 28, 2018 18:19
-
-
Save locona/f9e8c69eb93c1952fb4ca03022ceabea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| func camelCase(s string) string { | |
| s = strings.TrimSpace(s) | |
| buffer := make([]rune, 0, len(s)) | |
| var prev rune | |
| for _, curr := range s { | |
| if !isDelimiter(curr) { | |
| if isDelimiter(prev) || (prev == 0) { | |
| buffer = append(buffer, toUpper(curr)) | |
| } else { | |
| buffer = append(buffer, toLower(curr)) | |
| } | |
| } | |
| prev = curr | |
| } | |
| return string(buffer) | |
| } | |
| func isDelimiter(ch rune) bool { | |
| return ch == '-' || ch == '_' || isSpace(ch) | |
| } | |
| func toUpper(ch rune) rune { | |
| if ch >= 'a' && ch <= 'z' { | |
| return ch - 32 | |
| } | |
| return ch | |
| } | |
| func toLower(ch rune) rune { | |
| if ch >= 'A' && ch <= 'Z' { | |
| return ch + 32 | |
| } | |
| return ch | |
| } | |
| func isSpace(ch rune) bool { | |
| return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment