ceph-csi/vendor/github.com/stoewer/go-strcase/snake.go
dependabot[bot] 07b05616a0 rebase: bump k8s.io/kubernetes from 1.26.2 to 1.27.2
Bumps [k8s.io/kubernetes](https://github.com/kubernetes/kubernetes) from 1.26.2 to 1.27.2.
- [Release notes](https://github.com/kubernetes/kubernetes/releases)
- [Commits](https://github.com/kubernetes/kubernetes/compare/v1.26.2...v1.27.2)

---
updated-dependencies:
- dependency-name: k8s.io/kubernetes
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-06-06 12:21:43 +00:00

59 lines
1.4 KiB
Go

// Copyright (c) 2017, A. Stoewer <adrian.stoewer@rz.ifi.lmu.de>
// All rights reserved.
package strcase
import (
"strings"
)
// SnakeCase converts a string into snake case.
func SnakeCase(s string) string {
return delimiterCase(s, '_', false)
}
// UpperSnakeCase converts a string into snake case with capital letters.
func UpperSnakeCase(s string) string {
return delimiterCase(s, '_', true)
}
// delimiterCase converts a string into snake_case or kebab-case depending on the delimiter passed
// as second argument. When upperCase is true the result will be UPPER_SNAKE_CASE or UPPER-KEBAB-CASE.
func delimiterCase(s string, delimiter rune, upperCase bool) string {
s = strings.TrimSpace(s)
buffer := make([]rune, 0, len(s)+3)
adjustCase := toLower
if upperCase {
adjustCase = toUpper
}
var prev rune
var curr rune
for _, next := range s {
if isDelimiter(curr) {
if !isDelimiter(prev) {
buffer = append(buffer, delimiter)
}
} else if isUpper(curr) {
if isLower(prev) || (isUpper(prev) && isLower(next)) {
buffer = append(buffer, delimiter)
}
buffer = append(buffer, adjustCase(curr))
} else if curr != 0 {
buffer = append(buffer, adjustCase(curr))
}
prev = curr
curr = next
}
if len(s) > 0 {
if isUpper(curr) && isLower(prev) && prev != 0 {
buffer = append(buffer, delimiter)
}
buffer = append(buffer, adjustCase(curr))
}
return string(buffer)
}