package main
import (
"fmt"
"regexp"
"strings"
"unicode"
)
func toPascalCase(input string) string {
// 1. Handle camelCase by inserting spaces
re := regexp.MustCompile("([a-z])([A-Z])")
input = re.ReplaceAllString(input, "$1 $2")
// 2. Replace non-breaking spaces with regular spaces
input = strings.ReplaceAll(input, "\u00A0", " ")
// 3. Split into words using spaces and underscores as delimiters
words := strings.FieldsFunc(strings.ToLower(input), func(r rune) bool {
return unicode.IsSpace(r) || r == '_'
})
var result string
for _, word := range words {
if len(word) > 0 {
// 4. Capitalize the first letter of each word
runes := []rune(word)
runes[0] = unicode.ToUpper(runes[0])
result += string(runes)
}
}
return result
}
func main() {
fmt.Println(toPascalCase("get file content"))
fmt.Println(toPascalCase("get_file_content"))
fmt.Println(toPascalCase("get______file___content"))
fmt.Println(toPascalCase("get______file____\u00A0 content"))
fmt.Println(toPascalCase("GET FILE CONTENT"))
fmt.Println(toPascalCase("get\u00A0 \u00A0 file\u00A0 \u00A0 \u00A0 content"))
fmt.Println(toPascalCase("getFileContent"))
fmt.Println(toPascalCase(" get file content"))
fmt.Println(toPascalCase("get file content "))
}
/*
run:
GetFileContent
GetFileContent
GetFileContent
GetFileContent
GetFileContent
GetFileContent
GetFileContent
GetFileContent
GetFileContent
*/