mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-29 20:13:27 +00:00
e81ccc406b
Change all license headers to comply with REUSE specification. Fix #16132 Co-authored-by: flynnnnnnnnnn <flynnnnnnnnnn@github> Co-authored-by: John Olheiser <john.olheiser@gmail.com>
88 lines
2 KiB
Go
88 lines
2 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package util
|
|
|
|
import "github.com/yuin/goldmark/util"
|
|
|
|
func isSnakeCaseUpper(c byte) bool {
|
|
return 'A' <= c && c <= 'Z'
|
|
}
|
|
|
|
func isSnakeCaseLowerOrNumber(c byte) bool {
|
|
return 'a' <= c && c <= 'z' || '0' <= c && c <= '9'
|
|
}
|
|
|
|
// ToSnakeCase convert the input string to snake_case format.
|
|
//
|
|
// Some samples.
|
|
//
|
|
// "FirstName" => "first_name"
|
|
// "HTTPServer" => "http_server"
|
|
// "NoHTTPS" => "no_https"
|
|
// "GO_PATH" => "go_path"
|
|
// "GO PATH" => "go_path" // space is converted to underscore.
|
|
// "GO-PATH" => "go_path" // hyphen is converted to underscore.
|
|
func ToSnakeCase(input string) string {
|
|
if len(input) == 0 {
|
|
return ""
|
|
}
|
|
|
|
var res []byte
|
|
if len(input) == 1 {
|
|
c := input[0]
|
|
if isSnakeCaseUpper(c) {
|
|
res = []byte{c + 'a' - 'A'}
|
|
} else if isSnakeCaseLowerOrNumber(c) {
|
|
res = []byte{c}
|
|
} else {
|
|
res = []byte{'_'}
|
|
}
|
|
} else {
|
|
res = make([]byte, 0, len(input)*4/3)
|
|
pos := 0
|
|
needSep := false
|
|
for pos < len(input) {
|
|
c := input[pos]
|
|
if c >= 0x80 {
|
|
res = append(res, c)
|
|
pos++
|
|
continue
|
|
}
|
|
isUpper := isSnakeCaseUpper(c)
|
|
if isUpper || isSnakeCaseLowerOrNumber(c) {
|
|
end := pos + 1
|
|
if isUpper {
|
|
// skip the following upper letters
|
|
for end < len(input) && isSnakeCaseUpper(input[end]) {
|
|
end++
|
|
}
|
|
if end-pos > 1 && end < len(input) && isSnakeCaseLowerOrNumber(input[end]) {
|
|
end--
|
|
}
|
|
}
|
|
// skip the following lower or number letters
|
|
for end < len(input) && (isSnakeCaseLowerOrNumber(input[end]) || input[end] >= 0x80) {
|
|
end++
|
|
}
|
|
if needSep {
|
|
res = append(res, '_')
|
|
}
|
|
res = append(res, input[pos:end]...)
|
|
pos = end
|
|
needSep = true
|
|
} else {
|
|
res = append(res, '_')
|
|
pos++
|
|
needSep = false
|
|
}
|
|
}
|
|
for i := 0; i < len(res); i++ {
|
|
if isSnakeCaseUpper(res[i]) {
|
|
res[i] += 'a' - 'A'
|
|
}
|
|
}
|
|
}
|
|
return util.BytesToReadOnlyString(res)
|
|
}
|